English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

that is a 2-dimensional array that makes this output it uses the random funcion:

Enter row size:4
Enter column size: 5
6 5 4 7 8
0 2 9 2 3
6 4 3 2 2
8 7 4 1 1<
Enter first column: 4
Enter second colmn: 2

column sum:
12
4
6
8

2007-01-30 18:30:00 · 1 answers · asked by Edrew c 2 in Computers & Internet Programming & Design

1 answers

There is no way to allocate a 2D array in C++ using dynamic value (ie. values entered by the user). thus, you have to "fake" a 2D array using a 1-dimensional array or a pointer.

scanf("%d", rows);
scanf("%d", columns);
int* myarray = new int[rows*columns];

for(int i=0; i < rows; i++)
{
for( int j=0; j < columns; j++)
{
myarray[i*columns + j] = rand()*10;
printf("%d ", myarray[i*columns + j]);
}
printf("\n");
}

2007-01-30 18:43:07 · answer #1 · answered by sspade30 5 · 0 0

fedest.com, questions and answers