1

I am trying to create a Magic Square program based on a text file input. I'm getting stuck on the arrays. I need to get the size of the array from 'n' number then store the values of the rows and columns in 2d array. So here's an example from the text file:

3
4 9 2
3 5 7
8 1 6

3 would be the n, then I'd need a 2d array to store the n x n information. Here's what I coded:

int main() {
    int n;
    ifstream inFile;
    inFile.open("input.txt");
    inFile >> n;
    int square[n][n];

    readSquare(n, square);
}

void readSquare(int n, int square[][]) {
    ifstream inFile("input.txt");
    for (int r = 0; r < n; r++)
    {
        for (int c = 0; c < n; c++)
        {
            inFile >> square[r][c];
            cout << square[r][c];
            system("pause");
        }
    }
}
Will Estes
  • 63
  • 10
  • Are you doing this to learn C++, or get a working program? – Beta Dec 06 '15 at 03:46
  • You can't define arrays like that (non-constant size), and you can't pass multi-dimensional arrays as arguments. You should use `std::vector>` instead. – Jonathan Potter Dec 06 '15 at 03:47
  • @Beta I'm doing this for a homework assignment. So I guess "to learn C++." – Will Estes Dec 06 '15 at 03:49
  • @JonathanPotter are vectors 2D? I need to create multiple functions and I need to pass something 2D according to the instructions – Will Estes Dec 06 '15 at 03:51
  • So I presume that `std::vector` is forbidden. I suggest you try something simple first: a program that uses *dynamic allocation* of an array (of variable length), and a function that accepts an argument of type `int*`. Once that's working perfectly, you can contemplate the 2D array. – Beta Dec 06 '15 at 03:53
  • @Beta well for the function I tried to define, here is the instruction: a. Void readSquare (n, square) - Read and print n; Read the n rows into a 2-dimensional array, square Is that similar to what you're suggesting? – Will Estes Dec 06 '15 at 03:58
  • Is there any space between the digits in the file in each row? because if there is no space then you will not get the desired input for your 2D matrix. – surajs1n Dec 06 '15 at 04:15
  • @psyco Yes, there are spaces between each number of each row. – Will Estes Dec 06 '15 at 04:23
  • @WillEstes. Please, put spaces between the digits in the input text file you mentioned on this site. – surajs1n Dec 06 '15 at 04:40

1 Answers1

0

It looks like you haven't got to std::vector yet, for now you can just use plain arrays, which are actually harder.

Creating a 1D array is this:

int *square = new int[n*n];

You can actually use this in place of 2D array. You put row*n + column to access each element at row and col. Or you can use a 2D array:

int **square = new int*[n];
for (int i = 0; i < n; i++)
    square[i] = new int[n];

Then you have to pass the array by reference.

See also
Pass array by reference
Create 2D array

Putting it together:

void readSquare(int &n, int** &square)
{
    std::ifstream inFile("input.txt");
    if (!inFile)
        return;

    inFile >> n;
    if (n < 1) return;

    square = new int*[n];
    for (int i = 0; i < n; i++)
        square[i] = new int[n];

    int row = 0, col = 0;
    while (inFile)
    {
        int temp = 0;
        inFile >> temp;
        square[row][col] = temp;
        col++;
        if (col == n)
        {
            col = 0;
            row++;
            if (row == n) 
                break;
        }
    }
}

int main() 
{
    int n = 0;
    int **square = 0;
    readSquare(n, square);
    if (n)
    {
        //do stuff with n and square
        //free memory which was allocated by readSquare:
        for (int i = 0; i < n; i++)
            delete[]square[i];
        delete[]square;
    }
    return 0;
}
Community
  • 1
  • 1
Barmak Shemirani
  • 30,904
  • 6
  • 40
  • 77
  • Hi. I appreciate your answer. But could you explain the meaning behind the `**` and the `&` signs? – Will Estes Dec 06 '15 at 17:50
  • 1
    `&` does different things in c++. In above function it is used to indicate that the parameter is being "*passed by reference*". That means the changes to `n` and `square` are kept. `*` is pointer which is similar to array. While `**` is pointer to pointer, which is similar to "array of array", or 2-D array. This is important in c++, you have to read books about it, I can't explain it more in comment section. – Barmak Shemirani Dec 06 '15 at 18:13