0% found this document useful (0 votes)
0 views6 pages

Arrayy Notes

The document provides an overview of arrays in C programming, defining them as collections of similar data types stored in contiguous memory locations. It explains the properties, advantages, and operations of arrays, including insertion and deletion, along with examples of code. Additionally, it discusses memory allocation, indexing methods, and the time and space complexity of various array operations.

Uploaded by

smitauscs21
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views6 pages

Arrayy Notes

The document provides an overview of arrays in C programming, defining them as collections of similar data types stored in contiguous memory locations. It explains the properties, advantages, and operations of arrays, including insertion and deletion, along with examples of code. Additionally, it discusses memory allocation, indexing methods, and the time and space complexity of various array operations.

Uploaded by

smitauscs21
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Data Structure Using 'C'

(BCA-C151)
Data Structure Using 'C'

Lecture-2

Array

Definition:

Arrays are defined as the collection of similar type of data items stored at contiguous memory locations.

Arrays are the derived data type in C programming language which can store the primitive type of data
such as int, char, double, float, etc.

Array is the simplest data structure where each data element can be randomly accessed by using its
index number.

For example, if we want to store the marks of a student in 6 subjects, then we don't need to define
different variable for the marks in different subject instead of that, we can define an array which can
store the marks in each subject at a contiguous memory locations.

The array marks[10] defines the marks of the student in10 different subjects where each subject marks
are locate data particular subscript in the array i.e. marks[0] denotes the marks in first subject, marks[1]
denotes the marks in 2nd subject and soon.

Properties of the Array:

1) Each element is of same data type and carries a same size i.e.int = 4bytes.
2) Elements of the array are stored at contiguous memory locations where the first
element is stored at the smallest memory location.
3) Elements of the array can be randomly accessed in we can calculate the address of
each element of the array with the given base address and the size of data element.
For example, in C language, the syntax of declaring an array is like following:
Int arr [10]; char a [10]; float ar [5];

Need of using Array:

In computer programming, the most of the cases requires to store the large number of data of similar
type. To store such amount of data, we need to define a large number of variables. It would be very
difficult to remember names of all the variables while writing the programs.

Instead of naming all the variables with a different name, it is better to define an array and store all the
elements into it.

Following example illustrates how array can be useful in writing code for a particular problem.

We have marks of a student in six different subjects. The problem intends to calculate the average of
all the marks of the student.

In order to explain the importance of array, we have created two programs, one is without using array
and other involves the use of array to store marks.

Program without array:

#include <stdio.h>
void main()

2| Content Writer: Dharmendra Kumar


Data Structure Using 'C'

{
int marks_1=56, marks_2=78, marks_3=88, marks_4=76, marks_5=56, marks_6=89;
float avg;
avg = (marks_1 + marks_2 + marks_3 + marks_4 + marks_5 + marks_6) / 6;
printf ("%f", avg);
}
Program by using array:

#include <stdio.h>
void main()
{
int marks [6]={56, 78, 88, 76, 56, 89};
int i;
float avg;
for ( i = 0; i < 6; i++)
{
avg = avg + marks[i];
}
Printf ("%f", avg);
}
Advantages of Array:

Array provides the single name for the group of variables of the same type therefore, it is easy
to remember the name of all the elements of an array. Traversing an array is a very simple
process; we just need to increment the base address of the array in order to visit each element
one by one. Any element in the array can be directly accessed by using the index.

Memory Allocation of the array:

As we have mentioned, all the data elements of an array are stored at contiguous locations in the main
memory. The name of the array represents the base address or the address of first element in the main
memory. Each element of the array is represented by a proper indexing.

The indexing of the array can be defined in three ways.

0 (zero – based indexing) : The first element of the array will be arr[0].

1(one – based indexing) : The first element of the array will be arr[1].

n (n – based indexing) : The first element of the array can reside at any random index number.

In the following image, we have shown the memory allocation of an array arr of size 5. The array
follows 0 - based indexing approach. The base address of the array is 100th byte. This will be the address
of arr[0]. Here, the size of int is 4 bytes therefore each element will take 4 bytes in the memory.

