//WAP to get sum of diagonal elements of an array of order 'mxn'.
#include<stdio.h>
#include<conio.h>
void main()
{
int matrics_1[100][100];
int i,j,sum=0;
int total_row,total_col;
printf("for sum, the matrix must be square\n");
printf("enter total number of rows(m)\n");
scanf("%d",&total_row); // for total number of rows of matrix
printf("enter total columns(n)\n");;
scanf("%d",&total_col); // for total number of columns of matrix
printf("enter elements of matrix\n");
for(i=0;i<total_row;i++)
{
for(j=0;j<total_col;j++) // nested loop for input of elements
{
scanf("%d",&matrics_1[i][j]); // getting inputs from user
}
}
printf("the matrix is\n"); // message display
for(i=0;i<total_row;i++)
{
for(j=0;j<total_col;j++)
{
printf("%d",matrics_1[i][j]); // display of matrix
}
printf("\n"); // display in next row
}
printf("------------------\n"); // for transposed matrix
for(i=0;i<total_row;i++)
{
for(j=0;j<total_col;j++)
{
if(i==j) // testing whether the locations are same or not
{
sum=sum+matrics_1[i][j];
} // display of matrix with trasnposed elemnts
}
printf("\n");
}
printf("sum=%d",sum);
getch();
}
------------------------------------------------------------------------------------------
using codeblocks.
-------------------------------------------------------------------------------------
//WAP to get sum of diagonal elements of an array of order 'mxn'.
#include<stdio.h>
int main()
{
int matrics_1[100][100];
int i,j,sum=0;
int total_row,total_col;
printf("for sum, the matrix must be square\n");
printf("enter total number of rows(m)\n");
scanf("%d",&total_row); // for total number of rows of matrix
printf("enter total columns(n)\n");;
scanf("%d",&total_col); // for total number of columns of matrix
printf("enter elements of matrix\n");
for(i=0;i<total_row;i++)
{
for(j=0;j<total_col;j++) // nested loop for input of elements
{
scanf("%d",&matrics_1[i][j]); // getting inputs from user
}
}
printf("the matrix is\n"); // message display
for(i=0;i<total_row;i++)
{
for(j=0;j<total_col;j++)
{
printf("%d",matrics_1[i][j]); // display of matrix
}
printf("\n"); // display in next row
}
printf("------------------\n"); // for transposed matrix
for(i=0;i<total_row;i++)
{
for(j=0;j<total_col;j++)
{
if(i==j) // testing whether the locations are same or not
{
sum=sum+matrics_1[i][j];
} // display of matrix with trasnposed elemnts
}
printf("\n");
}
printf("sum=%d",sum);
return 0;
}
#include<stdio.h>
#include<conio.h>
void main()
{
int matrics_1[100][100];
int i,j,sum=0;
int total_row,total_col;
printf("for sum, the matrix must be square\n");
printf("enter total number of rows(m)\n");
scanf("%d",&total_row); // for total number of rows of matrix
printf("enter total columns(n)\n");;
scanf("%d",&total_col); // for total number of columns of matrix
printf("enter elements of matrix\n");
for(i=0;i<total_row;i++)
{
for(j=0;j<total_col;j++) // nested loop for input of elements
{
scanf("%d",&matrics_1[i][j]); // getting inputs from user
}
}
printf("the matrix is\n"); // message display
for(i=0;i<total_row;i++)
{
for(j=0;j<total_col;j++)
{
printf("%d",matrics_1[i][j]); // display of matrix
}
printf("\n"); // display in next row
}
printf("------------------\n"); // for transposed matrix
for(i=0;i<total_row;i++)
{
for(j=0;j<total_col;j++)
{
if(i==j) // testing whether the locations are same or not
{
sum=sum+matrics_1[i][j];
} // display of matrix with trasnposed elemnts
}
printf("\n");
}
printf("sum=%d",sum);
getch();
}
------------------------------------------------------------------------------------------
using codeblocks.
-------------------------------------------------------------------------------------
//WAP to get sum of diagonal elements of an array of order 'mxn'.
#include<stdio.h>
int main()
{
int matrics_1[100][100];
int i,j,sum=0;
int total_row,total_col;
printf("for sum, the matrix must be square\n");
printf("enter total number of rows(m)\n");
scanf("%d",&total_row); // for total number of rows of matrix
printf("enter total columns(n)\n");;
scanf("%d",&total_col); // for total number of columns of matrix
printf("enter elements of matrix\n");
for(i=0;i<total_row;i++)
{
for(j=0;j<total_col;j++) // nested loop for input of elements
{
scanf("%d",&matrics_1[i][j]); // getting inputs from user
}
}
printf("the matrix is\n"); // message display
for(i=0;i<total_row;i++)
{
for(j=0;j<total_col;j++)
{
printf("%d",matrics_1[i][j]); // display of matrix
}
printf("\n"); // display in next row
}
printf("------------------\n"); // for transposed matrix
for(i=0;i<total_row;i++)
{
for(j=0;j<total_col;j++)
{
if(i==j) // testing whether the locations are same or not
{
sum=sum+matrics_1[i][j];
} // display of matrix with trasnposed elemnts
}
printf("\n");
}
printf("sum=%d",sum);
return 0;
}
No comments:
Post a Comment