Given a 2-D array of size n x n, get the value of the array and print the values of the array.
#include<stdio.h> #include<stdlib.h> #define n 6 // Size of the square matrix n x n int main() { /* Array of pointers to 1-D arrays in the 2-D array */ int *a[n]; int i,j; /* Memory allocation for 1-D arrays in the 2-D array */ for(i=0;i<n;i++) *(a+i)=(int*)malloc(sizeof(int)*n); /* Get the input for the 2-D array */ for(i=0;i<n;i++) { for(j=0;j<n;j++) { scanf("%d",(*(a+i)+j)); } } /* Print the values stored in the 2-D array */ for(i=0;i<n;i++) { for(j=0;j<n;j++) { printf("%d ",*(*(a+i)+j)); } /* After printing every 1-D array , print a newline */ printf("\n"); } return 0; }
Input:
1 2 3 4 5 6
7 8 9 10 11 12
13 14 15 16 17 18
19 20 21 22 23 24
25 26 27 28 29 30
31 32 33 34 35 36
Output:
1 2 3 4 5 6
7 8 9 10 11 12
13 14 15 16 17 18
19 20 21 22 23 24
25 26 27 28 29 30
31 32 33 34 35 36