Calculate the Sum of the Elements of each Row & Column
#include
#include
int Addrow(int array1[10][10], int k, int c);
int Addcol(int array1[10][10], int k, int r);
void main()
{
int arr[10][10];
int i, j, row, col, rowsum, colsum, sumall=0;
printf("Enter the order of the matrix \n");
scanf("%d %d", &row, &col);
printf("Enter the elements of the matrix \n");
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
scanf("%d", &arr[i][j]);
}
}
printf("Input matrix is \n");
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
printf("%3d", arr[i][j]);
}
printf("\n");
}
for (i = 0; i < row; i++)
{
rowsum = Addrow(arr, i, col);
printf("Sum of row %d = %d\n", i + 1, rowsum);
}
for (j = 0; j < col; j++)
{
colsum = Addcol(arr, j, row);
printf("Sum of column %d = %d\n", j + 1, colsum);
}
for (j = 0; j < row; j++)
{
sumall = sumall + Addrow(arr, j, col);
}
printf("Sum of all elements of matrix = %d\n", sumall);
}
int Addrow(int array1[10][10], int k, int c)
{
int rsum = 0, i;
for (i = 0; i < c; i++)
{
rsum = rsum + array1[k][i];
}
return(rsum);
}
int Addcol(int array1[10][10], int k, int r)
{
int csum = 0, j;
for (j = 0; j < r; j++)
{
csum = csum + array1[j][k];
}
return(csum);
getch();
}
Output
Enter the order of the matrix
3 3
Enter the elements of the matrix
2 3 4
7 1 5
3 8 9
Input matrix is
2 3 4
7 1 5
3 8 9
Sum of row 1 = 9
Sum of row 2 = 13
Sum of row 3 = 20
Sum of column 1 = 12
Sum of column 2 = 12
Sum of column 3 = 18
Sum of all elements of matrix = 42
Check if 2 Matrices are Equal
#include
#include
#include
void main()
{
int a[10][10], b[10][10];
int i, j, row1, column1, row2, column2, flag = 1;
printf("Enter the order of the matrix A \n");
scanf("%d %d", &row1, &column1);
printf("Enter the order of the matrix B \n");
scanf("%d %d", &row2, &column2);
printf("Enter the elements of matrix A \n");
for (i = 0; i < row1; i++)
{
for (j = 0; j < column1; j++)
{
scanf("%d", &a[i][j]);
}
}
printf("Enter the elements of matrix B \n");
for (i = 0; i < row2; i++)
{
for (j = 0; j < column2; j++)
{
scanf("%d", &b[i][j]);
}
}
printf("matrix a is \n");
for (i = 0; i < row1; i++)
{
for (j = 0; j < column1; j++)
{
printf("%3d", a[i][j]);
}
printf("\n");
}
printf("matrix b is \n");
for (i = 0; i < row2; i++)
{
for (j = 0; j < column2; j++)
{
printf("%3d", b[i][j]);
}
printf("\n");
}
if (row1 == row2 && column1 == column2)
{
printf("Matrices can be compared \n");
for (i = 0; i < row1; i++)
{
for (j = 0; j < column2; j++)
{
if (a[i][j] != b[i][j])
{
flag = 0;
break;
}
}
}
}
else
{
printf(" Cannot be compared\n");
exit(1);
}
if (flag == 1)
printf("Two matrices are equal \n");
else
printf("But, two matrices are not equal \n");
getch();
}
Output
enter the order of the matrix a
2 2
enter the order of the matrix b
2 2
enter the elements of matrix a
23 56
45 80
enter the elements of matrix b
50 26
39 78
matrix a is
23 56
45 80
matrix b is
50 26
39 78
matrices can be compared
but,two matrices are not equal
enter the order of the matrix a
2 2
enter the order of the matrix b
2 2
enter the elements of matrix a
10 50
15 30
enter the elements of matrix b
10 50
15 30
matrix a is
10 50
15 30
matrix b is
10 50
15 30
matrices can be compared
two matrices are equal
No comments:
Post a Comment