// The program below should add two matrices using function.
// I have problem in the third function (the summing function)
// It's saying that something wrong with the array! But I can't figure out
// how to fix that. I'm new to arrays and functions
// Any help will be appreciated! Thanks in advance!
Now you can create functions that do things to or with a matrix (or matrices).
1 2 3 4 5 6 7 8 9 10 11 12 13
void get_matrix_from_user( const string& name, matrix_t m )
{
cout<<"\n\nEnter elements for Matrix " << name << " :::\n\n";
for(int x=0;i<NROWS;x++)
{
for(int y=0;j<NCOLS;y++)
{
cin>>m[x][y];
}
cout<<"\n";
}
}
1 2 3 4 5 6 7 8 9 10 11
void add_matrices( matrix_t a, matrix_t b, matrix_t m )
// m = a + b
{
for (int x=0; ...)
{
for (int y=0; ...)
{
m[x][y] = a[x][y] + b[x][y];
}
}
}
You'll also want a function to display a matrix to the user:
1 2 3 4
void display_matrix( matrix_t m )
{
for ...
}
Now all you need to do is have a main function:
1 2 3 4 5 6 7 8 9 10 11 12 13
int main()
{
matrix_t a;
matrix_t b;
matrix_t c;
get_matrix_from_user( "A", a );
get_matrix_from_user( "B", b );
add_matrices( a, b, c ); // c = a + b
cout << "\n\nThe sum of Matrices A and B is:\n\n";
display_matrix( c );
}
Be careful with your scratch variable names and with your loops. You didn't use [code] tags, but from the placement of the return statement in your add() function, I suspect you are not being careful with indentation.
You don't need <conio.h> in there. Please get rid of it.
Thanks for your help, but I'm sorry I can't clearly understand your reply. Can you please use my variables so I know exactly my errors.
Also,
* I did define all the matrices, why you ask me to define them?
* I have a cout statement in the add function to display the matrix why you want me to do a for loop?
However, I made some changes can you see this code now, please.
I think my main problem are
a) the function add "header"
b) calling the function add in the main