You are on page 1of 12

Name Suyog Sachin Shah

UID no. 2020300064


Experiment No. 5

AIM:
 Demonstrate the use of two-
dimensional arrays to solve a given
problem.

Program 1
PROBLEM  Write a program to perform matrix multiplication. Dimensions of matrices
STATEMENT : will be decided by user.
PROGRAM: ALGORITHM:

Step 1:START Initialize int multidimensional array A[3]


[3],B[3][3],C[3][3] and int ,i,j,k,sum.
Step 2: Take the input of two matrices from user and
store it in A and B using for loop.
Step 3:Multiply both matrices using for loop and store
the sum in C.
Step 4: Print C
Step 5 : STOP
Code:
#include <stdio.h>

int main()
{
int A[3][3],B[3][3],C[3][3],i,j,k,sum;
printf("Enter the element of Matrix A:");
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
scanf("%d",&A[i][j]);
}
printf("Enter the element of Matrix B:");
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
scanf("%d",&B[i][j]);
}
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
sum=0;
for(k=0;k<=2;k++)
{
sum=sum+A[i][k]*B[k][j];
C[i][j]=sum;
}
}
}

for(i=0;i<=2;i++)
{ printf("\n");
for(j=0;j<=2;j++)

printf(" %d ",C[i][j]);
}

return 0;
}

RESULT: SUCCESSFULLY THE MULTIPLIED TWO MATRICES.

INPUT: A=2,4,7,1,0,3,7,2,7 B=1,8,2,9,3,6,1,7,1


OUTPUT:
Program 2
Write a program which reads the current year followed by N followed by a
PROBLEM
list of N employee numbers and their current ages. Produce a list showing
STATEMENT : the years in which the employees retire (become 65 years old). If more
than one employee retires in a given year then include them all under the
same heading. 
For example
Year
Number
1986
896743
1988
674501
450926
If you have character arrays available then use employee names rather
than number.

.
PROGRAM: ALGORITHM:

Step 1: START

Step 2: Initialize int n,c .

Step 3: Get current year as input and save it in c and number of


employees in n.

Step 4:Initialize multidimensional array data[n][3]

Step 5: Using for loop take input from user of employee ID and
employee age and store it in data[n][3].

Step 6: Using for loop and if else statement find the retirement year of
employee.

Step 7: Print EmployeeID ,EmployeeAge ,RetirementYear using for loop.

Step 8:STOP.
Code:

#include <stdio.h>

int main()

int n,c;

printf("Enter current year:\n");

scanf("%d", &c);

printf("Enter number of Employees:\n");

scanf("%d", &n);

int data[n][3];

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

printf("Enter Employee ID :\n");

scanf("%d", &data[i][0]);

printf("Enter Employee age :\n");

scanf("%d", &data[i][1]);

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

if (data[i][1] < 65)

data[i][2] = c + (65 - data[i][1]);

else if (data[i][1] > 65)

data[i][2] = c - (data[i][1] - 65);

else

{
data[i][2] = c;

printf("EmployeeID\tEmployeeAge\tRetirementYear\n");

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

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

printf("%d\t\t", data[i][j]);

printf("\n");

return 0;
}
RESULT: Arranged the data.

INPUT:

OUTPUT:
Program 3
Write a program to calculate the determinant of matrix. Dimensions of
PROBLEM
matrix will be decided by user.
STATEMENT :
PROGRAM:
ALGORITHM:

.Step 1:START

Step 2: Initialize int A[3][3],d1,d2,d3,d4,d5,d6,d.

Step 3: Take element of matrix A as input from user and store it int
A[3][3].

Step 4: Do d1=A[0][0]*A[1][1]*A[2][2];

d2=A[0][1]*A[1][2]*A[2][0];

d3=A[2][1]*A[1][0]*A[0][2];

d4=A[2][0]*A[1][1]*A[0][2];

d5=A[2][1]*A[1][2]*A[0][0];

d6=A[0][1]*A[1][0]*A[2][2];

d=d1+d2+d3-d4-d5-d6;

Step 5: Print d

Step 6:Stop
Code:
#include <stdio.h>

int main()
{
int A[3][3],d1,d2,d3,d4,d5,d6,d;
printf("Enter the element of Matrix A:");
for(int i=0;i<=2;i++)
{
for(int j=0;j<=2;j++)
scanf("%d",&A[i][j]);
}
d1=A[0][0]*A[1][1]*A[2][2];
d2=A[0][1]*A[1][2]*A[2][0];
d3=A[2][1]*A[1][0]*A[0][2];
d4=A[2][0]*A[1][1]*A[0][2];
d5=A[2][1]*A[1][2]*A[0][0];
d6=A[0][1]*A[1][0]*A[2][2];
d=d1+d2+d3-d4-d5-d6;

printf("Determinant of Matrix A:%d",d);


return 0;
}
RESULT: SUCCESSFULLY FOUND THE DETERMINANT

INPUT: A=3,2,1,7,5,9,1,0,2

OUTPUT:

You might also like