Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a \"C\" program that calls a function to add two matrices (matrix = two di

ID: 3606991 • Letter: W

Question

Write a "C" program that calls a function to add two matrices (matrix = two dimensional array).

You decide on the size of the matrix and the elements of the matrices.

The main program should create and populate the matrices with non-zero values. The function should perform a matrix addition and assign the result to another array. The function should be flexible enough to work with any array size and the sizes communicated to the function from the main function.

After the function call, the main program should print the array with the initial values followed by the result of the addition as shown below.

Initial values

3 1 2

7 8 9

6 5 1

3 4 5

Result

9   6   3

10 12 14

Use as many variables and arrays as needed.

Provide restriction so that the original array passed to it cannot be modified by the function.

Print using %2d to streamline the output

Explanation / Answer

Please find the C program to calculate the sum of 2 matrices below:

#include <stdio.h>

void sum(int X[10][10],int Y[10][10],int Z[10][10], int m,int n)
{
for (int c = 0; c < m; c++) {
for (int d = 0 ; d < n; d++) {
Z[c][d] = X[c][d] + Y[c][d];
}
}
  
}

int main()
{
  
//Declare matrices and variables for size;
int m, n;
int X[10][10]={{3, 1, 2},{7, 8, 9 }} , Y[10][10]={{6, 5, 1},{3, 4, 5 }}, Z[10][10];
m=2; //No. of rows
n=3; //No. of columns

printf("Initial Values: ");
for (int c = 0; c < m; c++) {
for (int d = 0 ; d < n; d++) {
printf("%2d ", X[c][d]);
}
printf(" ");
}

printf(" ");

for (int c = 0; c < m; c++) {
for (int d = 0 ; d < n; d++) {
printf("%2d ", Y[c][d]);
}
printf(" ");
}
  
//Call sum function to calculate sum
sum(X,Y,Z,m,n);
  
printf(" Result: ");

for (int c = 0; c < m; c++) {
for (int d = 0 ; d < n; d++) {
printf("%2d ", Z[c][d]);
}
printf(" ");
}

return 0;
}

Output:

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote