-1

I'm writing a program that flips a two dimensional array 90, 180, or 270 degrees based on what the user requests. I figured out the algorithm for rotating the array (which I thought would be the hard part), but I've run into a problem that I never thought I would have. The following code is the snippet that generates the original array based on the dimensions that the user specifies. Its going to be randomly generated, but for troubleshooting purposes, I just made the generated array constantly ascending from 0 to 8 and have been setting the dimensions to 3x3 every time I run it for now.

1: int array_side_length = 3;
2: int x = 0;
3: int y = 0;
4: int original_array[array_side_length][array_side_length];
5: int z = 0;
6:
7: for(x = 0; x < array_side_length; x++){
8:    printf("\n");
9:    for(y = 0; y < array_side_length; y++){
10:        original_array[x][y] = z;
11:        z++;
12:        printf("%d ", original_array[0][0]);
13:    }
14: }

When I change line 12 to read

printf("%d ", original_array[x][y]);

the block of code generates the following array, as expected:

0 1 2
3 4 5
6 7 8

BUT... when I change the parameters of "original_array" to "[0][0]", I get the following:

0 0 0
3 3 3
6 6 6

This is confusing to me, because I expected the code to return an array of all zeros. I'm not seeing where the value of "original_array[0][0]" is changing.

bradybunch
  • 17
  • 1
  • 4
  • You want to do [Matrix transposition](http://stackoverflow.com/a/16743203/489590) ? – Brian Cain Jan 29 '15 at 03:30
  • how are you creating the array (i.e. are you doing int original_array[3][3] or using malloc?) – Foon Jan 29 '15 at 03:32
  • 5
    "I'm not seeing where the value of "original_array[0][0]" is changing" - well, *we're* not seeing where `original_array` is *declared*. Post a [**MCVE**](http://stackoverflow.com/help/mcve) – WhozCraig Jan 29 '15 at 03:33
  • `array_side_length = 0;` ??? Are you sure ? I changed it to 3, and the output is all the zero. I can't understand your question. – pezy Jan 29 '15 at 04:32
  • @BrianCain, yes but I have that part figured out. I also added the variable declarations so now you can see how I'm creating the array. The code above is just _supposed to_ fill in values for each location. – bradybunch Jan 29 '15 at 04:34
  • @pezy, sorry. In the rest of the code that I didn't put in this post, the user defines `array_side_length`. I just initialized it as 0 and forgot to change it to 3 when I posted it. – bradybunch Jan 29 '15 at 04:39
  • 1
    Your code, *verbatim* to what you have above, [**works as expected**](http://ideone.com/2fx4Jd). Whatever your problem is, it isn't with this code. VTC. – WhozCraig Jan 29 '15 at 06:54

1 Answers1

1

I wasn't able to recreate the matrix, by setting "original_array" to "[0][0]",

0 0 0
3 3 3
6 6 6

However, I was able to get the resulting matrix by setting original_array[x][0]. I wanna guess that you might have compiled the following instead of [0][0] ? (or possibly tried to compile original_array[0][0], missed an error and used an old executable?)

Hedge7707
  • 557
  • 1
  • 5
  • 17