3

I am trying to assign values to each index in my 3x3 dimensional array. I initialized all the values to be 1 at the beginning, then set index[0][2] to be 2. however, somehow index[1][0] also attached to the value 2. then i tried to set [1][2] to 2, and [2][0] also set to value 2. I am not sure what is happening here?

1 | 2 | 1 | ----> 1 | 1 | 2 |

1 | 1 | 1 | ----> 1 | 1 | 1 |

2 | 1 | 1 | ----> 1 | 2 | 1 |

void magicSquare (int param){
//param = 3
int volume = param - 1;

int squareArray[volume][volume];

int c = 0;
int d = 0;

for (int i = 0; i < param*param; i++) {

    squareArray[c][d] = 1;
    c +=1;
     if (c == param) {
                d +=1;
                c = 0;

     }

}

squareArray[0][2]= 2;


c = 0;
d = 0;

printf (" %d | ",squareArray[c][d]);
for (int i = 1; i < param*param; i++) {

    c +=1;

    if (c == param) {
        d +=1;
        c = 0;
        printf ("\n %d | ",squareArray[c][d]);
    }
    else printf (" %d | ",squareArray[c][d]);

}
2
  • Why are you using an if statement in a for loop instead of nested for loop to assign the array? Commented Dec 16, 2014 at 3:17
  • int squareArray[volume][volume]; (Variable Length Array) is not standard C++ and supported only by extension to the language. Use std::vector instead. Commented Dec 16, 2014 at 3:30

3 Answers 3

3

You declare your array as a 2x2 instead of 3x3.

param-1 (3-1) is assigned to volume, thus 2x2. Arrays start indexes from 0 and end at size-1. When declaring arrays, you declare the size, and then access elements from 0 to size-1.

Also, instead of using c and d, you can use nested for loops:

for(int i=0; i<param; ++i){      // will loop [0,param-1] or [0,param) 
    for(int j=0; j<param; ++j){  // same
        arr[i][j] = 1;
    }
}

Edit: I intentionally left out dynamic allocation as the OP is obviously a beginner.

Sign up to request clarification or add additional context in comments.

2 Comments

ok, that's where i misunderstood about c++ array. now it works fine. and thanks for the nested for loop!
you are using C arrays. C++ has STL (standard library) for containers such as arrays, lists, queues and such. You'll see them in the future, they are more handy and easier to use so you won't bother with manual tasks such as boundary checks. You're welcome:) keep up the good work, write more programs!
1

If you always know the dimension of squares at compile-time, then you can write:

template<int param>
void magicSquare ()
{
    int squareArray[param][param];

and leave the rest of your code as it is.

However, if param is not known until runtime then this doesn't work in standard C++. (Some compilers allow an extension but there are good reasons to avoid using such extensions).

Without getting into too much detail, a standard fix would be to write:

    vector< vector<int> > squareArray(param, vector<int>(param));

Yes this is a bit ugly, but then you don't have to change the rest of your code. To enable this you'll need at the top of your file , #include <vector>, and also using std::vector; if you didn't already use namespace std.

Comments

1

You are accessing squareArray out of bounds. Instead of

int squareArray[volume][volume];

use

int squareArray[param][param];

When you declare a 2D array

int array[M][N];

the valid range of indices to use to access the array is:

array[0][0] .... array[M-1][N-1]

In your case, param is 3. You are declaring an array of size 2 x 2. The valid range indices to access the array are :

squareArray[0][0]  .... squareArray[1][1]

In the loop:

for (int i = 0; i < param*param; i++) {

    squareArray[c][d] = 1;
    c +=1;
     if (c == param) {
                d +=1;
                c = 0;

     }
}

you are accessing the array up to squareArray[2][2]. That in itself leads to undefined behavior.

Update

Thanks to @MattMcNabb, I realized that the question has been tagged C++.

int squareArray[param][param];

is not a valid C++ statement. It has to be changed to use a dynamic array of some sort -- such as std::vector or std::array.

You can use:

std::vector<std::vector<int>> squareArray(param, std::vector<int>(param, 0));

4 Comments

array starts at index 0. That's why i used volume = param - 1.
This is not valid in standard C++
@MattMcNabb, thanks for pointing that out. Since the OP had used a VLA in the function, I assumed they were working in C.
does this mean that the int squareArray[param][param] can only be used if the param is fixed, i.e.defined from the beginning of the program, and does not allow to be called as function's parameter?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.