1

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

user3814362
  • 23
  • 1
  • 6
  • possible duplicate of [What is the scope of a while and for loop?](http://stackoverflow.com/questions/7880658/what-is-the-scope-of-a-while-and-for-loop) – Theolodis Jul 08 '14 at 18:33
  • "I'm new to arrays" - you should learn `std::vector` first, and learn about arrays only much later. – Christian Hackl Jul 08 '14 at 19:20

2 Answers2

1

You define j inside the loop and refer to it outside the loop. But once you leave the loop, j is going out of scope.

Ashalynd
  • 12,363
  • 2
  • 34
  • 37
1

The error is self-explanatory: the printout happens outside the nested loop, so j is not in scope any longer. When a variable is defined in the header portion of a composite statement, such as an if, a for, or a while, the scope of that variable (i.e. the place where you can use the variable) ends with the corresponding control statement.

You can change your code to use i to index both rowSum and colSum. You also need to switch the indexes in the += operations inside the nested loop to give i and j proper meaning:

for (int i = 0; i < M; i++)
{
    for (int j = 0; j < N; ++j)
    {
        // For rowSum, i means the row and j means the column
        rowSum[i] += myArray[i][j];
        // For colSum, it is the other way around
        colSum[i] += myArray[j][i];
    }
    // In both cases i represents the current sum:
    // row sum for the rowSum[] array, and column sum for colSum[] array
    cout << left << setw(6) << rowSum[i] << endl;
    cout << left << setw(6) << colSum[i] << endl;
}
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523