0% found this document useful (0 votes)
29 views

Ex 10

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

Ex 10

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Experiment No – 10

Title - Write a program to multiply matrix A with B.

Theory-

Dimension of an array refers to a particular direction in which array elements can be varied.
An array with a single dimension is known as a one-dimensional array. An array that has a
dimension greater than one is known as a multidimensional array.

For example, an array with two dimensions is a two-dimensional array. 2D array is the
simplest form of a multidimensional array which could be referred to as an array of arrays.
The 2D array is organized as matrices which can be represented as the collection of rows and
columns.

Declaration of two dimensional

The syntax to declare the 2D array is given below.

data_type array_name[rows][columns];

Consider the following example.

int twodimen[4][3];

Here, 4 is the number of rows, and 3 is the number of columns.

The following figures illustrates the difference between a one-dimensional array and a
two-dimensional array:
Initializing Two – Dimensional Array

Compile time Array initialization -

We can initialize a two-dimensional array in C in any one of the following two ways:

Method 1 - We can use the syntax below to initialize a two-dimensional array in C of


size x * y without using any nested braces.

int Arr[x][y] = {element 1, element 2, ... element xy}

This will create an array of size x * y with elements being filled in the following
manner:

From left to right, the first y elements will be on the first row. y + 1 onwards, the next
y elements, in the order of left to right, will be filled on the second row. In this
manner, all the x rows will be filled one by one.

Let us understand this with the below example:

int A[2][3] = {3, 2, 1, 8, 9, 10}

So we will have an array of size 2 * 3 with the above initialization. Let us see how the
elements will be filled:

 From left to right, the first three elements will be on the first row.
 Fourth to the last element, in the order of left to right, will be filled on the
second row.

Method 2- A two-dimensional array in C can also be initialized using nested braces,


which makes the visualization of each row and column a bit easier.

The syntax is as follows-


int Arr[x][y] = {{ele 1, ele 2, .. ele y} , {......} , {..., ele xy-1, ele xy}};

Let us see by an example how we can use nested braces to implement the above:

int A[2][3] = {{3, 2, 1}, {8, 9, 10}};

Each nested brace denotes a single row, with the elements from left to right being the
order of elements in the columns in our 2d array.

Thus, the Number of nested braces = the Number of rows.

When a normal array is initialized during declaration, we need not to specify the size
of it. However that’s not the case with 2D array, you must always specify the second
dimension even if you are specifying elements during the declaration. Let’s
understand this with the help of few examples –

/* Valid declaration*/
int abc[2][2] = {1, 2, 3 ,4 }
/* Valid declaration*/
int abc[][2] = {1, 2, 3 ,4 }
/* Invalid declaration – you must specify second dimension*/
int abc[][] = {1, 2, 3 ,4 }
/* Invalid because of the same reason mentioned above*/
int abc[2][] = {1, 2, 3 ,4 }

Runtime Array initialization -

Let's suppose we have an array Arr of 3 rows and 3 columns of type int.

To insert elements at each index,

for(int i = 0; i < 3; i++)


{
for(j=0;j<3;j++)
{
Arr[i][j] = 0;
}
}

To insert elements at each index according to user, we can do the following,

for(int i = 0; i < 3; i++)


{
for(j=0;j<3;j++)
{
scanf("%d", &Arr[i][j]);
}
}
Access C Two Dimensional Array Elements

We can access the Two Dimensional Array in C Programming elements using


indexes. Using the index, we can access or alter/change each element present in the
array separately. The index value starts at 0 and ends at n-1, where n is the size of a
row or column.

For example, if an array of Student[8][5] will store 8 row elements and 5 column
elements. To access or alter 1st value, use Student[0][0], to access or alter 2nd row
3rd column value, then use Student[1][2], and to access the 8th row 5th column, then
use Student[7][4]. Let’s see the example of a C Two Dimensional Array for better
understanding:

int Employees[4][3] = { {10, 20, 30},


{15, 25, 35},
{22, 44, 66},
{33, 55, 77}
};

//To Access the values in the Employees[4][3]


printf("%d", Employees[0][0]) = 10
printf("%d", Employees[0][1]) = 20
printf("%d", Employees[0][2]) = 30
printf("%d", Employees[1][0]) = 15

//To Alter the values in the Employees[4][3]

Employees[2][1] = 98; - It will change the value of Employees[2][1] from 44 to 98

Exercise-

1. Write a C program to check whether two matrices are equal or not.


2. Write a C program to find sum of each row and column of a matrix.
3. Write a C program to find sum of main diagonal elements of a matrix.

You might also like