Q:

Write a C Program to Print Square of Each Element of Matrix

0

Write a C Program to Print Square of Each Element of Matrix. Here’s simple Program to Print Square of Each Element of Matrix in C Programming Language.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

What is Matrix ?


Matrix representation is a method used by a computer language to store matrices of more than one dimension in memory. C uses “Row Major”, which stores all the elements for a given row contiguously in memory.

Two-dimensional Arrays : :

The simplest form of multidimensional array is the two-dimensional array. A two-dimensional array is, in essence, a list of one-dimensional arrays. An m × n (read as m by n) order matrix is a set of numbers arranged in m rows and n columns.

To declare a two-dimensional integer array of size [x][y], you would write something as follows −

type arrayName [ x ][ y ];

Where type can be any valid C data type and arrayName will be a valid C identifier.


Below is the source code for C Program to Print Square of Each Element of Matrix which is successfully compiled and run on Windows System to produce desired output as shown below :


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

OUTPUT : :


0       1       4       9
16      25      36      49
64      81      100     121

Above is the source code for C Program to Print Square of Each Element of Matrix which is successfully compiled and run on Windows System.The Output of the program is shown above .

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C Matrix Solved Programs – C Programming

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
<< C Program to Find Frequency of Odd and Even Number...