-1

I am currently working on this code. But there are some problems. First of all, if I run this code, it says array size in new-expression must be constant. If I make the array arr[n], this error message does not appear. Also this code makes error message when it gets to the line cout << arr[i][j] << endl; saying that invalid types 'int[int]' for array subscript. I do not understand why this error message comes out because I made the array arr[n][n] not arr[n]. What I want to make with this code is showing the magic-square for n*n if I type in n in the argument line.

This is my main.cc

#include <iostream>
#include <cstdlib>
#include <cstring>
#include "magic_square.h"
using namespace std;

int main(int argc, const char** argv) {
    int n = atoi(argv[1]);
    int *arr = new int[n][n];

    if (n % 2 == 0 || n < 3)
        return 0;
    else
        magicSquare(n, arr);
    for (int i = 0, j = 0; i < n, j < n; i++, j++)
        cout << arr[i][j] << endl;

    delete[] arr;
    return 0;
}

This is my magic_square.cc. I did not add magic_square.h as it is only the declaration of the funtion void magicSquare(int n, int* arr).

#include <iostream>
#include "magic_square.h"

void magicSquare(int n, int* arr) {

    for (int i = 0, j = n/2, num = 1; num <= n*n; num++) {
        arr[i][j] = num;
        if (num % n == 0) {
            i++;
        }
        else {
            i--, j++;
            if (i < 0)
                i = n - 1;
            if (j > (n - 1))
                j = 0;
        }
    }
}
~                                                                           

Can anybody help me with this errors? Thanks in advance!

  • 1
    A `int[n][n]` is not an `int *`. It's not an `int **` either. It's hard to make a fully dynamic 2D array. Usually what is offered is an array of arrays, but these can have poor caching behaviour. A could alternative trick is to make a 1D array and use some quick math `(row*numbercolumns) +column` to fake a 2D array. [Here's an example](https://stackoverflow.com/a/2076668/4581301) – user4581301 Oct 15 '18 at 04:59
  • thx! I've worked it out! :) – Doyoung Kim Oct 15 '18 at 05:05
  • @DoyoungKim: Please post your solution as it can be useful to others as well – P.W Oct 15 '18 at 05:23
  • I changed `int *arr = new int[n][n];` to ```int **arr = new int*[n]; for (int i = 0; i < n; ++i) arr[i] = new int[n]; ``` – Doyoung Kim Oct 16 '18 at 05:09
  • the problem was that I did not make the arr[n][n] 2dimension. I had to use for-loop to make it into a right type! – Doyoung Kim Oct 16 '18 at 05:10

1 Answers1

0

Take account that the for condition is just working because the two comma separated expressions are true at the same time.

for (int i = 0, j = 0; i < n, j < n; i++, j++)

That end condition is equivalent to only do the last test, since its result will be the result of the comma separated expression.

Probably you should have written this:

for (int i = 0, j = 0; i < n && j < n; i++, j++)
UaT
  • 116
  • 9
  • The problem that I had was that I did not make a arr[n][n] type. To fix this I did this: ``` int **arr = new int*[n]; for (int i = 0; i < n; ++i) arr[i] = new int[n]; ``` This made arr[n][n] type and it all worked fine! Thx for the comment though! :) – Doyoung Kim Oct 16 '18 at 05:05