Software & Finance





C++ - Matrix Addition and Subtraction with out using classes





I have given the program for matrix addition and subtraction in one function. This is a simple version.

To access the more flexible and enhanced version using the class CMatrix is using operator overloading +, -, =, ==, >> and << along with copy constructor, click here.

 


Source Code


#include <stdio.h>

#include <iostream>

#include <tchar.h>

#include <math.h>

 

int main()

{   

    int row1, col1;

    double matrixA[24][24];

    double matrixB[24][24];

    double matrixC[24][24];

 

    // Init Array

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

    {

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

        {

            matrixA[i][j] = 0;

            matrixB[i][j] = 0;

            matrixC[i][j] = 0;

        }

    }

 

 

    // Input the rows and cols of the matrix A and B

    std::cout << "Enter rows and columns of the Matrix A and B: ";

    std::cin >> row1;

    std::cin >> col1;

   

    // Input for Matrix A

    {

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

        {

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

            {

                std::cout << "Input For Matrix A Row: " << i + 1 << " Col: " << j + 1 << " = ";

                std::cin >> matrixA[i][j];

            }

            std::cout << "\n";

        }

        std::cout << "\n";

    }

 

    // Input for Matrix B

    {

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

        {

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

            {

                std::cout << "Input For Matrix B Row: " << i + 1 << " Col: " << j + 1 << " = ";

                std::cin >> matrixB[i][j];

            }

            std::cout << "\n";

        }

        std::cout << "\n";

    }

 

    // Matrix C = A + B

    {

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

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

                matrixC[i][j] += matrixA[i][j] + matrixB[i][j];

    }

 

 

    // Display Matrix C = A + B

    {

        std::cout << "\n\nMatrix C = A + B : " << " Rows: " << row1 << " Cols: " << col1 << "\n\n";

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

        {

            std::cout << " | ";

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

                std::cout << matrixC[i][j] << " ";

            std::cout <<  "|\n";

        }

        std::cout << "\n\n";

    }

    return 0;

}

 

Output


Enter rows and columns of the Matrix A and B: 3

3

Input For Matrix A Row: 1 Col: 1 = 3

Input For Matrix A Row: 1 Col: 2 = 3

Input For Matrix A Row: 1 Col: 3 = 3

 

Input For Matrix A Row: 2 Col: 1 = 6

Input For Matrix A Row: 2 Col: 2 = 6

Input For Matrix A Row: 2 Col: 3 = 6

 

Input For Matrix A Row: 3 Col: 1 = 9

Input For Matrix A Row: 3 Col: 2 = 9

Input For Matrix A Row: 3 Col: 3 = 9

 

 

Input For Matrix B Row: 1 Col: 1 = 2

Input For Matrix B Row: 1 Col: 2 = 2

Input For Matrix B Row: 1 Col: 3 = 2

 

Input For Matrix B Row: 2 Col: 1 = 4

Input For Matrix B Row: 2 Col: 2 = 4

Input For Matrix B Row: 2 Col: 3 = 4

 

Input For Matrix B Row: 3 Col: 1 = 6

Input For Matrix B Row: 3 Col: 2 = 6

Input For Matrix B Row: 3 Col: 3 = 6

 

 

 

 

Matrix C = A + B :  Rows: 3 Cols: 3

 

 |  5  5  5 |

 | 10 10 10 |

 | 15 15 15 |