Array is a collection of data of same types stored in sequential memory location. It is a linear data structure, where data is stored linearly one after the other. The elements in an array is accessed using an index. In C, the index of array starts from zero. For example, in an array of n elements, the first element has index zero and the last element has index (n-1). Elements with consecutive index (i.e. i and i+1) are stored in consecutive memory location in the system.
2. Multi-Dimensional Array
1. Two Dimensional Array
2. Three Dimensional Array
3. Accessing elements of Array
4. Array as function parameter
In C programming language, array can be divided into following types:
- One Dimensional Array
- Multi Dimensional Array
1. One Dimensional Array
An array where data is arranged in a single dimension is called one dimensional array.
Syntax for declaration of One Dimensional Array
datatype array_name[size];
Here, array_name is an array of type datatype and size is the number of elements in that array.
For example,
int a[5]; // declares an integer array with 5 elements float f[5]; // declares an float array with 5 elements char c[10]; // declares an character array with 10 elements
Arrangement of One Dimensional Array
Example of One Dimensional Array
Program #1: C Program to enter 5 numbers and print them in reverse order.
#include<stdio.h> int main() { int i,a[10]; printf("Enter 5 numbersn"); for(i=0;i<5;i++) { scanf("%d",&a[i]); } printf("Reverse ordern"); for(i=4;i>=0;i--) { printf("%dn",a[i]); } return 0; }
In this program, a one dimensional integer array with 5 elements is declared where these values are entered by the user. These values are then stored in respective index of array a using a for loop. Then, these values are printed in reverse order, i.e. the value with higher index is printed at first, by traversing through the array.
Output
Enter 5 numbers 11 34 -21 6 90 Reverse order 90 6 -21 34 11
2. Multi-Dimensional Array
An array where data are arranged in multiple dimensions is called multi-dimensional array. In other words, a multi-dimensional array is an array of another array with one dimension less i.e. a two dimensional array is an array of one dimensional array, a three dimensional array is an array of two dimensional array and so on. An array can have as many dimensions as required. However, two dimensional and three dimensional array are commonly used.
Syntax for declaration of Multi-Dimensional Array
datatype array_name[d1][d2]...[dn];
Here, array_name is an array of type datatype and it has n dimensions.
The number of elements in a multi dimensional array is equal to the product of size of all dimensions i.e. total number of elements in array array_name is d1*d2* … dn.
Two Dimensional Array
An array where data is arranged in two dimensions is called two dimensional array.
Syntax and Declaration of Two Dimensional Array
datatype array_name[d1][d2];
Here, array_name is an array of type datatype and it has 2 dimensions. The number of elements in array_name is equal to d1*d2.
For example,
int i[5][5]; // declares an integer array with 25 elements float f[5][10]; // declares an float array with 50 elements char n[5][10]; // declares an character array with 50 elements
Arrangement of Two Dimensional Array
Example #2: C Program to find the sum of all the elements of a 2 x 3 matrix.
#include<stdio.h> int main() { int i,j,a[2][3],sum=0; printf("Enter elements of matrixn"); for(i=0;i<2;i++) { for(j=0;j<3;j++) { printf("a[%d][%d] = ",i+1,j+1); scanf("%d",&a[i][j]); sum = sum + a[i][j]; } } printf("Sum = %d",sum); return 0; }
In this program, a 2 x 3 matrix is represented by a two dimensional array a[2][3]. The elements of matrix are entered by user. A variable sum is declared and initialized to zero that holds the sum. A nested for loop is used to enter the elements of matrix and to add them with sum. Finally, the sum is printed after the loop is terminated.
Output
Enter elements of matrix a[1][1] = 10 a[1][2] = 4 a[1][3] = 21 a[2][1] = 7 a[2][2] = 19 a[2][3] = 0 Sum = 61
Three Dimensional Array
An array where data is arranged in three dimensions is called three dimensional array.
Syntax for declaration of Three Dimensional Array
datatype array_name[d1][d2][d3];
Here, array_name is an array of type datatype and it has 3 dimensions. The number of elements in array_name is equal to d1*d2*d3.
For example,
int i[5][5][10]; // declares an integer array with 250 elements float f[2][5][10]; // declares an float array with 100 elements char n[5][5][10]; // declares an character array with 250 elements
Example #3: C program to show the concept of three dimensional array.
#include <stdio.h> int main() { int a[10][10][10],d1,d2,d3,i,j,k; printf("Enter size of three dimensions: "); scanf("%d%d%d",&d1,&d2,&d3); printf("Enter elements of arrayn"); for(i=0;i<d1;i++) { for(j=0;j<d2;j++) { for(k=0;k<d3;k++) { printf("a[%d][%d][%d] = ",i,j,k); scanf("%d",&a[i][j][k]); } } } printf("Displaying elements of arrayn"); for(i=0;i<d1;i++) for(j=0;j<d2;j++) for(k=0;k<d3;k++) printf("a[%d][%d][%d] = %dn",i,j,k,a[i][j][k]); getch(); return 0; }
This example show how data is stored and accessed from a three dimensional array. The values of size of three dimensions: d1, d2 and d3 are entered by user. According to these values, a nested loop is created to enter the value of elements of array and display them. The outermost loop runs d1 times, middle loop runs d2 times and the innermost loop runs d3 times printing the values of the array a as they go accessed by their index.
Output
Enter size of three dimensions: 2 2 2 Enter elements of array a[0][0][0] = 5 a[0][0][1] = 9 a[0][1][0] = -11 a[0][1][1] = 23 a[1][0][0] = 45 a[1][0][1] = -111 a[1][1][0] = 2 a[1][1][1] = 0 Displaying elements of array a[0][0][0] = 5 a[0][0][1] = 9 a[0][1][0] = -11 a[0][1][1] = 23 a[1][0][0] = 45 a[1][0][1] = -111 a[1][1][0] = 2 a[1][1][1] = 0
Accessing elements of array
Elements of an array can be accessed by using the name of array and index of element to be accessed.
One Dimensional Array
For example, consider a one dimensional array
int x[10];
Now, fifth element of this array can be accessed as x[4].
Note: First element of array has index 0, so the index of nth element is n-1.
Multi Dimensional Array
Elements of multi dimensional array can be accessed similarly as one dimensional array.
For example, consider a two dimensional array
char x[5][10];
Now, element of third row and second column can be accessed as x[2][1].
Array as function parameter
Array can be passed through function as parameter.
Declaration of function parameter
return_type function_name (datatype [ ], [other arguments type]); or return_type function_name (datatype [size], [other arguments type]);
The size of array may be defined or not during declaration.
Declaration of function named parameter
Functions with array as parameter can be defined in similar way.
return_type function_name (datatype array_name[ ], [other arguments]) { body of function; } or return_type function_name (datatype array_name[size], [other arguments]) { body of function; }
Declaration of function type-less parameter
Similarly, such functions can be called as follows:
function_name (array_name, [other arguments]);
Example #4: C Program to pass an array as function parameter
#include<stdio.h> void func(int [], int); // function declaration int main() { int a[5]={1,2,3,4,5}; func(a,5); // function call return 0; } void func(int x[], int size) // function definition { int i; for(i=0;i<5;i++) printf("%dn",x[i]); }
In this program an array x is passed as parameter from main() to a function func(). The contents of array are printed using a for loop.
Output
1 2 3 4 5