I am having trouble creating a 2D Array of a size defined by the user, with numbers 1, 2, 3.etc.
If the user chooses for example: a = 2 and b = 2
, the program produces:
3 4
3 4
instead of:
1 2
3 4
My program looks like:
#include <stdio.h>
int main()
{
int a = 0;
int b = 0;
int Array[a][b];
int row, column;
int count = 1;
/*User Input */
printf("enter a and b
");
scanf("%d %d", &a, &b);
/* Create Array */
for(row = 0; row < a; row++)
{
for(column = 0; column <b; column++)
{
Array[row][column] = count;
count++;
}
}
/* Print Array*/
for(row = 0; row<a; row++)
{
for(column = 0; column<b; column++)
{
printf("%d ", Array[row][column]);
}
printf("
");
}
return 0;
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…