3| Content Writer: Dharmendra Kumar


Data Structure Using 'C'

In 0 based indexing, If the size of an array is n then the maximum index number, an element can have
is n-1. However, it will be n if we use 1 based indexing.

Passing array to the function:

The name of the array represents the starting address or the address of the first element of the array. All
the elements of the array can be traversed by using the base address. The following example illustrates
how the array can be passed to a function.

#include < stdio.h >


int summation (int [ ]);
void main()
{
int arr [5] = {0, 1, 2, 3, 4};
int sum = summation (arr);
printf ("%d", sum);
}
int summation (int arr [ ])
{
int sum = 0, i;
for(i = 0;i <5;i++)
{
sum = sum + arr[i];
}
return sum;
}
The above program defines a function named as summation which accepts an array as an argument.
The function calculates the sum of all the elements of the array and returns it.

Array operations:

Insertion:

To insert an element in an array at a specified location / position.

For example, if an array arr consists of elements arr={7, 8, 12, 3, 9} and if we want to insert element
27 at position 3 then the new array would be arr={7, 8, 12, 27, 3, 9}(as array starts from index0).

4| Content Writer: Dharmendra Kumar


Data Structure Using 'C'

Logic:

We start to iterate from the back. The reason for this is that all the elements after the element which is
inserted will be shifted by one place to the right.

Take input array ‘arr’ and number of elements (n) as 5

Let us take elements for array arr = {7, 8, 12, 3, 9}.

Let us take an element as 27 which is to be inserted at position 3.

1st iteration for (I = n-1 ; I >= pos; i– –) i.e.for (I = 5-1; I >= 3; i– –) i.e.for(I = 4;4 >=3; i– –)

arr [i+1] = arr [i] i.e. arr [4+1] = arr [4] i.e. arr [5] = arr [4] i.e. arr [5] = 9

2nd iteration for (I = n-1; I >= pos; i– –) i.e. for (i = 3;I >= 3; i– –) i.e. for (i = 3; 3 >=3;I – –)

arr [i+1] = arr[i] i.e. arr [3+1] = arr [3] i.e. arr [4] = arr [3] i.e.arr [4] = 3

Now we come out of the for loop as i (2) is less than pos (3)

n = n+1 i.e.n = 5 + 1 i.e. n=6 // We are increasing the number of elements

arr [pos]= number i.e. arr[3] = 27 // number = no. to be inserted

Hence our new array would be arr = {7, 8, 12, 27, 3, 9}.

Deletion: To delete an element from an array from a specified location / position.

For example, if an array arr consists of elements arr = {7, 8, 12, 3, 9} and if we want to delete element
at position 3 then the new array would be arr = {7, 8, 12, 9} (as array starts from index 0).

Logic:

We start to iterate from the position from which we want to delete the element. The reason for this is
that all the elements after the element which is deleted will be shifted by one place towards the left.

Take input array ‘arr’ and no of elements (n) a s5

Let us take elements for array arr ={7, 8, 12, 3, 9}.

Let us delete an element from the array, which is at position 3.

1st iteration for (i= pos; i<n-1; i++) i.e. for (i=3; i<5-1;i++) i.e. for (i=3;i=3<4;i++)

Arr [i] = arr [i+1] i.e. arr[3] = arr [3+1] i.e. arr [3] = arr [4] i.e. arr [3] = 9

Now we move out of the for loop as i will be 4 which is not less than n-1(4).

n=n-1i.e. n=5-1i.e n=4 //We are decreasing the no of elements

Hence our new array would be arr = {7, 8, 12, 9}.

Time and space complexity of various array operations are described in the following table.

5| Content Writer: Dharmendra Kumar


Data Structure Using 'C'

Time Complexity

Algorithm Average Case Worst Case

Access. O (1) O (1)

Search O (n) O (n)

Insertion O (n) O (n)

Deletion O (n) O (n)

Space Complexity

In array, space complexity for worst case is O(n).

6| Content Writer: Dharmendra Kumar

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy