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;```
}``
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • 3
    `int n, x, i, j; int arr[n][x];` what is `n` and `x` when `arr` is declared? Hint: They are uninitialized. – Ted Lyngmo Mar 15 '23 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. – Some programmer dude Mar 15 '23 at 18:01

2 Answers2

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');
    }
}
Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
  • what does if(scanf("%d", &arr[i][j]) != 1) return 1; mean? – Asmaa Magdy Mar 16 '23 at 20:53
  • 1
    @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 `int`s 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 `scanf`s 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. – Ted Lyngmo Mar 16 '23 at 21:30
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.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335