C programming Input code in "Enter code here" section
C
Input code in "Enter code here" section
Approach
Function matrix_printer
void matrix_printer(int matrixA[][4], int matrixB[][4]) {
printf("MatrixA \t MatrixB\n");
for (int c = 0; c < 4; c++) {
for (int d = 0 ; d < 4; d++) {
//print matrixA elements
printf("%d ", matrixA[c][d]);
}
printf("\t");
for (int e = 0 ; e < 4; e++) {
//print matrixB elements
printf("%d ", matrixB[c][e]);
}
printf("\n");
}
printf("\n");
}
Function matrix_operation
void matrix_operation(int matrixA[][4], int matrixB[][4], char operator) {
printf("\nResult\n");
int matrixC[4][4];
int c,d;
if (operator == '+') //check if operator is '+'
{
for (c = 0; c < 4; c++) {
for (d = 0 ; d < 4; d++) {
matrixC[c][d] = matrixA[c][d] + matrixB[c][d]; //perform addition
printf("%d ", matrixC[c][d]); //print result
}
printf("\n");
}
}
if (operator == '-') //check if operator is '-'
{
for (c = 0; c < 4; c++) {
for (d = 0 ; d < 4; d++) {
matrixC[c][d] = matrixA[c][d] - matrixB[c][d]; //perform subtraction
printf("%d ", matrixC[c][d]); //print result
}
printf("\n");
}
}
}
Step by step
Solved in 2 steps with 2 images