Week 7 Solutions
Week 7 Solutions
int main(){
int a[10][10],i,j,sum=0,m,n;
for(i=0;i<m;i++){
printf("\n");
for(j=0;j<n;j++){
printf("%d\t",a[i][j]);
}
}
for(i=0;i<m;i++){
for(j=0;j<n;j++){
if(i==j)
printf(“%d”,a[i][j]);
}
}
return 0;
}
2. Write a C Program to find transpose of a given matrix.
#include<stdio.h>
int main()
{
int a[10][10],i,j,m;
printf("Enter order of square matrix: ");
scanf("%d",&m);
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{
printf("Enter value of a[%d][%d]: ",i,j);
scanf("%d",&a[i][j]);
}
}
for(i=0;i<m;i++)
for(j=0;j<m;j++)
b[i][j]=a[j][i]
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{
printf("%d\t",b[i][j]);
}
printf(“\n”);
}
}
3. Write a C Program to find whether given matrix is symmetric or not.
#include<stdio.h>
int main()
{
int a[10][10],i,j,m;
printf("Enter order of square matrix: ");
scanf("%d",&m);
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{
printf("Enter value of a[%d][%d]: ",i,j);
scanf("%d",&a[i][j]);
}
}
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{
if(a[i][j]!=a[j][i])
{
printf("\n\nMatrix is not symmetric");
exit(0);
}
}
}
printf("\n\nMatrix is symmetric");
}
int main ()
{
int array[10][10];
int i, j, m, n, sum = 0;
printf("Enter the order of the matrix\n");
scanf("%d %d", &m, &n);
printf("Enter the elements of the matrix\n");
for (i = 0; i< m; ++i)
{
for (j = 0; j < n; ++j)
{
scanf("%d", &array[i][j]);
}
}
for (i = 0; i< m; ++i)
{
for (j = 0; j < n; ++j)
{
sum = sum + array[i][j] ;
}
printf("Sum of the %d row is = %d\n", i, sum);
sum = 0;
}
}
5. Write a C Program to calculate the sum of elements of each column in
given matrix using functions.
#include <stdio.h>
int main ()
{
int array[10][10];
inti, j, m, n, sum = 0;
sum = 0;
for (j = 0; j < n; ++j)
{
for (i = 0; i< m; ++i)
{
sum = sum + array[i][j];
}
printf("Sum of the %d column is = %d\n", j, sum);
sum = 0;
}
}
6. Write a C program to find sum of main and opposite diagonal elements of
a matrix using functions.
#include <stdio.h>
int main ()
{
int array[10][10];
inti, j, m, n, d1 = 0,d2 = 0;
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Enter array elements of B matrix\n");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
scanf("%d",&b[i][j]);
}
}
printf("MAtrix Multiplication\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{
c[i][j]=0;
for(k=0;k<c1;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
printf("%d\t",c[i][j]);
}
printf("\n");
}
}
return 0;
}
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Enter array elements of B matrix\n");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
scanf("%d",&b[i][j]);
}
}
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
printf("MAtrix addition\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
Printf(“%d\t”,c[i][j]);
}
Printf(“\n”);
return 0;
}
12. Write a C Program to check whether two given matrices are equal or not.
#include <stdio.h>
int main()
{
int A[10][10], B[10][10];
int i,j,m,n;
if(A[i][j] != B[i][j])
{
flag = 0;
break;
}
}
}
if(flag == 1)
{
printf("\nMatrix A is equal to Matrix B");
}
else
{
printf("\nMatrix A is not equal to Matrix B");
}
return 0;
}
if(A[i][j]!=1&& A[j][i]!=0)
{
flag=0;
break;
}
}
}
/* If it is an Identity matrix */
if(flag == 1)
{
printf("\nThe given matrix is an Identity Matrix.\n");
}
else
{
return 0;
}
14. Write a C program to find trace and normal of a given matrix.
#include <stdio.h>
#include <math.h>
int main ()
{
int array[10][10];
int i, j, m, n, sum = 0, sum1 = 0, normal;
}
}
for (i = 0; i< m; ++i)
{
for (j = 0; j < n; ++j)
{
Sum1=sum1+a[i][j];
}
}
normal = sqrt(sum1);
printf("The normal of the given matrix is = %d\n", normal);
for (i = 0; i< m; ++i)
{
sum = sum + array[i][i];
}
printf("Trace of the matrix is = %d\n", sum);
}
temp = A[i][i];
A[i][i] = A[i][m-i - 1];
A[i][m-i - 1] = temp;
}
printf("\n");
}
return 0;
}