Reputation: 56
I am trying to make a program that gets the input of two matrices from the user. Then pass the inputted data to another function to make the calculation after that. The sum of the two matrices is passed to a third function to print the output.
The problem I am facing is passing the arrays between functions, otherwise the logic of the code is fine.
The problem with my code is that I don't get an output for the print function. As well the add function does not do any calculations since I think the data from the arrays are not passed.
Could someone please explain to me what am I doing wrong when passing.
const int MTRX_MAX = 3;
//Function Prototypes
void Get_Matrix(); //Ask the user for input
void Add_Matrix(int matrix1[][MTRX_MAX], int matrix2[][MTRX_MAX], int sum[][MTRX_MAX]); //Calculates the sum of the matrecies
void Print_Matrix(int matrix1 [][MTRX_MAX], int matrix2 [][MTRX_MAX]); //Printout the inputed data
int main()
{
int matrix1[MTRX_MAX][MTRX_MAX];
int matrix2[MTRX_MAX][MTRX_MAX];
int sum[MTRX_MAX][MTRX_MAX];
Get_Matrix();
Add_Matrix(matrix1,matrix2,sum);
Print_Matrix(matrix1, matrix2,sum);
return 0;
}
//Function Definetions
void Get_Matrix()
{
int matrix1[3][3] = { 0 };
int matrix2[3][3] = { 0 };
cout << "Enter the first Matrix: \n";
for (int i = 0; i < MTRX_MAX; i++)
{
for (int j = 0; j < MTRX_MAX; j++)
{
cin >> matrix1[i][j];
}
}
////////////////
cout << "Enter the second Matrix: \n";
for (int i = 0; i < MTRX_MAX; i++)
{
for (int j = 0; j < MTRX_MAX; j++)
{
cin >> matrix2[i][j];
}
}
}
void Add_Matrix(int matrix1[][MTRX_MAX], int matrix2[][MTRX_MAX], int sum[][MTRX_MAX])
{
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
sum[i][j] = matrix1[i][j] + matrix2[i][j];
}
}
}
void Print_Matrix(int matrix1[][MTRX_MAX], int matrix2[][MTRX_MAX])
{
for (int i = 0; i < MTRX_MAX; i++)
{
for (int new_line = 0; new_line < 1; new_line++)
{
for (int j = 0; j < MTRX_MAX; j++)
{
cout << sum[i][j] << " ";
}
cout << endl;
}
}
}
Program Output:
Enter the first Matrix:
1 2 3 4 5 6 7 8 9
Enter the second Matrix:
9 8 7 6 5 4 3 2 1
-1717986920 -1717986920 -1717986920 -1717986920 -1717986920 -1717986920 -1717986920 -1717986920 -1717986920
Press any key to continue . . .
Upvotes: 0
Views: 107
Reputation: 364049
Get_Matrix
stores the user input into local variables. They go out of scope when the function returns, discarding the data. Your prototypes look correct for passing 2D arrays for the functions where you do pass them. Things get trickier when you want to pass variable-dimension 2D arrays, but you have it right for the case where all but the first dimension are compile-time constants.
Get_Matrix
needs to take the same args as the other matrix functions (non-const), and store the matrix into the space provided by the caller.
(The other way would be by using global variables, instead of passing pointers around, but don't do that.)
Using a debugger would have showed you that main
's arrays weren't set when Get_Matrix
returned.
Upvotes: 1
Reputation: 31868
In the comments :
for (int new_line = 0; new_line == 1; new_line++) //The condition 'new_line==1' doesn't really seems to be fulfilled
{
for (int j = 0; j < MTRX_MAX; j++)
{
cout << matrix1[i][j] << " ";
}
cout << endl;
}
you end up printing nothing.
Update 1 : you actually don't need that extra loop for (int new_line = 0; new_line == 1; new_line++)
Update 2 :void Print_Matrix(int matrix1[][MTRX_MAX], int matrix2[][MTRX_MAX])
does know what int sum[i][j]
in your code is. Infact Print_Matrix(matrix1, matrix2,sum);
contradicts your declaration and definition.
Upvotes: 1