C Program to Print Square of Each Element of 2D Array Matrix
C Program : C Program to Print Square of Each Element of 2D Matrix
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | #include<stdio.h> #include<conio.h> #define MAX_ROWS 3 #define MAX_COLS 4 void print_square(int[]); void main(void) { int row; int num[MAX_ROWS][MAX_COLS] = { { 0, 1, 2, 3 }, { 4, 5, 6, 7 }, { 8, 9, 10, 11 } }; for (row = 0; row < MAX_ROWS; row++) print_square(num[row]); } void print_square(int x[]) { int col; for (col = 0; col < MAX_COLS; col++) printf("%d\t", x[col] * x[col]); printf("\n"); } |
Output :
1 2 3 | 0 1 4 9 16 25 36 49 64 81 100 121 |
Explanation :
Note 1 :
- Wherever a macro name occurs in Program the Preprocessor Substitutes the code of the macro at that position.
- Whenever we use variable name instead of Macro it will throw error.
1 2 | int row=3,column=3; int arr[row][column]; |