Write a C program to Print Square of Each Element of 2D Array Matrix
Posted by Samath
Last Updated: January 04, 2017

Write a C Program to Print Square of Each Element of 2D Matrix. Two-dimensional (2D) arrays are indexed by two subscripts, one for the row and one for the column. 
Declare a local variable rating that references a 2D
array of int: 

int[][] rating;

Code:

#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");
}