fork download
  1. /* Example 2: User Input for a 2D Array */
  2. #include <stdio.h>
  3. int main() {
  4. int rows, cols;
  5. // Ask user for the size of the array
  6. printf("Enter number of rows and columns: ");
  7. scanf("%d %d", &rows, &cols);
  8. int matrix[rows][cols];
  9. // Input elements into the 2D array
  10. printf("Enter elements of the matrix:\n");
  11. for (int i = 0; i < rows; i++) {
  12. for (int j = 0; j < cols; j++) {
  13. scanf("%d", &matrix[i][j]);
  14. }
  15. }
  16. // Print the 2D array
  17. printf("The entered matrix is:\n");
  18. for (int i = 0; i < rows; i++) {
  19. for (int j = 0; j < cols; j++) {
  20. printf("%d ", matrix[i][j]);
  21. }
  22. printf("\n");
  23. }
  24. return 0;
  25. }
Success #stdin #stdout 0.01s 5280KB
stdin
Standard input is empty
stdout
Enter number of rows and columns: Enter elements of the matrix:
The entered matrix is: