Much better.
(1)
It asks for the row number twice because you ask for the row number twice. Get rid of lines 34-36. (Why are you asking the user to tell information found in the file? Don't do that.)
Your assignment is to read a file and write a different file. There is no reason to be using
cin or
cout.
(2)
The point of using a function is to avoid having to duplicate code. Notice that you have two functions to read a matrix. The only difference in them is in the names of things:
inputMatrix1 -->
inputMatrix2
and
A -->
B
See
Hint 5 above in my original post. Get rid of one of the functions. It may help you to understand better if you don't use the names "A", "B", or "M" in the function. Try this:
inputMatrix(ifstream& fin, int Q[MAX_SIZE][MAX_SIZE], int nrow, int ncol)
Inside your function, all you have to care about is
Q. That's the point of the argument. The array
Q is whichever one your code tells it to be:
42 43
|
inputMatrix(fin, A, nrow, ncol); // Q == A
inputMatrix(fin, B, nrow, ncol); // Q == B
|
(3)
Move your variables back into
main().
I know it takes some careful reading to understand what the compiler says when it complains, but you have to pay close attention. Here's what GCC says:
a.cpp: In function 'int main()':
a.cpp:18:5: warning: unused variable 'M' [-Wunused-variable]
a.cpp: In function 'void sumArrays(int (*)[20], int (*)[20], int, int)':
a.cpp:65:4: error: 'M' was not declared in this scope
|
This tells us two things are wrong. The first says "unused variable 'M'". In other words, you have a variable that is not used in the function. Take another look at
main(). At no point do you actually
use M. The compiler noticed this and let you know.
The second problem is what you complained about. It says "'M' was not declared in this scope".
Scope is anything that appears between two braces { }. It also gives you a line number (which will probably be different for you than me). If you go to that line you will find it puts you in the middle of the
sumArrays() function, right where
M is used.
The problem is, the function doesn't know anything about any array named "M". It only knows about an array named "A", another array named "B", an integer named "nrow", and another integer named "ncol".
If you want to put the results in "M", then you need to tell the function about it first.
void sumArrays(int M[MAX_SIZE][MAX_SIZE], int A[MAX_SIZE][MAX_SIZE], int B[MAX_SIZE][MAX_SIZE], int nrow, int ncol)
And call it appropriately:
45 46 47
|
sumArrays(M, A, B, nrow, ncol); // M = A + B
writeMatrix(fout, _, nrow, ncol); // which matrix do you want to write to file?
|
(4)
All your functions are ignoring the values of
nrows and
ncols. The number of rows and columns
is (
nrows,
ncols),
not (
MAX_SIZE,
MAX_SIZE).
(5)
Once you get all the above problems fixed and your program works, then you can help make things easier to read with a typedef.
Since all the matrices are the same, you can give a name to the type of a matrix instead of having to spell it all out each time.
1 2 3 4 5 6 7 8 9 10 11 12
|
typedef int Matrix[MAX_SIZE][MAX_SIZE];
...
void inputMatrix(ifstream& fin, Matrix A, int nrow, int ncol);
...
int main()
{
Matrix A, B, M;
int nrows, ncols;
|
(6)
Finally, make sure to check that the user's matrix will fit in your matrix. Your matrix has space for a maximum of a (20, 20) matrix. What if the user gives you a (30,40) matrix?
Once you read the values for (
nrows,
ncols), check to see that they are <=
MAX_SIZE. If they aren't, complain and halt.
Hope this helps.