Professional Documents
Culture Documents
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
Output:
Experiment no. 3.4
#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("%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
#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);
Output
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.