#include <stdio.h>int main() { int N, M; scanf("%d %d", &N, &M); int saplings[N][M]; for (int i = 0; i < N; i++) { for (int j = 0; j < M; j++) { scanf("%d", &saplings[i][j]); } } printf("\n"); for (int i = 0; i < N; i++) { for (int j = 0; j < M; j++) { printf("%d ", saplings[i][j]); } printf("\n"); } return 0;}
Question
#include <stdio.h>int main() { int N, M; scanf("%d %d", &N, &M); int saplings[N][M]; for (int i = 0; i < N; i++) { for (int j = 0; j < M; j++) { scanf("%d", &saplings[i][j]); } } printf("\n"); for (int i = 0; i < N; i++) { for (int j = 0; j < M; j++) { printf("%d ", saplings[i][j]); } printf("\n"); } return 0;}
Solution
This is a simple C program that reads in a 2D array of integers from the user and then prints out the array. Here's a step-by-step explanation:
-
The program starts with the inclusion of the standard input/output library,
stdio.h. -
The
mainfunction is declared, which is the entry point of the program. -
Two integer variables,
NandM, are declared. These will hold the dimensions of the 2D array. -
The
scanffunction is used to read in the values ofNandMfrom the user. -
A 2D array,
saplings, of sizeNxMis declared. -
Two nested
forloops are used to iterate over each element in thesaplingsarray. Thescanffunction is used to read in the value of each element from the user. -
A newline character is printed to separate the input from the output.
-
Two more nested
forloops are used to iterate over each element in thesaplingsarray again. This time, theprintffunction is used to print out the value of each element. -
After all elements in a row have been printed, a newline character is printed to move to the next row.
-
The program ends with a return statement,
return 0;, which indicates that the program has ended successfully.
Similar Questions
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.