How to display it (?

int m, n, i, j; int* arrayl; int main() { printf("strings pls:"); scanf_s("%d", &m); printf("columns pls:"); scanf_s("%d", &n); arrayl = (int*)malloc(n * m * sizeof(int)); for (i = 0; i < n; i++) { for (j = 0; j < m; j++) { printf("arrayl (%d)(%d) = ", i, j); scanf_s("%d", arrayl); } } for (i = 0; i < n; i++) { for (j = 0; j < m; j++) { printf("%5d ",); } printf("\n"); } getchar(); getchar(); return 0; } 
  • The code is written utter rubbish when entering the array. There is nothing to display. - AnT

2 answers 2

Your array is located in one piece, so that accessing array[i][j] performed simply as the j element after i lines, i.e. to element number i*(число столбцов)+j , i.e. in your version - array[i*n+j], где n` is the number of elements in the string.

Note that you have strings - m , so you actually fill the array in columns , and in the same way you try to output it ...

So it will be better:

 int rows, cols; printf("Rows pls:"); scanf_s("%d", &rows); printf("columns pls:"); scanf_s("%d", &cols); arrayl = (int*)malloc(rows * cols * sizeof(int)); for (i = 0; i < rows; i++) { for (j = 0; j < cols; j++) { printf("arrayl (%d)(%d) = ", i, j); scanf_s("%d", &arrayl[i*cols+j]); } } for (i = 0; i < rows; i++) { for (j = 0; j < cols; j++) { printf("%5d ",arrayl[i*cols+j]); } printf("\n"); } 

Please note that the variable names - rows and cols (columns) do not make it possible to confuse that there is a number of rows (by the way, a row in the table in English is row, but not string ...), which is a column. I would even change i and j to r and c . The choice of variable names is more important than it seems to beginners ...

  • Thank you very much - user237701
  • and how then can we transpose such a matrix if we fill in the lines? Without using a submenu - user237701
  • Yes, just as usual. If it is square, then rows==cols , so that you exchange a[i*cols+j] with a[j*rows+i] , and if not square - then all the same simple transposition will not work, there will be overlap. It will be easier to create a new matrix of the appropriate size ... - Harry
  • Please see the code below. What is wrong? - user237701
  • So after all exchange , and not assign. Exchange two variables - this is either std::swap(a,b) , or by hand - int tmp = a; a = b; b = tmp; int tmp = a; a = b; b = tmp; - Harry

More habitually, when the appeal to the elements of an array in cycles is carried out in rows and not in columns, as you have. That is, instead of

 for (i = 0; i < n; i++) ^^^^^ { for (j = 0; j < m; j++) ^^^^^ { printf("arrayl (%d)(%d) = ", i, j); scanf_s("%d", arrayl); } } 

It would be better to write

 for (i = 0; i < m; i++) ^^^^^ { for (j = 0; j < n; j++) ^^^^^^ { printf("arrayl (%d)(%d) = ", i, j); scanf_s("%d", &arrayl[i * n + j] ); } } 

because m is the number of rows, and n is the number of columns.

Otherwise this hint

 printf("arrayl (%d)(%d) = ", i, j); ^^^^ 

in your original cycle does not look right and should be

 printf("arrayl (%d)(%d) = ", j, i); ^^^^ 

It is this (corrected) cycle that I will keep in mind when answering a question, so as not to confuse the reader’s answer and question.

You can output the elements of an array with the following statement inside nested loops.

 printf("%5d ", array1[i * n + j] ); 

And for input you need to use a pointer to the corresponding element

 scanf_s("%d",&arrayl[ i * n + j] ); 

That is, in your program a two-dimensional array is simulated using a one-dimensional array.

If there is a two-dimensional array and its element indices i and j , as, for example,

 array2[i][j] 

then the sequence number of the element of this array will be equal to i * n + j . This will be the index of the one-dimensional array.

For simplicity, imagine that you have a two-dimensional array with 10 elements in a row. Then the sequence number of the element, for example, array2[2][5] will be 25. And you can think of the number 25 as 2 * 10 + 5 , that is, 2 * n + 5 , where n is the number of elements in the line. Keep in mind that indexes start from 0. This means that before any element that is located in the line with index 2, there are 2 lines of elements.

  • Can you explain why it is so introduced and displayed? - user237701
  • @PaulFawkes See my updated answer. - Vlad from Moscow
  • In fact, the author of the code m number of lines ... - Harry
  • @Harry Thank you, I corrected this typo. - Vlad from Moscow