You are on page 1of 6

Experiment no. 3.

TO PERFORM VARIOUS OPERATIONS ON THE ELEMENTS STORED


IN TWO DIMENSIONAL ARRAYS

OBJECTIVES:
· To perform various operations on the elements stored in two dimensional arrays.
· To write programs utilizing two dimensional arrays in C.

THEORY:
A particular sort of data structure called an array can hold a fixed-size sequential collection of identical-
type elements. Comparable to a table with rows and columns is a two-dimensional array. After the
variable name in the declaration statement, two square [] brackets are used to declare two-dimensional
arrays. An array of arrays is one way to define the two-dimensional array. Such arrays can be utilized to
construct a data structure that resembles a relational database. It makes it simple to keep a large amount
of data at once and transfer it to as many functions as needed. Multiplication, transposition, and other
operations involving matrices can all be carried out using two-dimensional arrays.

DEMONSTRATION:
Program 1: To add two matrices

#include <stdio.h>
void main(){
int r, c, i, j, a[10][10], b[10][10], sum[10][10];
printf("Enter the rows of Matrices\n");
scanf("%d", &r);
printf("Enter the columns of Matrices\n");
scanf("%d", &c);
printf("Enter the elements of First Martix\n");
for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++)
{
scanf("%d", &a[i][j]);
}
}
printf("Enter the elements of Second Martix\n");
for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++)
{
scanf("%d", &b[i][j]);
}
}
for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++)
{
sum[i][j] = a[i][j] + b[i][j];
}
}
printf("\nFirst Matrix is\n");
Experiment no. 3.4

for (i = 0; i < r; i++)


{
for (j = 0; j < c; j++)
{
printf("%d\t", a[i][j]);
}
printf("\n");
}
printf("\nSecond Matrix is\n");
for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++)
{
printf("%d\t", b[i][j]);
}
printf("\n");
}
printf("\nSum of the two matrices is\n");
for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++)
{
printf("%d\t", sum[i][j]);
}
printf("\n");
}
}

Output:
Experiment no. 3.4

Program 2 : To multiply two matrices

#include <stdio.h>
void main()
{
int r, c, ro, co, i, j, k, a[10][10], b[10][10], m[10][10];
printf("Enter the rows of First Matrix\n");
scanf("%d", &r);
printf("Enter the columns of First Matrix\n");
scanf("%d", &c);
printf("Enter the rows of Second Matrix\n");
scanf("%d", &ro);
printf("Enter the columns of Second Matrix\n");
scanf("%d", &co);
if (c != ro)
{
printf("Invalid Input!");
return 0;
}
printf("Enter the elements of First Matrix\n");
for (i = 0; i < r;i++)
{
for (j = 0; j < c; j++)
{
scanf("%d", &a[i][j]);
}
}
printf("Enter the elements of Second Matrix\n");
for (i = 0; i < ro;i++)
{
for (j = 0; j < co; j++)
{
scanf("%d", &b[i][j]);
}
}
for (i = 0; i < r;i++)
{
for (j = 0; j < co; j++)
{
m[i][j] = 0;
for (k = 0; k < c; k++)
{
m[i][j] = m[i][j] + a[i][k] * b[k][j];
}
}
}

printf("\nFirst Matrix is\n");


for (i = 0; i < r;i++)
{
for (j = 0; j < c; j++)
Experiment no. 3.4

{
printf("%d\t", a[i][j]);
}
printf("\n");
}
printf("\nSecond Matrix is\n");
for (i = 0; i < ro;i++)
{
for (j = 0; j < co; j++)
{
printf("%d\t", b[i][j]);
}
printf("\n");
}
printf("\nProduct of the two matrices is\n");
for (i = 0; i < r;i++)
{
for (j = 0; j < co; j++)
{
printf("%d\t", m[i][j]);
}
printf("\n");
}
}
Output
Experiment no. 3.4

Program 3 : To find transpose of a matrix.

#include <stdio.h>

void main()
{
int r, c, i, j, a[10][10], t[10][10];
printf("Enter the rows of the Matrix\n");
scanf("%d", &r);
printf("Enter the columns of the Matrix\n");
scanf("%d", &c);

printf("Enter the elements of Martix\n");


for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++)
{
scanf("%d", &a[i][j]);
t[j][i] = a[i][j];
}
}

printf("\nGiven Matrix is\n");


for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++)
{
printf("%d\t", a[i][j]);
}
printf("\n");
}

printf("\nTranspose of given Matrix is\n");


for (i = 0; i < c; i++)
{
for (j = 0; j < r; j++)
{
printf("%d \t", t[i][j]);
}
printf("\n");
}
}
Experiment no. 3.4

Output

Result and Discussion:


In the first program, we calculate the sum of the two matrices entered by the user. Likewise, in the
second program, we calculated the product of the matrices. Finally, in the last program we find the
transpose of the matrix entered by the user.

Conclusion:
Hence, from the above experiment we are able to perform various operation on the elements of a two-
dimensional array in C programming language.

You might also like