Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

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.

ALGORITHM: 1. Declare variables for storing values of rows and columns of matrices

2. Ask user to enter number of rows and columns of the matrices

3. Declare 2D arrays for storing the Matrix elements:


matrix1[r1][c1], matrix2[r2][c2], res[r1][c2]

4. Ask user to enter the elements of the two matrices

5. If number of columns of matrix1 = number of rows of matrix2,


I) For i=0, i<r1, i++ :
i) For j=0, j<c2, j++ :
a) Initialize value of each element of resultant matrix
array to 0
b) For k=0, k<c1, k++ :
res[i][j]+=a[i][k]*b[k][j]
II) Print the resultant matrix

6. If number of columns of matrix1 ≠ number of rows of matrix2


Print that the matrices cannot be multiplied
PROGRAM: #include<stdio.h>

int i,j;

void scan_matrix(int r, int c, int t[r][c])


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

void print_matrix( int r, int c, int t[r][c])


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

}
printf("\n");
}
}

int main()
{
int r1, c1, r2, c2,k;

printf("\nFor First Matrix,\nEnter number of rows:");


scanf("%d",&r1);
printf("Enter number of columns:");
scanf("%d",&c1);
int a[r1][c1];
printf("Enter elements of the first matrix: ");
scan_matrix(r1, c1, a);
printf("\nThe First Matrix is:\n\n");
print_matrix(r1, c1, a);

printf("\nFor Second Matrix,\nEnter number of rows:");


scanf("%d",&r2);
printf("Enter number of columns:");
scanf("%d",&c2);
int b[r2][c2], res[r1][c2];

printf("Enter elements of the second matrix: ");


scan_matrix(r2, c2, b);
printf("\nThe Second Matrix is:\n\n");
print_matrix( r2, c2, b);
printf("\n\n");

if(c1==r2)
{
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{
res[i][j] = 0;
for(k=0;k<c1;k++)
{

res[i][j]+=a[i][k]*b[k][j];

}
}
}
printf("The Multiplication of the Two Matrices is:\n\n");
print_matrix(r1, c2, res);
}

else
printf("The matrices cannot be multiplied.");

}
RESULT:

INPUT/ OUTPUT: INPUT:


Enter number of rows:3
Enter number of columns:2
Enter elements of the first matrix: 1 2 3 1 0 3
1 2
3 1
0 3

Enter number of rows:2


Enter number of columns:3
Enter elements of the second matrix: 1 1 1 2 0 1
1 1 1
2 0 1

OUTPUT:
5 1 3
5 3 4
6 0 3

Program 2

PROBLEM Write a program which reads the current year followed by N followed by a
STATEMENT: list of N employee numbers and their current ages. Produce a list showing
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.

ALGORITHM: 1. Declare variables to store the values of total number of employees


and the current year
2. Create a function that sorts the years of retirement in ascending
order
3. Create a function that prints the retirement years along with the
corresponding employee number and their current age in form of
rows and columns as:
Retirement year Employee No. Current Age
4. Ask user to enter the number of employees and the current year
5. Declare arrays to store the values of the retirement years and current
ages of the employees
6. Ask user to enter the current age of each employee along with their
number
7. Write an equation that calculates the year of retirement of the
employee (consider the age of retirement as 65)
8. Print the resultant matrix containing the required information by
using the functions created in step 3 and 4.
PROGRAM: #include <stdio.h>
void sort_emp(int e[][3],int r)
{
int i,j,min_i;
int t;
//selection sort
for(i=0;i<r-1;i++) //pass loop
{
min_i=i;
for(j=i+1;j<r;j++)
{
if(e[j][2]<e[min_i][2])
min_i=j;
}
//swapping
for(j=0;j<3;j++)
{
t=e[i][j];
e[i][j]=e[min_i][j];
e[min_i][j]=t;
}
}//pass loop
}
void print_emp(int e[][3],int r)
{
int i;
for(i=0;i<r;i++)
{
printf("\n%d",e[i][2]);
printf("\t%d",e[i][0]);
printf("\t%d",e[i][1]);
}
}

int main()
{
int emp[10][3];
int n,i,e,r;
int cy;
printf("\nEnter Current Year:");
scanf("%d",&cy);
printf("\nEnter Number of Employees:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEmployee no.:");
scanf("%d",&emp[i][0]);
printf("Current Age.:");
scanf("%d",&emp[i][1]);
emp[i][2]=cy+65-emp[i][1];
}
printf("\nThe order is of the form:\nRetriement Year, Employee
Number, Current Age of Employee\n");
sort_emp(emp,n);
print_emp(emp,n);

return 0;
}

RESULT:

INPUT/ OUTPUT: INPUT:


Enter Current Year:2021
Enter Number of Employees:5

Employee no.:1
Current Age.:29

Employee no.:2
Current Age.:33

Employee no.:3
Current Age.:57

Employee no.:4
Current Age.:45

Employee no.:5
Current Age.:63

OUTPUT:
2023 5 63
2029 3 57
2041 4 45
2053 2 33
2057 1 29

You might also like