This code is asking the user to fill out to matrix and then it calls a void function to add them together. I have a http://www.ideone.com
I cannot change much of the code also. It is requeired to have all those define statements and a void function.
#include <stdio.h>
#include <math.h>
#define NCOL1 2
#define NCOL2 2
#define NROW1 2
#define NROW2 2
#define NCOL3 2
#define NROW3 2
int main (void)
{
//Initiate variables
double a, b;
int i, j;
void addarray(double a, double b);
double ans;
double arr1[NCOL1][NROW1], arr2[NCOL2][NROW1];
//Ask user to enter numbers for the first matrix
printf("Please enter numbers for Matrix 1 :\n ");
for (i = 0; i < NCOL1; i++) {
for (j = 0; j < NROW1; j++) {
scanf("%lf", &arr1[i][j]);
}
}
//Ask user to enter numbers for the second matrix
printf("Please enter numbers for Matrix 2 :\n ");
for (i = 0; i < NCOL2; i++) {
for (j = 0; j < NROW2; j++) {
scanf("%lf", &arr2[i][j]);
}
}
//Iterate through void function and print out result
for (i = 0; i < NCOL3; i++) {
for (j = 0; j < NROW3; j++) {
addarray(arr1[i][j], arr2[i][j]);
printf("%lf", ans);
}
}
return 0;
}
void addarray (double a, double b)
{
int i,j;
double arrsum[NCOL3][NROW3];
for (i = 0; i < NCOL3; i++) {
for (j = 0; j < NROW3; j++) {
arrsum[i][j] = a + b;
}
}
}
EDIT: A better solution to the problem is:
But, with minimal changes the following will also work.
It looks like addarray() is putting it’s results into a local matrix called arrsum[][]. For this program to work you will most likely want to make arrsum[][] available to the rest of the program ( although a global array is not a great idea ).
Without testing or compiling the code you should make these changes at a minimum:
1- Remove
double arrsum[NCOL3][NROW3];fromaddarray()2- Make
arrsum[][]globalI’ve gone and rewritten parts of the program, compiled and tested it. The new code is below.