C Record
C Record
5(c[0][0]) 9(𝑐[0][1])
C =[ ]
8(𝑐[1][0]) 9(𝑐[1][1])
Program:
#include<stdio.h>
int main()
{
int a[10][10],b[10][10],c[10][10],m,n,p,q,i,j;
printf("enter no.of rows and colms of matrix A:");
scanf("%d,%d",&m,&n);
printf("enter no.of rows and colms of matrix B:");
scanf("%d,%d",&p,&q);
if((m==p)&&(n==q))
{
printf("enter %d elements into A:",m*n);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("enter %d elements into B:",p*q);
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<n;j++)
{
c[i][j]=a[i][j]+b[i][j];
}printf("\n");
}
printf("elements of matrix C:\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d ",c[i][j]);
}printf("\n");
}
}else
{
printf("matrix addition not possible:");
}return 0;
}
Output:
1) enter no.of rows and colms of matrix A:2,2
enter no.of rows and colms of matrix B:3,3
matrix addition not possible
2) enter no.of rows and colms of matrix A:3,3
enter no.of rows and colms of matrix B:3,3
enter 9 elements into A:
123
456
789
enter 9 elements into B:
123
456
789
elements of matrix C:
246
8 10 12
14 16 18
WEEK 7:
#include<stdio.h>
int main()
int a[10][10],b[10][10],c[10][10],m,n,p,q,i,j,k;
scanf("%d,%d",&m,&n);
scanf("%d,%d",&p,&q);
if(n==p)
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<n;j++)
c[i][j]=0;
for(k=0;k<n;k++)
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}printf("\n");
for(i=0;i<m;i++)
for(j=0;j<q;j++)
printf("%d ",c[i][j]);
}printf("\n");
}else
{
}return 0;
Output:
243
432
24
33
42
elements of matrix C:
28 26
25 29
WEEK 7:
c. Transpose of a Matrix
AIM: To find the transpose of a Matrix.
DESCRIPTION:
The transpose of a matrix is a new matrix that is obtained by exchanging the rows and
columns.
Note: The transpose of an m × n matrix will result in an n × m matrix.
To transpose a matrix in C programming language, we can follow the following steps:
Declare the matrix: First, we need to declare a two-dimensional array to represent our
matrix
Initialize the matrix: Next, we need to initialize the matrix with some values. We can do this
using a nested for loop
Transpose the matrix: Once we have our matrix initialized, we can transpose it using another
nested for loop with the help of indices.
Display the transposed matrix
For example: if the matrix is
1 2
3 4
5 6
Then the resultant matrix will be
1 3 5
2 4 6
PROGRAM:
#include<stdio.h>
int main()
{
int a[100][100],b[100][100];
int c[100][100],i,j,m,n,t;
printf("Enter the rows of matrix A:");
scanf("%d",&m);
printf("Enter the columns of matrix A:");
scanf("%d",&n);
printf("Enter the %d elements of matrix A:\n",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++)
{
if(j==i)
{
c[i][j]=a[i][j];
}
if(j>i)
{
t=a[j][i];
c[j][i]=a[i][j];
c[i][j]=t;
}
}
}
printf("The resultant matrix is:\n");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
printf("%3d",c[i][j]);
}
printf("\n");
}
return 0;
}
OUTPUT:
Enter the rows of matrix A:3
Enter the columns of matrix A:2
Enter the 6 elements of matrix A:
12
34
56
The resultant matrix is:
1 3 5
2 4 6
WEEK 7:
D ) Trace of matrix
Description: Sum of diagnol elements of a square matrix is known as trace of matrix. Precondition is
it must be a square matrix.
1 2 3
Ex: A = [4 5 6]
7 8 9
Trace of matrix A is 1+5+9=15.
Program:
#include<stdio.h>
int main()
int a[10][10],m,n,i,j,s;
scanf("%d,%d",&m,&n);
if(m==n)
printf("enter %d elements\n",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 ",a[i][j]);
}
printf("\n");
s=0;
for(i=0;i<m;i++)
for(j=0;j<n;j++)
if(i==j)
s=s+a[i][j];
else
}return 0;
Output:
enter 9 elements
123456789
123
456
789
trace of matrix 15
WEEK 7:
In order to print lower triangle matrix we have to print the values upto j<=i.
(i) 0 1 2 3 (j)
0 00
1 10 11
2 20 21 22
3 30 31 32 33
Eg: 1
4 5
7 8 9
Program:
#include<stdio.h>
int main()
int a[10][10],m,n,i,j;
scanf("%d,%d",&m,&n);
if(m==n)
printf("enter %d elements\n",m*n);
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
}
printf("lower matrix elements are:\n");
for(i=0;i<m;i++)
for(j=0;j<=i;j++)
printf("%d ",a[i][j]);
printf("\n");
}else
}return 0;
Output:
enter 9 elements
123456789
45
789