MODULE III
ARRAYS
Definition:
An array is a collection of elements of the same data type which is stored in consecutive memory
locations. Each data item of an array is called an element and each element is located in separated
memory location.
Each value in an array is indicated by the same name that is array name and an index which indicates
the position of value in an array. The datatype of an array can be int, float, double and char.
Representation of index:
In the below example, ‘a’ represents the name of an array. ‘0’ to ‘4’ represents the index.
The index always begins from zero (0) and continues till (size-1) position.
Ex: int a[5];
a[0] a[1] a[2] a[3] a[4]
a
Any element in an array can be accessed using
Name of the array
Position of the element in an array.
Types of Arrays:
One dimensional array
Two Dimensional array
Multidimensional array
One dimensional array(or) 1D array:
Definition:
The is the simplest type of array that contains only one row (linear list) for storing the values of
same datatype.
Declaration:
datatype array_name[size];
where datatype: type of an array.
array_name: is the name of the array that represent the set of values.
size: the number of values that can be stored.
Ex: int a[5];
a[0] a[1] a[2] a[3] a[4]
a
Memory occupied by one dimensional array is
Total memory= (array_size) * sizeof(datatype).
Methods of initializing one dimensional array:
Initialization of array can be done in 3 ways. They are:
Initialize in a single statement.
Assigning values to each index.
Read input from keyboard.
Initialize in a single statement:
Basic Initialization: Initialize all values of the array at the beginning. Ex: int a[5]={10,20,30,40,50};
a[0] a[1] a[2] a[3] a[4]
a 10 20 30 40 50
Initialization without size: We needn’t have to specify the size of array provided we are initializing the
values in beginning itself
Ex: int a[ ]={10,20,30,40,50};
a[0] a[1] a[2] a[3] a[4]
a 10 20 30 40 50
Partial Initialization: If we not specify the all the elements in the array, the unspecified elements will
be initialized to zero
Ex: int a[5]={10,20};
a[0] a[1] a[2] a[3] a[4]
a 10 20 0 0 0
Initialization with zero: All the elements in the array will be initialized to zero. Ex: int a[5]={0};
a[0] a[1] a[2] a[3] a[4]
a 0 0 0 0 0
Assigning values to each index:
Initialization of elements can be done by one by one using the below syntax. Ex: int a[5];
a[0]=10;
a[1]=20;
a[2]=30;
a[3]=40;
a[4]=50;
a[0] a[1] a[2] a[3] a[4]
a 10 20 30 40 50
Read input from keyboard:
The values to array can be read using scanf.
Syntax:
Consider an array a[n] where n value can be given directly (i.e., a[5]) or can be read using scanf(i.e.,
scanf(“%d”, &n);) and ‘i’ is used to read the index values.
for(i=0;i<n;i++)
{
scanf(“%d”, &a[i]);
}
Methods of accessing one dimensional array:
C uses indexes to access individual elements in an array. Ex: To get the first element of anarray, a[0]
can be used.
To print the elements of array, individual elements (or) all the elements of an array can be done using
printf statement.
Ex:
To access individual element, printf(“ %d”,&a[0]);
To access all the elements,
for(i=0;i<n;i++)
{
printf(“%d\t”, &a[i]); (“/t” is used to get spaces in between the elements)
}
Write a program to read and display elements of a one dimensional array.
#include<stdio.h>
main()
{
int a[10], n, i;
printf(“ Enter the size of an array”);
scanf(“%d”,&n);
printf(“ Enter the elements of an array”);
for(i=0;i<n;i++)
{
scanf(“%d”, &a[i]);
}
printf(“ The array elements are:”);
for(i=0;i<n;i++)
{
printf(“%d\t”, &a[i]);
}
}
Output:
Enter the size of an array 5
Enter the elements of an array
1 2 3 4 5
The array elements are:
1 2 3 4 5
Write a program to print the position of smallest of numbers using array.
#include<stdio.h>
void main()
{
int i,a[20],n,small,pos;
printf("Enter the number of elements: ");
scanf("%d",&n);
printf("Enter the elements: ");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
small=a[0];
pos=0;
for(i=0;i<n;i++)
{
if(a[i]<small)
{
small=a[i];
pos=i;
}
}
printf("The smallest element is %d and the position is%d",small,pos+1);
}
Output:
Enter the number of elements: 5
Enter the elements: 2 3 4 5 6
The smallest element is 2 and the position is 1
Two dimensional array(or) 2D array
Definition:
The two dimensional array is the simplest type of the multi-dimensional array. A two dimensional
array is that which contains rows and columns for storing the values of same datatype.
Declaration:
datatype array_name[size1][size2]; where
datatype: type of an array.
array_name: nameof the array to represent the set of values. size1: the number of rows that can be
stored.
Size2: the number of columns that can be stored.
Ex: int a[3][5];
a[0][0] a[0][1] a[0][2] a[0][3] a[0][4]
a[1][0] a[1][1] a[1][2] a[1][3] a[1][4]
a[2][0] a[2][1] a[2][2] a[2][3] a[2][4]
Methods of initializing one dimensional array:
Initialization of array can be done in 3 ways. They are:
Initialize in a single statement.
Assigning values to each index.
Read input from keyboard.
Initialize in a single statement: provide values for all the elements in 2-D array. Ex: int a[3][3]={10,
20, 30, 40, 50, 60, 70, 80, 90};
10 20 30
40 50 60
70 80 90
Ex: int a[3][3]={{10,20,30},{40,50,60}, {70,80,90};
10 20 30
40 50 60
70 80 90
Ex: int a[3][3]={{10,20,30},{40,50}};
10 20 30
40 50
Assign all the elements in 2-D array with zero value.
Ex: int a[3][3]={0};
0 0 0
0 0 0
0 0 0
Assigning values to each index:
Initialization of elements can be done by one by one using the below syntax. Ex: int a[3][3];
a[0][0]=10;
a[0][1]=20;
a[1][0]=30;
a[1][1]=40;
10 20
30 40
Read input from keyboard:
The values to array can be read using scanf.
Syntax:
Consider an array a[m][n] where m & n value can be given directly (i.e., a[5][5]) or can be read using
scanf(i.e., scanf(“%d%d”, &m, &n);) and ‘i’ & ‘j’ is used to get the index values.
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf(“%d”, &a[i][j]);
}
}
Methods of accessing two dimensional array:
C uses indexes to access individual elements in an array. Ex: To get the one element of an array,
a[0][0] can be used.
To print the elements of array, individual elements (or) all the elements of an array can be done using
printf statement.
Example:
To access individual element,printf(“ %d”,a[0][1]);
To access all the elements,
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf(“%d\t”, a[i][j]); //(/t is used to spaces between the numbers)
}
printf(“\n”); // (/n is used to differentiate rows and columns)
}
Write a program to read and display elements of a two dimensional array.
#include<stdio.h>
main()
{
int a[10][10],m, j, n, i;
printf(“ Enter the no of rows and columns of an array”);
scanf(“%d%d”,&m, &n);
printf(“ Enter the elements of an array”);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf(“%d”, &a[i][j]);
}
}
printf(“ The array elements are:”);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf(“%d\t”, a[i][j]);
}
printf(“\n”);
}
}
Output:
Enter the no of rows and columns of an array
2 2
Enter the elements of an array
1 2 3 4
The array elements are:
1 2
3 4
Operations on 2-Dimensional Array
Sum
Difference
Product
Sum of two matrices:
Two matrix that are compatible with each other (i.e., two matrix of same size, with same number of
rows and columns) can be added together to store the result in 3rd matrix.
Ci,j =Ai,j+Bi,j
Write a program to input 2 m x n matrices and then calculate the sum of their corresponding
elements and store it in third m x n matrix.
#include<stdio.h> void main()
{
int a[20][20],b[20][20],c[20][20],m,n,p,q,r,t,i,j;
printf("Enter the number of rows and columns in first matrix: ");
scanf("%d,%d",&m,&n);
printf("Enter the number of rows and columns in second matrix: ");
scanf("%d,%d",&p,&q);
if(m!=p||n!=q)
{
printf("Number of rows and columns of both the matrix should be equal");
}
r=m; t=n;
printf("Enter the elements of the array 1:");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Enter the elements of the array 2:");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
scanf("%d",&b[i][j]);
}
}
if((m==p) && (n==q))
{
for(i=0;i<r;i++)
{
for(j=0;j<t;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
}
printf("The elements of the resultant matrix are:\n"); for(i=0;i<r;i++)
{
for(j=0;j<t;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
}
Output:
Enter the number of rows and columns in first matrix: 2,2
Enter the number of rows and columns in second matrix: 2,2
Enter the elements of the array 1:2 2 2 2
Enter the elements of the array 2:2 2 2 2
The elements of the resultant matrix are:
4 4
5 4
Difference of two matrix:
Two matrix that are compatible with each other (i.e., two matrix of same size, with same number of
rows and columns) can be subtracted together to store the result in 3rd matrix.
Ci,j =Ai,j-Bi,j
Write a program to input 2 m x n matrices and then calculate the difference of their
corresponding elements and store it in third m x n matrix.
#include<stdio.h>
void main()
{
int a[20][20],b[20][20],c[20][20],m,n,p,q,r,t,i,j;
printf("Enter the number of rows and columns in first matrix: ");
scanf("%d,%d",&m,&n);
printf("Enter the number of rows and columns in second matrix: ");
scanf("%d,%d",&p,&q);
if(m!=p||n!=q)
{
printf("Number of rows and columns of both the matrix should be equal");
}
r=m; t=n;
printf("Enter the elements of the array 1:");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Enter the elements of the array 2:");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
scanf("%d",&b[i][j]);
}
}
if((m==p) && (n==q))
{
for(i=0;i<r;i++)
{
for(j=0;j<t;j++)
{
c[i][j]=a[i][j]-b[i][j];
}
}
}
printf("The elements of the resultant matrix are:\n");
for(i=0;i<r;i++)
{
for(j=0;j<t;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
}
Output:
Enter the number of rows and columns in first matrix: 2,2
Enter the number of rows and columns in second matrix: 2,2
Enter the elements of the array 1:3 3 3 3
Enter the elements of the array 2:2 2 2 2
The elements of the resultant matrix are:
1 1
1 1
Product of two matrix:
Two matrices can be multiplied with each other if the number of columns in the first matrix is equal to
number of rows in second matrix.
That is matrix A with size m X n can be multiplied with B matrix with size p X q, only if n = p.
C i , j = ∑A i , k - B k , j for k=1 to k < n
Write a program to input 2 m x n matrices and then calculate the product of their
corresponding elements and store it in third m x n matrix.
#include<stdio.h> void main()
{
int a[20][20],b[20][20],c[20][20],m,n,p,q,k,i,j;
printf("Enter the number of rows and columns in first matrix: ");
scanf("%d,%d",&m,&n);
printf("Enter the number of rows and columns in second matrix: ");
scanf("%d,%d",&p,&q);
if(n!=p)
{
printf("Matrix multiplication is not possible");
}
printf("Enter the elements of the array 1:");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Enter the elements of the array 2:");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
scanf("%d",&b[i][j]);
}
}
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
c[i][j]=0;
for(k=0;k<n;k++)
c[i][j]=a[i][k]*b[k][j]+c[i][j];
}
}
printf("The elements of the resultant matrix are:\n");
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
}
Output:
Enter the number of rows and columns in first matrix: 2,2
Enter the number of rows and columns in second matrix: 2,2
Enter the elements of the array 1:2 2 2 2
Enter the elements of the array 2:2 2 2 2
The elements of the resultant matrix are:
88
88