0% found this document useful (0 votes)
47 views4 pages

Matrix Assignment

The document provides 5 solutions to C programming problems involving matrices: 1) A program to check if two matrices are equal by comparing all elements. 2) A program to check if a matrix is an identity matrix by checking diagonal and off-diagonal elements. 3) A program to find the sum of rows and columns of a matrix by iterating through elements. 4) A program to find the sum of the left diagonal of a matrix by adding elements where row and column indices are equal. 5) A program to print an upper triangular matrix by printing zero for elements where the row index is less than the column index.

Uploaded by

einsteinriyad223
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views4 pages

Matrix Assignment

The document provides 5 solutions to C programming problems involving matrices: 1) A program to check if two matrices are equal by comparing all elements. 2) A program to check if a matrix is an identity matrix by checking diagonal and off-diagonal elements. 3) A program to find the sum of rows and columns of a matrix by iterating through elements. 4) A program to find the sum of the left diagonal of a matrix by adding elements where row and column indices are equal. 5) A program to print an upper triangular matrix by printing zero for elements where the row index is less than the column index.

Uploaded by

einsteinriyad223
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

1. Write a program in C to accept two matrices and check whether they are equal.

Solution:

#include<stdio.h>
int main(){
int a[100][100],b[100][100],i,n,arow,acoloum,brow,bcoloum,flag;
printf("Enter Your Rows and coloum for first Matrix : ");
scanf("%d %d",&arow,&acoloum);
printf("\nEnter First Matrix Element : \n");
for(int i=0;i<arow;i++){
for(int j=0;j<acoloum;j++){
scanf("%d",&a[i][j]);
}

printf("\nEnter Your Rows and coloum for seconed Matrix : ");


scanf("%d %d",&brow,&bcoloum);
printf("\nEnter Seconed Matrix Element : \n");

for(int i=0;i<brow;i++){
for(int j=0;j<bcoloum;j++){
scanf("%d",&b[i][j]);
}
}

for(int i=0;i<arow;i++){
for(int j=0;j<bcoloum;j++){
if(a[i][j]!=b[i][j]){
flag=1;
}
else{
flag=0;
}
}

}
if(flag==1){
printf("Not Equal matrix ");
}
else{
printf("Equal matrix ");
}
return 0;
}

2. Write a program in C to check whether a given matrix is an identity matrix.


Solution :

#include<stdio.h>
int main(){
int a[100][100],i,j,arows,acoloum,flag=0,n;
printf("Input rows and coloum for this matrix : ");
scanf("%d %d",&arows,&acoloum);
printf("\nInput Matrix Element : \n");
for(int i=0;i<arows;i++){
for(int j=0;j<acoloum;j++){
scanf("%d",&a[i][j]);
}
}
for(int i=0;i<arows;i++){
for(int j=0;j<acoloum;j++){

if(i==j&&a[i][j]!=1){
flag=1;
break;
}
else if(i!=j&&a[i][j]!=0){
flag=1;
break;
}

}
if(flag = = 0){
printf("Identity Matrix");
}
else{
printf("Not Identity Matrix ");
}
return 0;
}

3. Write a program in C to find the sum of rows and columns of a matrix.


Solution:

#include<stdio.h>
int main(){
int a[100][100],i,j,arow,acoloum,sum=0;
printf("Input Your Rows and Coloumn : ");
scanf("%d %d",&arow,&acoloum);
printf("\nEnter Your Matrix Element : \n");
for(int i=0;i<arow;i++){
for(int j=0;j<acoloum;j++){
scanf("%d",&a[i][j]);
}
}
printf("Row Total : ");
for(int i=0;i<arow;i++){
for(int j=0;j<acoloum;j++){
sum +=a[i][j];
}
printf("%d ",sum);
sum=0;
}
printf("\nColoum Total : ");
for(int j=0;j<acoloum;j++){
for(int i=0;i<arow;i++){
sum+=a[i][j];
}
printf("%d ",sum);
sum=0;
}

4. Write a program in C to find the sum of the left diagonals of a matrix.


Solution :

#include<stdio.h>
int main(){
int a[100][100],i,j,arow,acoloum,sum=0;
printf("Input Your Matrix rows and coloum : ");
scanf("%d %d",&arow,&acoloum);
printf("\nEnter Your Matrix Element : \n");
for(int i=0;i<arow;i++){
for(int j=0;j<acoloum;j++){
scanf("%d",&a[i][j]);
}
}
printf("Entered Matrix : \n");
for(int i=0;i<arow;i++){
for(int j=0;j<acoloum;j++){
printf("%d ",a[i][j]);
}
printf("\n");
}
printf("\nSum Of the left diagonals Matrix : ");
for(int i=0;i<arow;i++){
for(int j=0;j<acoloum;j++){
if(i==j){
sum+=a[i][j];
}
}
}
printf("%d ",sum);
return 0;
}

5. Write a program in C to print or display an upper triangular matrix

Solution :

#include<stdio.h>
int main(){
int a[100][100],i,j,arow,acoloum;
printf("Enter Your Matrix Row and coloum : ");
scanf("%d %d",&arow,&acoloum);
printf("\nInput Your Matrix Element : \n");
for(int i=0;i<arow;i++){
for(int j=0;j<acoloum;j++){
scanf("%d",&a[i][j]);
}
}
printf("Upper Triangular Matrix : \n");
for(int i=0;i<arow;i++){
for(int j=0;j<acoloum;j++){
if(i>j){
printf("0 ",a[i][j]);
}
else{
printf("%d ",a[i][j]);
}
}
printf("\n");
}

return 0;
}

You might also like