0

I am trying to construct a 2D array for an assignment. I've used a nested for loop to construct the 2D array using scanf():

 int width;
 int height;
 scanf("%d %d",&width,&height);
 int array[width][height];
 for (int i=0;i<height;i++){
 for (int j=0;j<width;j++){
 scanf("%d",&array[i][j]);
 }
 }

However when I print the array, I can see that it has been constructed in a strange way, where all the numbers of the first line past a certain point are the first few numbers from the second line (instead of what they should be). The next lines after work fine.

Example:

Input:

6 2

1 3 5 7 9 1

2 4 6 8 0 2

3 4 2 0 1 3

The created array looks like this:

1 3 2 4 6 8 (<-- these last 4 numbers are the first 4 numbers of the second line)

2 4 6 8 0 2 (correct)

3 4 2 0 1 3 (correct)

Any ideas? Thanks a lot.

asked Apr 12, 2018 at 23:25
5
  • 1
    You swapped width and height in the loop, either you do int array[height][width] or you swap the conditions i<width and j<height Commented Apr 12, 2018 at 23:29
  • Thanks for the quick reply. I swapped the loops, but still have a problem. The output is now: 135791 579124 So it is still incorrect, it's just the first 4 numbers of the 2nd line that are incorrect now. Commented Apr 12, 2018 at 23:32
  • check my answer, swapping the array declaration fixes the problem. See ideone.com/OJjj0Y Commented Apr 12, 2018 at 23:40
  • @callum202 I suspect a problem with the example input or display code. Can you post that? Notice that you say the input is 6 2 for size but that would give you two rows not three. Likewise, you seem to be giving it many numbers---so perhaps that's a typo. Commented Apr 12, 2018 at 23:41
  • @Steve thanks Steve, Pablo correctly identified the issue. Commented Apr 12, 2018 at 23:45

1 Answer 1

1

Your declaration of array

int array[width][height];

is wrong. The outer loop goes from 0 to height - 1, but array[i] can only go from 0 to width - 1. The same applies for the inner loop. You swapped width and height in the declaration of the array, it should be

int array[height][width];

Also note that for the matrix

1 3 5 7 9 1
2 4 6 8 0 2
3 4 2 0 1 3

the width is 6 and the height is 3, so the correct input should be

6 3
1 3 5 7 9 1
2 4 6 8 0 2
3 4 2 0 1 3

I compiled and run this code:

#include <stdio.h>
int main(void)
{
 int width;
 int height;
 scanf("%d %d",&width,&height);
 int array[height][width];
 for (int i=0;i<height;i++){
 for (int j=0;j<width;j++){
 scanf("%d",&array[i][j]);
 }
 }
 printf("----------------\n");
 for (int i=0;i<height;i++){
 for (int j=0;j<width;j++){
 printf("%d ", array[i][j]);
 }
 printf("\n");
 }
}

And the output is:

$ ./b 
6 3
1 3 5 7 9 1
2 4 6 8 0 2
3 4 2 0 1 3
----------------
1 3 5 7 9 1 
2 4 6 8 0 2 
3 4 2 0 1 3 

as you can see, now it's reading correctly. See https://ideone.com/OJjj0Y

answered Apr 12, 2018 at 23:32
Sign up to request clarification or add additional context in comments.

1 Comment

Ah great, that fixed it. Yeah I tried your suggestion before but forgot to switch the [height] and [width] in the initialization of the array. Now it works. Thanks a lot for helping me!

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.