Specification14:
Accept "n" elements into an array and find whether
an element is existed or not. (Searching)
Logic:
We will store 0 into "flag"
We will traverse element by element in searching for the
required element (x).
If the required element is existed then 1 is stored into "flag".
At the end of loop, if the value of "flag" is 1
then the required element is existed otherwise not existed.
flag=0; for(i=0;i<n;i++) if(a[i]==x) { flag=1; break; }
Program:
#include<stdio.h>
void main()
{
int n,i,x,flag,a[50];
printf("How many elements?");
scanf("%d",&n);
printf("Enter %d elements:\n",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Find what?");
scanf("%d",&x);
flag=0;
for(i=0;i<n;i++)
if(a[i]==x)
{
flag=1;
break;
}
if(flag)
printf("Existed");
else
printf("Not
existed");
}
Execution1:
How many elements? 6
Enter 6 elements:
45 56 78
45 34 23
Find what? 78
Existed
Execution2:
How many elements? 6
Enter 6 elements:
45 56 78
45 34 23
Find what? 17
Not existed
Specification15:
Accept "n" elements into an array, find an element
and replace it with another element and print the resultant array.
Logic:
We will traverse element by element in searching for the
required element (x).
If the required element is existed then we will overwrite it
with new element (a[i]=y;).
Finally we will print the total array as output.
for(i=0;i<n;i++) if(a[i]==x) a[i]=y;
Program:
#include<stdio.h>
void main()
{
int n,i,x,y,a[50];
printf("How many elements?");
scanf("%d",&n);
printf("Enter %d elements:\n",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Find what?");
scanf("%d",&x);
printf("Replace with what?");
scanf("%d",&y);
for(i=0;i<n;i++)
{
if(a[i]==x)
a[i]=y;
}
printf("The resultant array:\n");
for(i=0;i<ni++)
printf("%5d",a[i]);
}
Execution:
How many elements? 6
Enter 6 elements:
21 56 34
23 56 32
Find what? 56
Replace with what? 66
The resultant array:
21 66 34
23 66 32
Specification16:
Accept "n" elements into one array, "m"
elements into another array, concatenate the elements of second array to the
first array and print the resultant array (Concatenating two vectors).
Logic:
Program:
#include<stdio.h>
void main()
{
int i,j,n,m,a[50],b[50];
printf("How many elements into 1st vector:");
scanf("%d",&n);
printf("How many elements into 2nd vetor:");
scanf("%d",&m);
printf("Enter %d elements:\n",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Enter %d elements:\n",m);
for(i=0;i<m;i++)
scanf("%d",&b[i]);
for(i=n,j=0;j<m;i++,j++)
a[i]=b[j];
printf("The resultant array:\n");
for(i=0;i<n+m-1;i++)
printf("%5d",a[i]);
}
Execution:
How many elements into 1st vector: 5
How many elements into 2nd vector: 5
Enter 5 elements:
5 9 6
4 7
Enter 5 elements:
8 3 2
1 5
The resultant array:
5 9 6
4 7 8 3 2
1 5
Specification17:
Accept two vectors of same size, store addition of them into
another vector and print the sum. (Addition of two vectors)
Logic:
We can’t directly add any two vectors.
We need to add individual elements and store into another
vector. (c[i]=a[i]+b[i])
Program:
#include<stdio.h>
void main()
{
int i,n,a[50],b[50],c[50];
printf("Enter the size of vector:");
scanf("%d",&n);
printf("Enter %d elements into 1st vector:\n",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Enter %d elements into 2st vector:\n",n);
for(i=0;i<n;i++)
scanf("%d",&b[i]);
for(i=0;i<n;i++)
c[i]=a[i]+b[i];
printf("Sum of two vectors:\n");
for(i=0;i<n;i++)
printf("%5d",c[i]);
}
Execution:
Enter the size of vector: 6
Enter 6 elements into 1st vector:
12 23 10
8 16 22
Enter 6 elements into 2nd vector:
7 8 19
24 15 20
Sum of two vectors:
19 31 29
32 31 42
Specification18:
Accept "n" elements into an array, sort the
elements in ascending order and print the resultant array.
Logic:
- Select
element by element as key element.
- From
the next element to the key element compare the elements with the key
element.
- If
element is less than the key element then interchange with the key element
and continue until the end of array.
- Continue
the same procedure until the end of array.
Example:
Program:
#include<stdio.h>
void main()
{
int n,i,k,temp,a[50];
printf("How many elements?");
scanf("%d",&n);
printf("Enter %d elements:\n",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(k=0;k<n-1;k++)
for(i=k+1;i<n;i++)
if(a[i]<a[k])
{
temp=a[i];
a[i]=a[k];
a[k]=temp;
}
printf("Elements of array in ascending order:\n");
for(i=0;i<n;i++)
printf("%5d",a[i]);
}
Execution:
How many elements? 6
Enter 6 elements:
34 56 12
45 18 23
Elements of array in ascending order:
34 56 12
45 18 23
Specification19:
Accept "n" elements into an array, arrange them in
descending order and print the resultant array.
Program:
#include<stdio.h>
void main()
{
int n,i,k,temp,a[50];
printf("How many elements?");
scanf("%d",&n);
printf("Enter %d elements:\n",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(k=0;k<n-1;k++)
for(i=k+1;i<n;i++)
if(a[i]>a[k])
{
temp=a[i];
a[i]=a[k];
a[k]=temp;
}
printf("Elements of array in descending order:\n");
for(i=0;i<n;i++)
printf("%5d",a[i]);
}
Execution:
Execution:
How many elements? 6
Enter 6 elements:
34 56 12
45 18 23
Elements of array in ascending order:
56 45 34 23 18
12
Specification20:
Accept "n" elements into two arrays and print
whether they are equal or not.
Logic:
The flag is stored with 1 by default.
We will check for inequality by checking element by element
in both the arrays.
If inequality is found then 0 is stored in flag.
At the end of loop if flag has 1 then both the arrays are
equal otherwise not equal.
Program:
#include<stdio.h>
void main()
{
int i,n,flag,a[50],b[50];
printf("How many elements?");
scanf("%d",&n);
printf("Enter %d elements into 1st array:\n",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Enter %d elements into 2nd array:\n",n);
for(i=0;i<n;i++)
scanf("%d",&b[i]);
flag=1;
for(i=0;i<n;i++)
if(a[i]!=b[i])
{
flag=0;
break;
}
if(flag)
printf("Equals");
else
printf("Not
equal");
}
Execution:
How many elements? 5
Enter 5 elements into 1st array:
23 56 89
67 56
Enter 5 elements into 2nd array
23 56 89
67 56
Equals
Execution:
How many elements? 5
Enter 5 elements into 1st array:
23 56 89
67 56
Enter 5 elements into 2nd array
23 56 85
67 56
Not
equal
Two dimensional array (Matrix):
Set of data items of similar kind stored in contiguous memory
allocations is called a single dimensional array.
Set of single dimensional arrays of same size and type
stored in contiguous memory allocations is called a two dimensional array.
Set of sets of similar kind of data is called a two
dimensional array.
To improve the readability a two dimensional array is also
defined as similar kind of data items stored in a tabular format that is in rows
and columns. So it is also called matrix.
Every element in a matrix is identified with its row index
and column index which always start from 0.
Every element in a matrix is free to participate in
arithmetic, relational and logical operations.
Initializing while declaration of a matrix:
Assigning a set of sets of elements while declaration of a matrix
is called initializing while declaration.
Unlike single dimensional array it is mandatory to specify the
dimension.
Example:
#include<stdio.h>
void main()
{
int a[3][4]={{12,34,56,54},{21,55,78,76},{44,23,41,78}};
int i,j;
printf("Elements
of matrix:\n");
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
printf("%5d",a[i][j]);
printf("\n");
}
}
Output:
12 34 56
54
21 55 78
76
44 23 41
78
Example explained:
Inner loop executes completely once for every outer loop
iteration
When "i" is 0 then inner loop traverse the first
row a[0][j] ( a[0][0], a[0][1], a[0][2], a[0][3] )
When "i" is 1 then inner loop traverse the second
row a[1][j] (a[1][0], a[1][1], a[1][2], a[1][3] )
When "i" is 2 then inner loop traverse the second
row a[2][j] (a[2][0], a[2][1], a[2][2], a[2][3] )
Loop to
traverse matrix: Outer loop
is used to select rows. Inner loop
is used to traverse the selected row. Outer loop
must run for rows number of times that is 0 to rows-1 Inner loop
must run for columns number of times that is 0 to cols-1 for(i=0;i<number of rows;i++) {
for(j=0;j<number of
columns;j++)
---------------------a[i][j]---- Statements }
Specification1:
Accept a nxm matrix and print the matrix as it is.
Program:
#include<stdio.h>
void main()
{
int a[50][50],n,m,i,j;
printf("Enter
the class of matrix:\n");
scanf("%d%d",&n,&m);
printf("Enter %dx%d matrix:\n",n,m);
for(i=0;i<n;i++)
for(j=0;j<m;j++)
scanf("%d",&a[i][j]);
printf("The given matrix:\n");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
printf("%5d",a[i][j]);
printf("\n");
}
}
Execution:
Enter the class of matrix:
3 4
Enter 3x4 matrix:
12 45 67
88
32 56 44
23
78 56 33
89
The given matrix:
12 45 67
88
32 56 44
23
78 56 33
89
Specification2:
Accept a nxm matrix and print the mean of matrix.
Logic: average of all the elements in a matrix is called
mean.
Program:
#include<stdio.h>
void main()
{
int a[50][50],n,m,i,j,sum;
float mean;
printf("Enter the class of matrix:\n");
scanf("%d%d",&n,&m);
printf("Enter %dx%d matrix:\n",n,m);
for(i=0;i<n;i++)
for(j=0;j<m;j++)
scanf("%d",&a[i][j]);
for(sum=0,i=0;i<n;i++)
for(j=0;j<m;j++)
sum=sum+a[i][j];
mean=(float)sum/(n*m);
printf("Mean of matrix %f",mean);
}
Execution:
Enter the class of matrix:
3 4
Enter 3x4 matrix:
12 45 67
88
32 56 44
23
78 56 33
89
Mean of matrix: 51.916667
Specification3:
Accept a nxm matrix and print the sum of each row.
Program:
#include<stdio.h>
void main()
{
int
a[50][50],n,m,i,j,sum;
printf("Enter the class of matrix:\n");
scanf("%d%d",&n,&m);
printf("Enter %dx%d matrix:\n",n,m);
for(i=0;i<n;i++)
for(j=0;j<m;j++)
scanf("%d",&a[i][j]);
printf("Sum of individual rows:\n");
for(i=0;i<n;i++)
{
for(sum=0,j=0;j<m;j++)
sum=sum+a[i][j];
printf("\n%d",sum);
}
}
Execution:
Enter the class of matrix:
3 4
Enter 3x4 matrix:
12 45 67
88
32 56 44
23
78 56 33
89
Sum of individual rows:
212
155
256
Specification4:
Accept a nxm matrix and print its transpose that is rows as
columns and columns as rows.
Logic:
Here we use the outer loop to select the column and inner
loop to traverse the selected column.
When "i" is 0 then inner loop traverse the first
column a[j][0] ( a[0][0], a[1][0], a[2][0])
When "i" is 1 then inner loop traverse the second
column a[j][1] ( a[0][1], a[1][1], a[2][1])
When "i" is 2 then inner loop traverse the third
column a[j][2] ( a[0][2], a[1][2], a[2][2])
When "i" is 3 then inner loop traverse the fourth
column a[j][3] ( a[0][3], a[1][3], a[2][3])
Program:
#include<stdio.h>
void main()
{
int n,m,i,j,a[50][50];
printf("Enter the class of matrix:\n");
scanf("%d%d",&n,&m);
printf("Enter %dx%d matrix:\n",n,m);
for(i=0;i<n;i++)
for(j=0;j<m;j++)
scanf("%d",&a[i][j]);
printf("The transpose of matrix:\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
printf("%5d",a[j][i]);
printf("\n");
}
}
Execution:
Enter the class of matrix:
3 4
Enter 3x4 matrix:
12 45 67
88
32 56 44
23
78 56 33
89
The transpose of matrix:
12 32 78
45 56 56
67 44 33
88 23 89
Specification5:
Accept a nxm matrix, store the sum of rows and columns in
the same matrix and print the resultant matrix.
Logic:
Rows sum is stored in a[i][m].
Columns sum is stored in a[n][i].
Sum of rows sum and columns sum is called grad sum which is
stored in a[n][m]
Program:
#include<stdio.h>
void main()
{
int n,m,i,j,sum,gsum,a[50][50];
printf("Enter the class of matrix:\n");
scanf("%d%d",&n,&m);
printf("Enter %dx%d matrix:\n",n,m);
for(i=0;i<n;i++)
for(j=0;j<m;j++)
scanf("%d",&a[i][j]);
gsum=0;
for(i=0;i<n;i++)
{
for(sum=0,j=0,j<m;j++)
sum=sum+a[i][j]; /* calculating sum of rows */
a[i][m]=sum; /* storing sum of rows
into a[i][m] */
gsum=gsum+sum; /*
Adding rows sum to grand sum */
}
for(i=0;i<m;i++)
{
for(sum=0,j=0,j<n;j++)
sum=sum+a[j][i]; /*
calculating sum of columns */
a[n][i]=sum; /* storing sum of columns
into a[n][i] */
gsum=gsum+sum; /*
Adding columns sum to grand sum */
}
a[n][m]=gsum; /* storing grand sum into
a[n][m] */
printf("The resultant matrix:\n");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
printf("%5d",a[i][j]);
printf("\n");
}
}
Execution:
Enter the class of matrix:
3 4
Enter 3x4 matrix:
5 7 3 2
8 9 0 12
6 4 3 2
The resultant matrix:
5 7 3 2 17
8 9 0 12 29
6 4 3
2 15
19 20 6
16 122
Specification6:
Accept a nxm matrix and print its norm.
Logic:
Square root of sum of squares of all the elements is called
the norm of matrix.
Program:
#include<stdio.h>
#include<math.h>
void main()
{
int a[50][50],n,m,i,j;
long int sum;
double norm;
printf("Enter the class of matrix:\n");
scanf("%d%d",&n,&m);
printf("Enter %dx%d matrix:\n",n,m);
for(i=0;i<n;i++)
for(j=0;j<m;j++)
scanf("%d",&a[i][j]);
for(sum=0,i=0;i<n;i++)
for(j=0;j<m;j++)
sum=sum+a[i][j]*a[i][j];
norm=sqrt(sum);
printf("The norm of matrix %lf",norm);
}
Execution:
Enter the class of matrix:
3 4
Enter 3x4 matrix:
4 5 6 3
1 2 4 5
5 3 2 1
The norm of matrix: 13.076697
Specification7:
Accept a square matrix and print all the diagonal elements
and trace.
Logic: Sum of diagonal elements is called trace.
Program:
#include<stdio.h>
void main()
{
int a[50][50],n,i,j,sum;
printf("Enter the class of square matrix:\n");
scanf("%d",&n);
printf("Enter %dx%d matrix:\n",n,n);
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf("Diagonal elements are:\n");
for(sum=0,i=0;i<n;i++)
{
printf("%5d",a[i][i]);
sum=sum+a[i][i];
}
printf("\nTrace of matrix: %d",sum);
}
Enter the class of square matrix:
4
Enter 4x4 matrix:
12 15 16
20
4 10 2
21
33 14 23 9
22 43 54 6
Diagonal elements are:
12 10 23 6
Trace of matrix: 51
Specification8:
Accept a square matrix and print the revere diagonal
elements.
n-1
Logic:
n-1
Program:
#include<stdio.h>
void main()
{
int a[50][50],n,i,j,sum;
printf("Enter the class of square matrix:\n");
scanf("%d",&n);
printf("Enter %dx%d matrix:\n",n,n);
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf("The reverse diagonal elements are:\n");
for(i=0,j=n-1;i<n;i++,j--)
printf("%5d",a[i][j]);
}
Execution:
Enter the class of square matrix:
4
Enter 4x4 matrix:
12 15 16
20
4 10 2
21
33 14 23 9
22 43 54 6
The reverse diagonal elements are:
20 2 14
22
Specification9:
Accept a square matrix and print lower diagonal elements.
Logic:
.
Program:
#include<stdio.h>
void main()
{
int a[50][50],n,i,j,sum;
printf("Enter the class of square matrix:\n");
scanf("%d",&n);
printf("Enter %dx%d matrix:\n",n,n);
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf("The lower diagonal elements are:\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
if(i>j)
printf("%5d",a[i][j]);
printf("\n");
}
}
Execution:
Enter the class of square matrix:
4
Enter 4x4 matrix:
12 15 16
20
4 10 2
21
33 14 23 9
22 43 54 6
The lower diagonal elements are:
4
33 14
22 43 54
Specification10:
Accept a square matrix and print the upper diagonal
elements.
a[i][j] ------- a[0][1] 15 a[0][2] 16 a[0][3] 20 a[1][2] 2 a[1][3] 21 a[2][3] 9 for(i=0;i<n;i++) { for(j=0;j<m;j++) if(i<j) printf(“%5d”,a[i][j]); printf(“\n”); }
#include<stdio.h>
void main()
{
int a[50][50],n,i,j,sum;
printf("Enter the class of square matrix:\n");
scanf("%d",&n);
printf("Enter %dx%d matrix:\n",n,n);
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf("The upper diagonal elements are:\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
if(i<j)
printf("%5d",a[i][j]);
else
printf(" "); /* 5
spaces */
printf("\n");
}
}
Execution:
Enter the class of square matrix:
4
Enter 4x4 matrix:
12 15 16
20
4 10 2
21
33 14 23 9
22 43 54 6
The upper diagonal elements are:
15 16
20
2 21
9
Specification11:
Accept a nxm matrix, copy the transpose of matrix on to
another matrix and print the resultant matrix.
Logic:
Here outer loop ("i") is used to select rows of
first matrix and columns of second matrix.
Inner loop ("j") is used to copy the columns of
first matrix into rows of second matrix.
When "i" is 0 then first row of first matrix is
copied onto first column of second matrix (b[j][0]=a[0][j]).
When "i" is 1 then second row of first matrix is
copied onto second column of second matrix (b[j][1]=a[1][j]).
When "i" is 2 then third row of first matrix is
copied onto third column of second matrix (b[j][2]=a[2][j]).
When "i" is 3 then fourth row of first matrix is
copied onto fourth column of second matrix (b[j][3]=a[3][j]).
Program:
#include<stdio.h>
void main()
{
int i,j,n,m,a[50][50],b[50][50];
printf("Enter the class of matrix:\n");
scanf("%d%d",&n,&m);
printf("Enter %dx%d matrix:\n",n,m);
for(i=0;i<n;i++)
for(j=0;j<m;j++)
scanf("%d",&a[i][j]);
for(i=0;i<n;i++)
for(j=0;j<m;j++)
b[j][i]=a[i][j];
printf("Transpose of matrix:\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
printf("%5d",b[i][j]);
printf("\n");
}
}
Execution:
Enter the class of matrix:
4 5
Enter 4x5 matrix:
5 8 3 11 7
10 9 13 19 20
6 4 3
12 18
16 25 52
34 43
Transpose of matrix:
5 10 6 16
8 9 4 25
3 13 3
52
11 19 12
34
7 20
18 43
Speciication12:
Accept two nxm matrices and print the sum of them by storing
onto another matrix.
Logic:
c[i][j]=a[i][j]+b[i][j];
Program:
#include<stdio.h>
void main()
{
int i,j,n,m,a[50][50],b[50][50],c[50][50];
printf("Enter the class of matrix:\n");
scanf("%d%d",&n,&m);
printf("Enter the first matrix of %dx%d:\n",n,m);
for(i=0;i<n;i++)
for(j=0;j<m;j++)
scanf("%d",&a[i][j]);
printf("Enter the second matrix of %dx%d:\n",n,m);
for(i=0;i<n;i++)
for(j=0;j<m;j++)
scanf("%d",&b[i][j]);
for(i=0;i<n;i++)
for(j=0;j<m;j++)
c[i][j]=a[i][j]+b[i][j];
printf("The sum of two matrices:\n");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
printf("%5d",c[i][j]);
printf("\n");
}
}
Execution:
Enter the class of matrix:
3 4
Enter the first matrix of
3x4:
4 5 7
8
2 1 6
1
6 9 2
6
Enter the second matrix of 3x4:
10 34 4
5
12 54 15 7
10 9 3 2
The sum of two
matrices:
14 39 11
13
14 55 21
8
16 18 5
8
Specification13:
Accept a nxm matrix, sort every row in ascending order and
print the resultant matrix.
Logic:
#include<stdio.h>
void main()
{
int a[50][50],n,m,i,j,k,temp;
printf("Enter the class of matrix:\n");
scanf("%d%d",&n,&m);
printf("Enter %dx%d matrix:\n",n,m);
for(i=0;i<n;i++)
for(j=0;j<m;j++)
scanf("%d",&a[i][j]);
for(i=0;i<n;i++)
for(k=0;k<m-1;k++)
for(j=k+1;j<m;j++)
if(a[i][j]<a[i][k])
{
temp=a[i][k];
a[i][k]=a[i][j];
a[i][j]=temp;
}
printf("The resultant matrix:\n");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
printf("%5d",a[i][j]);
printf("\n");
}
}
Execution:
Enter the class of matrix:
3 4
Enter 3x4 matrix:
12 45 17
88
32 56 44
23
78 56 33
89
The resultant matrix:
12 17 45
88
23 32 44
56
33 56 78
89
Specification14:
Accept two matrices and print the product of them (matrix
multiplication).
Logic:
To multiply two matrices, columns of the first matrix must
be equal to the rows of second matrix.
Variable "i" is used to select rows of first
matrix (i=0;i<n;i++)
Variable "j" is used to select columns of second
matrix (j=0;j<q;j++)
Variable "k" is used to multiply and add the
elements of selected row of first matrix and column of second matrix
(k=0;k<m or p;k++)
We will store the sum of products of elements of selected
row and column into other matrix at "i" row and "j" column.
4
`
Program:
#include<stdio.h>
void main()
{
int i,j,k,sum,n,m,p,q,a[50][50],b[50][50],c[50][50];
printf("Enter the class of first matrix:\n");
scanf("%d%d",&n,&m);
printf("Enter the class of second matrix:\n");
scanf("%d%d",&p,&q);
if(m!=p)
printf("Cannot
multiply");
else
{
printf("Enter
%dx%d matrix:\n",n,m);
for(i=0;i<n;i++)
for(j=0;j<m;j++)
scanf("%d",&a[i][j]);
printf("Enter
%dx%d matrix:\n",p,q);
for(i=0;i<p;i++)
for(j=0;j<q;j++)
scanf("%d",&b[i][j]);
for(i=0;i<n;i++)
for(j=0;j<q;j++)
{
for(sum=0,k=0;k<m;k++)
sum=sum+a[i][k]*b[k][j];
c[i][j]=sum;
}
printf("Product
of two matrices:\n");
for(i=0;i<n;i++)
{
for(j=0;j<q;j++)
printf("%5d",c[i][j]);
printf("\n");
}
}/* end of else*/
}
Execution1:
Enter the class of first matrix:
3 4
Enter the class of second matrix:
5 3
Cannot multiply
Execution2:
Enter the class of first matrix:
3 4
Enter the class of second matrix:
4 4
Enter 3x4 matrix:
1 2 3 5
2 1 3 4
1 4 3 2
Enter 4x4 matrix:
3 4 5
6
1 2 3
4
2 1 3 1
2 3 5
3
Product of two matrices:
23 27 48
33
21 25 42
31
17 21 36
31
Multi dimensional arrays:
Set of data items of the similar type stored in contagious memory
allocations is called a single dimensional array or vector.
Set of single dimensional arrays of the size and type is
called a two dimensional array or matrix.
An array with more than two dimensions is called a
multidimensional array.
A maximum dimension allowed by the C language is compiler
dependent.
Performance reduces as the number of dimensions increases
due to the complex memory calculations.
Wastage of memory increases as the number of dimensions
increases.
It is suggested to use either single or two dimensional
arrays for better performance and effective utilization of memory.
Syntax: <data
type> <name>[dim 1][dim 2]………..[dim n] Example: int
a[2][3][4];
Program:
#include<stdio.h>
void main()
{
int i,j,k;
int a[2][3][4]={{{6,4,8,9},{12,45,22,11},{76,34,33,44}},
{{9,8,12,23},{15,65,9,10},{11,22,88,17}}};
printf("Elements of array are:\n");
for(k=0;k<2;k++)
{
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
printf("%5d",a[k][i][j]);
printf("\n");
}
printf("\n\n");
}
}
Output:
6 4 8
9
12 45 22
11
76 34 33
44
9 8 12
23
15 65 9
10
11 22 88
17
0 comments:
Post a Comment