2

I'm coding a C program that asks the user to input two variables that represent the 2d array sizes and then print the array, I don't know where the problem is! any help?

`#include <stdio.h>
int main(void) {
int n, x, i, j;
int arr[n][x];
printf("Enter no of columns:");
scanf("%d", &n);
printf("Enter no of rows:");
scanf( "%d", &x);
printf("Enter elements: \n");
for (i = 0; i < n; i++)
for (j = 0; j < x; j++)
 scanf("%d", &arr[i][j]);
for (i = 0; i < n; i++){
  for (j = 0; j < x; j++)
  printf("%d \t", arr[i][j]);
  printf("\n");
}   
return 0;```
}``
2
  • 3
    int n, x, i, j; int arr[n][x]; what is n and x when arr is declared? Hint: They are uninitialized. Commented Mar 15, 2023 at 17:59
  • 1
    Uninitialized local variables really are uninitialized. They will have indeterminate values, and it's almost better you look at them as garbage values until you explicitly initialize them one way or another (at definition, assignment, or using e.g. scanf). Before you have made sure the variables have a valid value, just don't use them. Commented Mar 15, 2023 at 18:01

2 Answers 2

1

You need to declare your variable length array arr after having initialized the variables you use to specify rows and columns. I also suggest naming the variables differently to not confuse readers.

Example:

#include <stdio.h>

int main(void) {
    int rows, cols;

    printf("Enter no of columns:");
    if(scanf("%d", &cols) != 1 || cols < 1) return 1;

    printf("Enter no of rows:");
    if(scanf("%d", &rows) != 1 || rows < 1) return 1;

    int arr[rows][cols]; // declare it after rows and cols have been initialized

    printf("Enter elements: \n");
    for(int i = 0; i < rows; i++) {
        for(int j = 0; j < cols; j++) {
            if(scanf("%d", &arr[i][j]) != 1) return 1;
        }
    }

    for(int i = 0; i < rows; i++) {
        for(int j = 0; j < cols; j++) {
            printf("%d \t", arr[i][j]);
        }
        putchar('\n');
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

what does if(scanf("%d", &arr[i][j]) != 1) return 1; mean?
@AsmaaMagdy scanf returns the number of successful extractions, so if you try to extract one int with "%d" it should return 1. If you had tried to extract two ints with "%d %d" it should return 2. It can also return EOF which means that the input stream ended before a match to any %d could be made. So, if your scanfs do not return 1, something failed and return 1; just exits the program. The convention is that returning anything but 0 from main indicates failure.
0

This declaration of the array

int n, x, i, j;
int arr[n][x];
printf("Enter no of columns:");
scanf("%d", &n);
printf("Enter no of rows:");
scanf( "%d", &x);
//...

has undefined behavior because the variables n and x are not initialized. You need to declare the array after entering values to the variables.

Also in the array declaration rows should precede columns.

That is you need at least to write

int n, x, i, j;
printf("Enter no of columns:");
scanf("%d", &n);
printf("Enter no of rows:");
scanf( "%d", &x);
int arr[x][n];
//...

And you need to check that entered values are positive.

Pay attention to that you should declare variables in minimum scopes where they are used.

Comments

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.