I'm new to arrays and have been trying to do the following:
a) generate a 12x12 matrix of random numbers
b) output the matrix
c) compute and output the sum of the rows
d) compute and output the sum of the columns
So far, I've been able to do a, b c and part of d.
My code is:
#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <iomanip>
using namespace std;
const int M = 12;
const int N = 12;
int myArray[M][N] = {0};
int rowSum[M] = {0};
int colSum[N] = {0};
void generateArray();
void sumRowsAndColumns();
int main()
{
generateArray();
sumRowsAndColumns();
return 0;
}
void generateArray()
{
// set the seed
unsigned setSeed = 1023;
srand(setSeed);
// generate the matrix using pseudo-random numbers
for (int i = 0; i < M; i++)
{
for (int j = 0; j < N; j++)
{
myArray[i][j] = rand() % 100;
// outputs the raw matrix (in case we need to see it)
cout << left << setw(4) << myArray[i][j] << " ";
}
cout << endl;
}
cout << endl << endl;
}
void sumRowsAndColumns()
{
cout << endl;
for (int i = 0; i < M; i++)
{
for (int j = 0; j < N; ++j)
{
rowSum[i] += myArray[i][j];
colSum[j] += myArray[i][j];
}
cout << left << setw(6) << rowSum[i] << endl;
cout << left << setw(6) << colSum[j] << endl; // the error indicates the the 'j' is undefined
}
}
At line:
cout << left << setw(6) << colSum[j] << endl;
I'm getting the error:
"j is undefined"
What's confusing is that I can 'see' the result when I hover over "colSum" in Visual Studio. I'm just having trouble with the output.
Can anyone offer any guidance in terms of how to define "j" (even though it looks like it's defined)?
Thanks, Ryan