About Me

My photo
Raipur, Chhattisgarh, India
Hi , I am Amit Thakur. I have worked as a QA Engineer for two years and as a Java Developer for one year in NIHILENT TECHNOLOGIES PVT. LTD., Pune.Currently I am working as DEAN (Research & Development) in Bhilai Institute of Technology, Raipur.

Monday, September 2, 2013

C Program to Print Square of Each Element of 2D Array Matrix

#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 ("%dt", x[col] * x[col]);
    printf("n");
}
Output :
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.
int row=3,column=3;

int arr[row][column];

No comments: