NAME : MUHAMMAD SAFYAN
CLASS : BSSE(3rd)
ROLL NO : FL-249
SUBJECT : DSA LAB
LAB REPORT : 02
SUBMITTED TO : Mr.H.M. Adnan Asghar
------------------------------------------
Task 1:
#include <iostream>
using namespace std;
int main()
{
int rows, columns;
cout << "Enter number of rows for matrix 1 & 2 : ";
cin >> rows;
cout << "Enter number of columns for matrix 1 & 2 : ";
cin >> columns;
while (rows != columns)
{
cout << "\nError : Number of rows and columns are not equal.
Please try again :\n";
cout << "Enter number of rows for matrix 1 & 2 : ";
cin >> rows;
cout << "Enter number of columns for matrix 1 & 2 : ";
cin >> columns;
}
int matrix1[rows][columns];
int matrix2[rows][columns];
int sumMatrix[rows][columns];
cout << "\nMatrix number 1 : " << endl;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
cout << "Enter values at index " << i << j << " : ";
cin >> matrix1[i][j];
}
}
cout << "\nMatrix number 2 : " << endl;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
cout << "Enter values at index " << i << j << " : ";
cin >> matrix2[i][j];
}
}
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
sumMatrix[i][j] = matrix1[i][j] + matrix2[j][i];
}
}
cout << "\nSum of Matrix 1 & 2 : " << endl;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
cout << sumMatrix[i][j] << " ";
}
cout << endl;
}
return 0;
}
console(output):
Task 2:
#include <iostream>
using namespace std;
int main()
{
int row1, column1, row2, column2;
cout << "Enter number of rows for matrix 1 : ";
cin >> row1;
cout << "Enter number of columns for matrix 1 : ";
cin >> column1;
cout << "\nEnter number of rows for matrix 2 : ";
cin >> row2;
cout << "Enter number of columns for matrix 2 : ";
cin >> column2;
while (row1 != column2)
{
cout << "\nError : Matrix 1 rows & Matrix 2 columns are not equal.
Please try again :\n";
cout << "Enter number of rows for matrix 1 : ";
cin >> row1;
cout << "Enter number of columns for matrix 1 : ";
cin >> column1;
cout << "\nEnter number of rows for matrix 2 : ";
cin >> row2;
cout << "Enter number of columns for matrix 2 : ";
cin >> column2;
}
int matrix1[row1][column1];
int matrix2[row2][column2];
int transposeMatrix[column2][row2];
int mulMatrix[row1][column2] = {0};
cout << "\nMatrix number 1 : " << endl;
for (int i = 0; i < row1; i++)
{
for (int j = 0; j < column1; j++)
{
cout << "Enter values at index " << i << j << " : ";
cin >> matrix1[i][j];
}
}
cout << "\nMatrix number 2 : " << endl;
for (int i = 0; i < row2; i++)
{
for (int j = 0; j < column2; j++)
{
cout << "Enter values at index " << i << j << " : ";
cin >> matrix2[i][j];
}
}
for (int i = 0; i < row2; i++)
{
for (int j = 0; j < column2; j++)
{
transposeMatrix[j][i] = matrix2[i][j];
}
}
for (int i = 0; i < row1; i++)
{
for (int j = 0; j < column2; j++)
{
for (int k = 0; k < column1; k++)
{
mulMatrix[i][j] += matrix1[i][k] * transposeMatrix[k][j];
}
}
}
cout << "\nMultiplication of Matrix : " << endl;
for (int i = 0; i < row1; i++)
{
for (int j = 0; j < column2; j++)
{
cout << mulMatrix[i][j] << " ";
}
cout << endl;
}
return 0;
}
console(output):