Module-4-2D and Multidimensional Arrays
Module-4-2D and Multidimensional Arrays
2-Dimensional Array
data_type array_name[row_size][column_size];
int a[2][3]={1,2,3,4,5,6}
int a[2][3]={{1,2,3},{4,5,6}}
3. Partial initialization
int a[2][3]={{1,1},{2}}
int a[ ][3]={{1,2,3},{4,5,6}}
for(i=0;i<row;i++)
for(j=0;j<column;j++)
scanf(“%d”,&a[i][j]);
#include<stdio.h>
void main()
int a[20][20],m,n,i,j;
scanf("%d,%d",&m,&n);
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
for(i=0;i<m;i++)
for(j=0;j<n;j++)
printf("%d\t",a[i][j]);
printf("\n");
Output:
1 2 3
4 5 6
7 8 9
1. Transpose
2. Sum
3. Difference
4. Product
#include<stdio.h>
void main()
int a[20][20],m,n,i,j,b[20][20];
scanf("%d,%d",&m,&n);
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
for(i=0;i<m;i++)
for(j=0;j<n;j++)
printf("%d\t",a[i][j]);
printf("\n");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
b[i][j]=a[j][i];
for(i=0;i<m;i++)
for(j=0;j<n;j++)
printf("%d\t",b[i][j]);
printf("\n");
Output:
1 2 3
4 5 6
7 8 9
1 4 7
2 5 8
3 6 9
#include<stdio.h>
int main()
int a[20][20],b[20][20],c[20][20],m,n,p,q,r,t,i,j;
scanf("%d,%d",&m,&n);
scanf("%d,%d",&p,&q);
if(m!=p||n!=q)
return 0;
r=m;
t=n;
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
for(i=0;i<p;i++)
for(j=0;j<q;j++)
scanf("%d",&b[i][j]);
for(i=0;i<r;i++)
for(j=0;j<t;j++)
c[i][j]=a[i][j]+b[i][j];
for(i=0;i<r;i++)
for(j=0;j<t;j++)
printf("%d\t",c[i][j]);
printf("\n");
Output:
4 4
4 4
#include<stdio.h>
int main()
int a[20][20],b[20][20],c[20][20],m,n,p,q,k,i,j;
scanf("%d,%d",&m,&n);
scanf("%d,%d",&p,&q);
if(n!=p)
return 0;
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
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];
for(i=0;i<m;i++)
for(j=0;j<q;j++)
printf("%d\t",c[i][j]);
printf("\n");
Output:
8 8
8 8
#include<stdio.h>
void main()
int n,a[10],i;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
square(a[i]);
void square(int x)
printf("%d\t",x*x);
return;
Output:
#include<stdio.h>
void main()
int b[6]={1,2,3,4,5,6};
avg(b);
int i,Average,sum=0;
for(i=0;i<6;i++)
sum=sum+a[i];
Average=sum/6;
printf("Average=%d",Average);
Output:
Average=3
Multi-Dimensional array
• Like we have 1 index in 1-D array, 2 index in 2-D array, we have n index
in n-dimensional array.
#include<stdio.h>
void main()
int a[2][2][2],i,j,k;
for(i=0;i<2;i++)
for(j=0;j<2;j++)
for(k=0;k<2;k++)
scanf("%d",&a[i][j][k]);
for(i=0;i<2;i++)
for(j=0;j<2;j++)
for(k=0;k<2;k++)
printf("a[%d][%d][%d]=%d\t",i,j,k,a[i][j][k]);
printf("\n");
Output:
a[0][0][0]=1 a[0][0][1]=2
a[0][1][0]=3 a[0][1][1]=4
a[1][0][0]=5 a[1][0][1]=6
a[1][1][0]=7 a[1][1][1]=8
Applications of array
• Storing and accessing data: Arrays are used to store and retrieve data
in a specific order. For example, an array can be used to store the scores
of a group of students, or the temperatures recorded by a weather
station.
• Stacks and queues: Arrays are used as the underlying data structure
for implementing stacks and queues, which are commonly used in
algorithms and data structures.
• Bubble sort
• Insertion sort
• Selection sort
Insertion sort
• The main idea behind insertion sort is that it inserts each item into its
proper place in the final list.
• It works as follows:
2. The sorting algorithm will proceed until there are elements in the
unsorted array.
3. Suppose there are n elements in the array. Initially the element with
index 0 is in the sorted set, rest is in the unsorted set.
4. The first element of the unsorted partition has array index 1. During
each iteration, the first element in the unsorted set is picked and
inserted in correct position.
Consider an array of integers given below. Sort the values in the array using
insertion sort.
39 9 45 63 18 81 108 54 72 36
39 9 45 63 18 81 108 54 72 36
Pass 1:
9 39 45 63 18 81 108 54 72 36
Pass 2:
9 39 45 63 18 81 108 54 72 36
Pass 3:
9 39 45 63 18 81 108 54 72 36
Pass 4:
9 18 39 45 63 81 108 54 72 36
Pass 5:
9 18 39 45 63 81 108 54 72 36
Pass 6:
9 18 39 45 63 81 108 54 72 36
Pass 7:
9 18 39 45 54 63 81 108 72 36
Pass 8:
9 18 39 45 54 63 72 81 108 36
Pass 9:
9 18 36 39 45 54 63 72 81 108
#include<stdio.h>
void main()
int a[10],i,n;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
ins_sort(a,n);
for(i=0;i<n;i++)
printf("%d\t",a[i]);
int i,j,temp;
for(i=0;i<n;i++)
temp=a[i];
j=i-1;
while((temp<a[j])&&(j>=0))
a[j+1]=a[j];
j--;
a[j+1]=temp;
Selection sort
• It is generally the preferred choice for sorting files with very large
objects.
Technique:
Consider array with N elements. The selection sort makes N-1 passes to sort
the entire array and works as follows. First find the smallest value in the array
and place it in the first position. Then find the second smallest value in the
array and place it in the second position. Repeat this procedure until the entire
array is sorted.
39 9 81 45 90 27 72 18
Pass POS A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7]
1 1 9 39 81 45 90 27 72 18
2 7 9 18 81 45 90 27 72 39
3 5 9 18 27 45 90 81 72 39
4 7 9 18 27 39 90 81 72 45
5 7 9 18 27 39 45 81 72 90
6 6 9 18 27 39 45 72 81 90
7 6 9 18 27 39 45 72 81 90
#include<stdio.h>
void main()
int a[10],i,n;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
sel_sort(a,n);
for(i=0;i<n;i++)
printf("%d\t",a[i]);
int pos=k,s=a[k],i;
for(i=k+1;i<n;i++)
if(a[i]<s)
s=a[i];
pos=i;
return pos;
int k,pos,temp;
for(k=0;k<n;k++)
pos=small(a,k,n);
temp=a[k];
a[k]=a[pos];
a[pos]=temp;
*****End*****