An Array is a collection of similar data items,
that are stored under a common name.
Array might belonging to any of the data types
Array size must be a constant value.
Always, Contiguous(adjacent) memory
locations are used to store array elements in
memory.
Stores the elements of same data type.
Used for maintaining multiple variable names
using a single name.
Used for sorting elements.
Matrix operations can be performed using
arrays.
Arrays are also used in CPU scheduling.
Array declaration:
› Syntax:
data_type array_name[size];
Example: int x[3];
X[0]
X[1]
X[2]
x
The initializer for an array is a comma-separated list of
constant expressions enclosed in braces ({ }).
The initializer is preceded by an equal sign (=).
It is not necessary to initialize all elements in an array. If an
array is partially initialized, elements that are not initialized
receive the value of the appropriate type.
Array initialization can be made either :
› At compile time or
› At run time
The value in x[0][0] is 1
The value in x[0][1] is 50
The value in x[1][0] is 2
The value in x[1][1] is 75
The value in x[2][0] is 3
The value in x[2][1] is 65
step2:
printf("n Enter the elements of matrix A n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("n Enter the elements of matrix B n");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
scanf("t%d",&b[i][j]);
}
}
Enter the size of matrix A: 2
2
Enter the size of matrix B: 2
2
Enter the elements of matrix A
2
2
2
2
Enter the elements of matrix B
3
3
3
3
The resultant matrix after addition of A&B is
5 5
5 5
step2:
printf("n Enter the elements of matrix A n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("n Enter the elements of matrix B n");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
scanf("t%d",&b[i][j]);
}
}
Enter the size of matrix A:2
2
Enter the size of matrix B:2
2
Enter the elements of matrix A
4
4
4
4
Enter the elements of matrix B
4
4
4
4
The resultant matrix is
32 32
32 32
Enter the size of matrix A:2
3
Enter the size of matrix B:3
2
Enter the elements of matrix A
1
2
3
4
5
6
Enter the elements of matrix B
2
4
6
8
2
4
The resultant matrix is
20 32
50 80
An array can be passed as a parameter to a function by
specifying the array's name without an index.
Here an array is transferred as parameter to a function.
void main() void fun(n,b[])
{ {
void fun(int,int); int x,b[5];
int a[5],n; …………..
…………… …………..
fun(n,a);
…………… }
}
Arrays can have more than one dimension, these arrays-of-
arrays are called multidimensional arrays.
Here is the general form of a multidimensional array
declaration:
datatype array_name [size1][size2]….[size n]
datatype - type of the data.
array_name -name of the array.
size -size of the array.
They are very similar to standard arrays with
the exception that they have multiple sets of
square brackets after the array identifier.
A two dimensional array can be thought of as a
grid of rows and columns.
A two-dimensional array is an example in this
section, although the techniques can be
extended to three or more dimensions.
The simplest form of the multidimensional array
is the two-dimensional array.
Example:
int a[3][3][3];
Col 0 Col 1 Col 2
row 0
row 1
row 2
X[0][0]
X[1][0]
X[2][0]
X[0][1]
X[1][1]
X[2][1]
X[0][2]
X[1][2]
X[2][2]