Rewrite The Matrix Program Using Pass-By-Reference:
Rewrite the matrix program using Pass-by-Reference:
#include <stdio.h>
#include <stdlib.h>
#include <cmath>
#include <string.h>
#include <math.h>
int main() {
int m, a, p, q, c, d, k, matrix, total = 0;
int first[3][3], second[3][3], multiply[3][3], addition[3][3], transpose[3][3];
printf(“Enter number of rows and columns of first matrix\n”);
scanf(“%d%d”, &m, &a);
printf(“Enter elements of first matrix\n”);
for (c = 0; c < m; c++)
for (d = 0; d < a; d++)
scanf(“%d”, &first[c][d]); //first matrix
printf(“Enter number of rows and columns of second matrix\n”);
scanf(“%d%d”, &p, &q);
printf(“Enter elements of second matrix\n”);
for (c = 0; c < p; c++)
for (d = 0; d < q; d++)
scanf(“%d”, &second[c][d]);
do {
printf(“\n1. Multiplication of the 2 Matrices\n”);
printf(“2. Addition of the 2 Matrices\n”);
printf(“3. Transposition of the 2 Matrices\n”);
printf(“4. Go back to Main Menu\n”);
printf(“Choose a number: \n”);
scanf(“%d”, &matrix);
if (matrix == 1) {
for (c = 0; c < m; c++) {
for (d = 0; d < q; d++) {
for (k = 0; k < p; k++) {
total = total + first[c][k]*second[k][d];
}
multiply[c][d] = total;
total = 0;
}
}
printf(“\nProduct of the matrices:\n”);
for (c = 0; c < m; c++) {
for (d = 0; d < q; d++)
printf(“%d\t”, multiply[c][d]);
printf(“\n”);
}
}//end of choice 1
else if (matrix == 2) {
printf(“Sum of entered matrices: \n”);
for (c = 0; c < m; c++) {
for (d = 0 ; d < a; d++) {
addition[c][d] = first[c][d] + second[c][d];
printf(“%d\t”, addition[c][d]);
}
printf(“\n”);
}
}//end of choice 2
else if (matrix == 3) {
for (c = 0; c < m; c++)
for( d = 0 ; d < a ; d++ )
transpose[d][c] = first[c][d];
for (c = 0; c < m; c++)
for( d = 0 ; d < a ; d++ )
transpose[d][c] = second[c][d];
printf(“Transpose of the matrix:\n”);
for (c = 0; c < a; c++) {
for (d = 0; d < m; d++)
printf(“%d\t”, transpose[c][d]);
printf(“\n”);
}
}//end of choice 3
else if (matrix == 4) {
//exit
}
else if (matrix >= 5) {
printf(“INVALID INPUT, please try again”);
}
}while(matrix!=4);
return 0;
}