Array
Dr. D. Sharma, Associate Professor, STME, NMIMS Indore
Dr. Dharmendr Sharma, Associate Professor, STME, NMIMS
Array
• An array is a user-defined data type that stores related information
together. All the information stored in an array belongs to the same
data type.
• Consider a situation in which we have 20 students in a class and we
have been asked to write a program that reads and prints the marks
of all the 20 students.
• to read the values of these 20 variables, we must have 20 read
statements.
• Print the value of these variables, we need 20 write statements
Dr. Dharmendr Sharma, Associate Professor, STME, NMIMS
Array
• An array is a collection of similar data elements
• elements of the array are stored in consecutive memory locations and
are referenced by an index
Dr. Dharmendr Sharma, Associate Professor, STME, NMIMS
DECLARATION OF ARRAYS
• Data type - the kind of values it can store, for example, int, char, float,
double.
• Name-to identify the array
• Size-the maximum number of values that the array can hold.
• type name[size];
• Example int marks[10];
Dr. Dharmendr Sharma, Associate Professor, STME, NMIMS
ACCESSING THE ELEMENTS OF AN
ARRAY
• To access all the elements, we must use a loop.
• we can access all the elements of an array by varying the value of the
subscript into the array
// Set each element of the array to –1
int i, marks[10];
for(i=0 ;i<10 ;i++)
marks[i] = –1;
Dr. Dharmendr Sharma, Associate Professor, STME, NMIMS
Calculating the Address of Array Elements
• Array name is a symbolic reference to the address of the first byte of
the array. When we use the array name, we are actually referring to
the first byte of the array.
• The subscript or the index represents the offset from the beginning of
the array to the element being referenced. That is, with just the array
name and the index, C can calculate the address of any element in the
array.
• Address of data element,
A[k] = BA(A) + w(k – lower_bound)
Dr. Dharmendr Sharma, Associate Professor, STME, NMIMS
Example
• Given an array int marks[] = {99,67,78,56,88,90,34,85}, calculate the address of
marks[4] if the base address = 1000.
• Sol:
99 67 78 56 88 90 34 85
marks[0] marks[1] marks[2] marks[3] marks[4] marks[5] marks[6] marks[7]
1000 1002 1004 1006 1008 1010 1012 1014
We know that storing an integer value requires 2 bytes, therefore, its size is 2
bytes.
marks[4] = 1000 + 2(4 – 0)
= 1000 + 2(4) = 1008
Dr. Dharmendr Sharma, Associate Professor, STME, NMIMS
STORING VALUES IN ARRAYS
• There are three ways to store values in an array.
• initialize the array
• input values for individual
• assign values to individual elements
Dr. Dharmendr Sharma, Associate Professor, STME, NMIMS
Initializing Arrays during Declaration
• type array_name[size]={list of values};
int marks[5]={90, 82, 78, 95, 88};
Dr. Dharmendr Sharma, Associate Professor, STME, NMIMS
Assigning Values to Individual Elements
marks[3] = 100;
int i, arr1[10], arr2[10 ];
arr1[10 ] = { 0,1,2,3,4,5,6,7,8,9};
for(i=0 ;i<10 ;i++)
arr2[i] = arr1[i];
// Fill an array with even numbers
int i,arr[1 ];
for(i=0;i<10;i++)
arr[i] = i*2;
Dr. Dharmendr Sharma, Associate Professor, STME, NMIMS
Inputting Values from the Keyboard
int i, marks[10 ];
for(i=0 ;i<10 ;i++)
scanf("%d", &marks[i]);
Dr. Dharmendr Sharma, Associate Professor, STME, NMIMS
OPERATIONS ON ARRAYS
• Traversing an array
• Inserting an element in an array
• Searching an element in an array
• Deleting an element from an array
• Merging two arrays
• Sorting an array in ascending or descending order
Dr. Dharmendr Sharma, Associate Professor, STME, NMIMS
Traversing an Array
• printing every element
• Step 1: [INITIALIZATION] SET I = lower_bound
• Step 2: Repeat Steps 3 to 4 while I <= upper_bound
• Step 3:Apply Process to A[I]
• Step 4: SET I = I + 1
• [END OF LOOP]
• Step 5: EXIT
• Write a program to read and display n numbers using an array.
• Write a program to find the mean of n numbers using arrays
Dr. Dharmendr Sharma, Associate Professor, STME, NMIMS
Algorithm to Insert an Element in the
Middle of an Array
• Step 1: [INITIALIZATION] SET I = N
• Step 2: Repeat Steps 3 and 4 while I >= POS
• Step 3:SET A[I + 1] = A[I]
• Step 4: SET I = I – 1 [END OF LOOP]
• Step 5: SET N = N + 1
• Step 6: SET A[POS] = VAL
• Step 7: EXIT
• Write a program to insert a number at a given location in an array.
Dr. Dharmendr Sharma, Associate Professor, STME, NMIMS
PASSING ARRAYS TO FUNCTIONS
Dr. Dharmendr Sharma, Associate Professor, STME, NMIMS
Passing Individual Elements-Passing data
values
Dr. Dharmendr Sharma, Associate Professor, STME, NMIMS
Passing Addresses
Dr. Dharmendr Sharma, Associate Professor, STME, NMIMS
Passing the Entire Array
• A function that accepts an array can declare the formal parameter in
either of the two following ways.
func(int arr[]); or func(int *arr);
• It is not necessary to pass the whole array to a function. We can also
pass a part of the array known as a sub-array. A pointer to a sub-array
is also an array pointer.
func(&arr[2], 8);
Dr. Dharmendr Sharma, Associate Professor, STME, NMIMS
Pointers And Arrays
• int arr[] = {1, 2, 3, 4, 5};
• The name of the array is the starting address of the array in memory.
It is also known as the base address.
main()
{
int arr[]={1,2,3,4,5};
printf("\n Address of array = %p %p %p", arr, &arr[0],&arr);
}
Dr. Dharmendr Sharma, Associate Professor, STME, NMIMS
Pointers And Arrays
• An error is generated if an attempt is made to change the address of
the array.
• When using pointers, an expression like *(arr+i) is equivalent to arr[i].
we can write
ptr = arr;
// ptr = &arr[0]
we cannot write
arr = ptr;
Dr. Dharmendr Sharma, Associate Professor, STME, NMIMS
Pointers And Arrays
• Look at the following code which modifies the contents of an array using a pointer to an array.
int main()
{ int arr[]={1,2,3,4,5};
int *ptr, i;
ptr=&arr[2];
*ptr = –1;
*(ptr+1) = 0;
*(ptr–1) = 1;
printf("\n Array is: ");
for(i=0;i<5;i++)
printf(" %d", *(arr+i));
return 0;}
Output: Array is: 1 1 –1 0 5
Dr. Dharmendr Sharma, Associate Professor, STME, NMIMS
Pointers And Arrays
• C also permits addition and subtraction of two pointer variables
int main()
{ int arr[]={1,2,3,4,5,6,7,8,9};
int *ptr1, *ptr2;
ptr1 = arr;
ptr2 = arr+2;
printf("%d", ptr2–ptr1);
return 0; }
• Output
•2
• Write a program to display an array of given numbers
Dr. Dharmendr Sharma, Associate Professor, STME, NMIMS
TWO-DIMENSIONAL ARRAYS
• A two dimensional array store data in the form of grids or tables
• A two-dimensional array is declared as:
data_type array_name[row_size][column_size];
• A two-dimensional array is an array that contains m X n data elements
and each element is accessed using two subscripts, i and j, where i <=
m and j <=n.
e.g. int marks[3][5];
Dr. Dharmendr Sharma, Associate Professor, STME, NMIMS
TWO-DIMENSIONAL ARRAYS
• There are two ways of storing a two-dimensional array in the memory.
• The row major order
(0,0) (0,1) (1,0) (1,1)
• The column major order.
(0,0) (1,0) (0,1) (1,1)
int a[2][2];
(0,0) (0,1)
(1,0) (1,1)
Dr. Dharmendr Sharma, Associate Professor, STME, NMIMS
Array Address Calculation
• computer stores the base address, and the address of the other
elements is calculated using the following formula.
• If the array elements are stored in column major order,
Address(A[I][J]) = Base_Address + w{M ( J – 1) + (I – 1)}
• And if the array elements are stored in row major order,
Address(A[I][J]) = Base_Address + w{N ( I – 1) + (J – 1)}
Dr. Dharmendr Sharma, Associate Professor, STME, NMIMS
Array Address Calculation
• Consider a 20 X5 two-dimensional array marks which has its base address =
1000 and the size of an element = 2. Now compute the address of the
element, marks[18][ 4] assuming that the elements are stored in row major
order.
• Solution
Address(A[I][J]) = Base_Address + w{N (I – 1) + (J – 1)}
Address(marks[18][4]) = 1000 + 2 {5(18 – 1) + (4 – 1)}
= 1000 + 2 {5(17) + 3}
= 1000 + 2 (88)
= 1000 + 176 = 1176
Dr. Dharmendr Sharma, Associate Professor, STME, NMIMS
Initializing Two-dimensional Arrays
• A two-dimensional array is initialized in the same way as a one-dimensional array
is initialized. For example,
int marks[2][3]={90, 87, 78, 68, 62, 71};
can also be written as:
int marks[2][3]={{90,87,78},{68, 62, 71}};
• In case of one-dimensional arrays, if the array is completely initialized, we may
omit the size of the array. The same concept can be applied to a two-dimensional
array, except that only the size of the first dimension can be omitted.
• int marks[][3]={{90,87,78},{68, 62, 71}};
• Write a program to print the elements of a 2D array
Dr. Dharmendr Sharma, Associate Professor, STME, NMIMS
Application of Array
• Arrays are widely used to implement mathematical vectors, matrices,
and other kinds of rectangular tables.
• Many databases include one-dimensional arrays whose elements are
records.
• Arrays are also used to implement other data structures such as
strings, stacks, queues, heaps, and hash tables. We will read about
these data structures in the subsequent chapters.
• Arrays can be used for sorting elements in ascending or descending
order.
Dr. Dharmendr Sharma, Associate Professor, STME, NMIMS
Thanks
Dr. Dharmendr Sharma, Associate Professor, STME, NMIMS