Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
14 views

Program Matrix

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Program Matrix

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Pgm-1

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-:

Program to demonstrate matrix Multiplication

ALGORITHM

1. Include necessary header files (iostream.h, conio.h)


2. Define constant MAX_SIZE as the maximum size of matrices
3. Define function matrixMultiplication:
- Parameters: mat1[MAX_SIZE][MAX_SIZE], mat2[MAX_SIZE]
[MAX_SIZE], result[MAX_SIZE][MAX_SIZE], rows1, cols1, rows2, cols2
- Initialize all elements of result matrix to 0
- Perform matrix multiplication:
- Iterate over rows of first matrix (i):
- Iterate over columns of second matrix (j):
- Iterate over columns of first matrix or rows of second matrix (k):
- Accumulate the result of multiplying corresponding elements of mat1 and
mat2
- Store the result in result[i][j]
4. End function matrixMultiplication
5. Define main function:
- Declare variables: rows1, cols1, rows2, cols2
- Clear the screen
- Read dimensions for both matrices
- If cols1 is not equal to rows2, display error message and end the program
- Declare matrices: matrix1[MAX_SIZE][MAX_SIZE], matrix2[MAX_SIZE]
[MAX_SIZE], result[MAX_SIZE][MAX_SIZE]
- Read elements of matrix1 and matrix2
- Call matrixMultiplication function with matrices and dimension.
- Display result of matrix multiplication
6. End main function
7. End
8.
SOURCE CODE

#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();
}

You might also like