0% found this document useful (0 votes)
73 views

Week 7 Solutions

The document provides C program code snippets for operations on 2D arrays/matrices, including: 1. Printing the diagonal elements of a matrix 2. Finding the transpose of a matrix 3. Checking if a matrix is symmetric 4. Calculating the sum of each row of a matrix 5. Calculating the sum of each column of a matrix 6. Finding the sum of main and opposite diagonal elements 7. Printing the lower triangular matrix 8. Printing the upper triangular matrix 9. Multiplying two matrices 10. Adding two matrices

Uploaded by

praneeth
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
73 views

Week 7 Solutions

The document provides C program code snippets for operations on 2D arrays/matrices, including: 1. Printing the diagonal elements of a matrix 2. Finding the transpose of a matrix 3. Checking if a matrix is symmetric 4. Calculating the sum of each row of a matrix 5. Calculating the sum of each column of a matrix 6. Finding the sum of main and opposite diagonal elements 7. Printing the lower triangular matrix 8. Printing the upper triangular matrix 9. Multiplying two matrices 10. Adding two matrices

Uploaded by

praneeth
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

WEEK-7

Programs on 2-D Arrays


1. Write a C Program to print diagonal elements of given matrix
#include<stdio.h>

int main(){

int a[10][10],i,j,sum=0,m,n;

printf("\nEnter the row and column of matrix: ");


scanf("%d %d",&m,&n);

printf("\nEnter the elements of matrix: ");


for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf("\nThe matrix is\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");
}

4. Write a C Program to calculate the sum of elements of each row in given


matrix using functions.
#include <stdio.h>

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;

printf("Enter the order of the matrix\n");


scanf("%d %d", &m, &n);
printf("Enter the array elements of the matrix\n");
for (i = 0; i< m; ++i)
{
for (j = 0; j < n; ++j)
{
scanf("%d", &array[i][j]);
}
}

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;

printf("Enetr the order of the matix \n");


scanf("%d %d", &m, &n);
if (m == n )
{
printf("Enter the co-efficients of the matrix\n");
for (i = 0; i< m; ++i)
{
for (j = 0; j < n; ++j)
{
scanf("%d", &array[i][j]);
}
}
printf("The given matrix is \n");
for (i = 0; i< m; ++i)
{
for (j = 0; j < n; ++j)
{
printf(" %d", array[i][j]);
}
printf("\n");
}
for (i = 0; i< m; ++i)
{
d1 =d1 + array[i][i];
d2= d2 + array[i][m - i - 1];
}
printf("\nThe sum of the main diagonal elements is = %d\n", d1);
printf("The sum of the off diagonal elemets is = %d\n", d2);
}
else
printf("The given order is not square matrix\n");
}
7. Write a C program to print lower triangular matrix
#include<stdio.h>
int main()
{
int a[3][3],i,j;

printf("Enter the 9 elements of matrix: ");


for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&a[i][j]);

printf("\nThe matrix is\n");


for(i=0;i<3;i++){
printf("\n");
for(j=0;j<3;j++)
printf("%d\t",a[i][j]);
}

printf("\nSetting zero in upper triangular matrix\n");


for(i=0;i<3;i++){
printf("\n");
for(j=0;j<3;j++)
if(i<=j)
printf("%d\t",a[i][j]);
else
printf("%d\t",0);
}
return 0;
}
8. Write a C program to print upper triangular matrix
#include<stdio.h>
int main()
{
int a[3][3],i,j;

printf("Enter the 9 elements of matrix: ");


for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&a[i][j]);

printf("\nThe matrix is\n");


for(i=0;i<3;i++){
printf("\n");
for(j=0;j<3;j++)
printf("%d\t",a[i][j]);
}

printf("\nSetting zero in upper triangular matrix\n");


for(i=0;i<3;i++){
printf("\n");
for(j=0;j<3;j++)
if(i>=j)
printf("%d\t",a[i][j]);
else
printf("%d\t",0);
}
return 0;
}
9. Write a C Program to find multiplication of two matrices.
#include<stdio.h>
int main()
{
int a[10][10],b[10][10],c[10][10];
int r1,c1,r2,c2,i,j,k;
printf("enter row and column of matrix A\n");
scanf("%d%d",&r1,&c1);
printf("enter row and column of matrix B\n");
scanf("%d%d",&r2,&c2);
if(c1!=r2)
{

printf("MAtrix Multiplication is not possible\n");


}
else
{
printf("Enter array elements of A matrix\n");

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;
}

10. Write a C Program to find addition of two matrices.


#include<stdio.h>
int main()
{
int a[10][10],b[10][10],c[10][10];
int r1,c1,r2,c2,i,j,k;
printf("enter row and column of matrix A\n");
scanf("%d%d",&r1,&c1);
printf("enter row and column of matrix B\n");
scanf("%d%d",&r2,&c2);
if(r1!=r2|| c1!=c2)
{

printf("MAtrix addition is not possible\n");


}
else
{
printf("Enter array elements of A matrix\n");

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;
}

11. Write a C Program to find subtraction of two matrices using functions.

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;

printf(“enter size of array”);


scanf(“%d%d”,&m,&n);
printf("Enter elements in matrix A \n");
for(i=0; i<m; i++)
{
for(j=0; j<n; j++)
{
scanf("%d", &A[i][j]);
}
}
printf("Enter elements in matrix B \n");
for(i=0; i<m; i++)
{
for(j=0; j<n; j++)
{
scanf("%d", &B[i][j]);
}
}

/* Assumes that the matrices are equal */


for(i=0; i<m; i++)
{
for(j=0; j<n; j++)
{

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;
}

13. Write a C Program to check whether given matrix is identity matrix or


not.
#include <stdio.h>
int main()
{
int A[50][50];
int i,j, flag=1;
scanf(“%d”,&n);
/* Input elements in matrix from user */
printf("Enter elements in matrix of size 3x3: \n");
for(i=0; i<n; i++)
{
for(j=0; j<n; j++)
{
scanf("%d", &A[i][j]);
}
}

/* Check whether it is Identity matrix or not */


for(i=0; i<n; i++)
{
for(j=0; j<n; j++)
{

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
{

printf("The given matrix is not Identity Matrix");


}

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;

printf("Enter the order of the matrix\n");


scanf("%d %d", &m, &n);
printf("Enter the n coefficients 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)
{
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);
}

15. Write a C program to interchange two diagonals of a given matrix.


#include <stdio.h>
int main()
{
int A[10][10];
int i,j m, temp;

printf("Enter elements in matrix of size “);


scanf(“%d”,&m);
for(i=0; i<m; i++)
{
for(j=0; j<m; j++)
{
scanf("%d", &A[i][j]);
}
}

for(i=0; i<m; i++)


{

temp = A[i][i];
A[i][i] = A[i][m-i - 1];
A[i][m-i - 1] = temp;
}

printf("\nMatrix after diagonals interchanged: \n");


for(i=0; i<m; i++)
{
for(j=0; j<n; j++)
{
printf("%d ", A[i][j]);
}

printf("\n");
}
return 0;
}

You might also like