Program Matrix
Program Matrix
Aim-:
Program to demonstrate matrix addition
ALGORITHM
1. Start
2. Define constants: MAX_ROWS, MAX_COLS
3. Define function matrixAddition:
- Parameters: mat1[MAX_ROWS][MAX_COLS], mat2[MAX_ROWS]
[MAX_COLS], result[MAX_ROWS][MAX_COLS], rows, cols
- Add corresponding elements of mat1 and mat2, store in result
4. Define main function:
- Declare variables: rows, cols, I, j
- Clear screen
- Read rows and cols
- Declare matrices: matrix1[MAX_ROWS][MAX_COLS],
matrix2[MAX_ROWS][MAX_COLS], result[MAX_ROWS][MAX_COLS]
- Read elements of matrix1 and matrix2
- Call matrixAddition function with matrices and dimensions
- Display sum of matrices
5. End main function
6. End
SOURCE CODE
#include <iostream.h>
#include <conio.h>
Const int MAX_ROWS = 100;
Const int MAX_COLS = 100;
Void matrixAddition(int mat1[MAX_ROWS][MAX_COLS], int mat2[MAX_ROWS]
[MAX_COLS], int result[MAX_ROWS][MAX_COLS], int rows, int cols) {
For (int I = 0; I < rows; ++i) {
For (int j = 0; j < cols; ++j) {
Result[i][j] = mat1[i][j] + mat2[i][j];
}
}
}
Void main() {
Int rows, cols;
Int I, j; // move declaration outside the loops
Clrscr();
Cout << “Enter the number of rows and columns for the matrices: “;
Cin >> rows >> cols;
Int matrix1[MAX_ROWS][MAX_COLS], matrix2[MAX_ROWS][MAX_COLS],
result[MAX_ROWS][MAX_COLS];
Cout << “Enter elements of first matrix:” << endl;
For (I = 0; I < rows; ++i) { // use the declared I and j
For (j = 0; j < cols; ++j) {
Cout << “Enter element “ << I + 1 << “,” << j + 1 << “: “;
Cin >> matrix1[i][j];
}
}
Cout << “Enter elements of second matrix:” << endl;
For (I = 0; I < rows; ++i) { // use the declared I and j
For (j = 0; j < cols; ++j) {
Cout << “Enter element “ << I + 1 << “,” << j + 1 << “: “;
Cin >> matrix2[i][j];
}
}
matrixAddition(matrix1, matrix2, result, rows, cols);
cout << “Sum of matrices:” << endl;
for (I = 0; I < rows; ++i) { // use the declared I and j
for (j = 0; j < cols; ++j) {
cout << result[i][j] << “ “;
}
Cout << endl;
}
getch();
L
}
Date-
Pgm-2
Aim-:
ALGORITHM
#include <iostream.h>
#include <conio.h>
Const int MAX_SIZE = 10;
Void matrixMultiplication(int mat1[MAX_SIZE][MAX_SIZE], int mat2[MAX_SIZE]
[MAX_SIZE], int result[MAX_SIZE][MAX_SIZE], int rows1, int cols1, int rows2, int cols2)
{
For (int I = 0; I < rows1; ++i)
{
For (int j = 0; j < cols2; ++j)
{
Result[i][j] = 0;
For (int k = 0; k < cols1; ++k)
{
Result[i][j] += mat1[i][k] * mat2[k][j];
}
}
}
}
Void main()
{
Int rows1, cols1, rows2, cols2;
Clrscr();
Cout << “Enter the number of rows and columns for the first matrix: “;
Cin >> rows1 >> cols1;
Cout << “Enter the number of rows and columns for the second matrix: “;
Cin >> rows2 >> cols2; if (cols1 != rows2)
{
Cout << “Matrix multiplication not possible. Number of columns in the first matrix must
be equal to the number of rows in the second matrix.”;
Getch();
Return;
}
Int matrix1[MAX_SIZE][MAX_SIZE], matrix2[MAX_SIZE][MAX_SIZE],
result[MAX_SIZE][MAX_SIZE];
Cout << “Enter elements of first matrix:” << endl;
For (int I = 0; I < rows1; ++i)
{
For (int j = 0; j < cols1; ++j)
{
Cout << “Enter element “ << I + 1 << “,” << j + 1 << “: “;
Cin >> matrix1[i][j];
}
}
Cout << “Enter elements of second matrix:” << endl;
For (int I = 0; I < rows2; ++i)
{
For (int j = 0; j < cols2; ++j)
{
Cout << “Enter element “ << I + 1 << “,” << j + 1 << “: “;
Cin >> matrix2[i][j];
}
}
matrixMultiplication(matrix1, matrix2, result, rows1, cols1, rows2, cols2);
cout << “Result of matrix multiplication:” << endl;
for (int I = 0; I < rows1; ++i)
{
For (int j = 0; j < cols2; ++j)
{
Cout << result[i][j] << “ “;
}
Cout << endl;
}
Getch();
}