Beruflich Dokumente
Kultur Dokumente
Theory:
Array:
An array is a collection of data that holds fixed number of values of same type. For example: if you want
to store marks of 100 students, you can create an array for it.
float marks[100];
The size and type of arrays cannot be changed after its declaration.
1. One-dimensional arrays
data_type array_name[array_size];
For example,
float mark[5];
Here, we declared an array, mark, of floating-point type and size 5. Meaning, it can hold 5 floating-point
values.
Suppose you declared an array mark as above. The first element is mark[0], second element
is mark[1] and so on.
Few key notes:
If the size of an array is n, to access the last element, (n-1) index is used. In this example, mark[4]
Suppose the starting address of mark[0] is 2120d. Then, the next address, a[1], will be 2124d,
address of a[2] will be 2128d and so on. It's because the size of a float is 4 bytes.
Here,
mark[0] is equal to 19
mark[1] is equal to 10
mark[2] is equal to 8
mark[3] is equal to 17
mark[4] is equal to 9
mark[2] = 9;
// take input from the user and insert in third element
scanf("%d", &mark[2]);
scanf("%d", &mark[i]);
printf("%d", mark[0]);
printf("%d", mark[i-1]);
Example: C Arrays
#include <stdio.h>
int main()
printf("Enter n: ");
scanf("%d", &n);
scanf("%d", &marks[i]);
sum += marks[i];
}
average = sum/n;
return 0;
Output
Enter n: 5
Enter number1: 45
Enter number2: 35
Enter number3: 38
Enter number4: 31
Enter number5: 49
Average = 39
Multidimensional Array:
In C, we can define multidimensional arrays in simple words as array of arrays. Data in multidimensional
arrays are stored in tabular form (in row major order).
data_type array_name[size1][size2]....[sizeN];
Examples:
Two dimensional array:
int two_d[10][20];
int three_d[10][20][30];
Total number of elements that can be stored in a multidimensional array can be calculated by multiplying
the size of all the dimensions.
For example:
The array int x[10][20] can store total (10*20) = 200 elements.
Similarly array int x[5][10][20] can store total (5*10*20) = 1000 elements.
data_type array_name[x][y];
We can declare a two dimensional integer array say ‘x’ of size 10,20 as:
int x[10][20];
Elements in two-dimensional arrays are commonly referred by x[i][j] where i is the row number
and ‘j’ is the column number.
A two – dimensional array can be seen as a table with ‘x’ rows and ‘y’ columns where the row
number ranges from 0 to (x-1) and column number ranges from 0 to (y-1). A two – dimensional
array ‘x’ with 3 rows and 3 columns is shown below:
The above array have 3 rows and 4 columns. The elements in the braces from left to right are stored in
the table also from left to right. The elements will be filled in the array in the order, first 4 elements from
the left in first row, next 4 elements in second row and so on.
Better Method:
This type of initialization make use of nested braces. Each set of inner braces represents one row. In the
above example there are total three rows so there are three sets of inner braces.
Elements in Two-Dimensional arrays are accessed using the row indexes and column indexes.
Example:
int x[2][1];
The above example represents the element present in third row and second column.
C program to find the sum of two matrices of order 2*2 using multidimensional arrays.
#include <stdio.h>
int main()
scanf("%f", &a[i][j]);
scanf("%f", &b[i][j]);
printf("\nSum Of Matrix:");
printf("%.1f\t", c[i][j]);
if(j==1)
printf("\n");
return 0;
Ouput
Enter a11: 2;
Enter a22: 2;
Enter b12: 0;
Sum Of Matrix:
2.2 0.5
-0.9 25.0
#include<conio.h>
void main()
int i, j, k;
int arr[3][3][3]=
},
},
},
};
clrscr();
for(i=0;i<3;i++)
for(j=0;j<3;j++)
for(k=0;k<3;k++)
printf("%d\t",arr[i][j][k]);
printf("\n");
printf("\n");
getch();
OUTPUT:
Conclusion: