Compute
the Product of Two Matrices
#include
#include
#define maxrows 10
#define maxcols 10
void readmatrix(int arr[][maxcols], int m, int n);
void printmatrix(int arr[][maxcols], int m, int n);
void productmatrix(int array1[][maxcols], int array2[][maxcols],
int array3[][maxcols], int m, int n);
void main()
{
int array1[maxrows][maxcols], array2[maxrows][maxcols],
array3[maxrows][maxcols];
int m, n;
printf("Enter the value of m and n \n");
scanf("%d %d", &m, &n);
printf("Enter Matrix array1 \n");
readMatrix(array1, m, n);
printf("Matrix array1 \n");
printMatrix(array1, m, n);
printf("Enter Matrix array2 \n");
readMatrix(array2, m, n);
printf("Matrix B \n");
printMatrix(array2, m, n);
productMatrix(array1, array2, array3, m, n);
printf("The product matrix is \n");
printMatrix(array3, m, n);
}
void readMatrix(int arr[][maxcols], int m, int n)
{
int i, j;
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
scanf("%d", &arr[i][j]);
}
}
}
void printMatrix(int arr[][maxcols], int m, int n)
{
int i, j;
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
printf("%3d", arr[i][j]);
}
printf("\n");
}
}
void productMatrix(int array1[][maxcols], int array2[][maxcols],
int array3[][maxcols], int m, int n)
{
int i, j, k;
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
array3[i][j] = 0;
for (k = 0; k < n; k++)
{
array3[i][j] = array3[i][j] + array1[i][k] *array2[k][j];
}
}
}
getch();
}
Output
Enter the value of m and n
3 3
Enter matrix array1
4 5 6
1 2 3
3 7 8
Matrix array1
4 5 6
2 3
7 8
Enter matrix array2
5 6 9
8 5 3
9 1
Matrix array2
6 9
8 5 3
2 9 1
The product matrix is
72103 57
27 43 18
87125 56
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