To choice the matrix operation write a program in C++

Question

Please program these one ma’am/sir. In addition, please add a explanation and the function use and how it work. Thank you!
1.) Write a program in C++ using a function to Add, subtract and Multiply matrices. Your program should use a menu type, that the user had an option to choice the matrix operation.

Summary

Here we write a program in C++ to choose the matrix operation, have an addition, subtraction, and multiplication of numbers in the matrices. Where in the program we will use a menu type, that the user had an option to choose the matrix operation. Where user will have the choice of what he wants to do i.e. add, sub or mul. After that, we will have an output.

Explanation

First, we enter the header files. Next, we add the add function to perform the addition operation. There are also different parameters pass to the function with the data type as int. In the add function, there is a nested for loop. As we have matrices there are two for loops. After the for loop, there is a cout statement that will represent the output of addition.

Once the additional statements are done we will write a subtraction statement. Next, there is a subtraction function with the different parameters along with int as a data type. Here also we have nested for loop to perform subtraction. And a cout statement to display it.

After we are done with the subtraction we have a multiplication function with again different parameters and int data types. It also has the same nested for loop and cout statement. Next, we have the main code where we provide a choice to the user. It depends on the user what operation he wants to perform. Based on the choice that operation or that function will get executed. Once we are done with this we have our output on the screen.

Code

#include <iostream>
using namespace std;
/* to add two matrices */
void add(int m1[10][10],int m2[10][10],int m,int n)
{
    int m3[10][10];
    /* adding each element from two matrices */
    for(int i=0;i<m;i++){
        for(int j=0;j<n;j++){
            m3[i][j]=m1[i][j]+m2[i][j];
        }
    }
    /* to print the resultant matrix */
    cout<<"M1 + M2: "<<endl;
    for(int i=0;i<m;i++){
        for(int j=0;j<n;j++)
            cout<<m3[i][j]<<" ";
        cout<<endl;
    }
    cout<<endl;
}

/* to subtract two matrices */
void subtract(int m1[10][10],int m2[10][10],int m,int n)
{
    int m3[10][10];
    /* subtracting each element of two matrices */
    for(int i=0;i<m;i++){
        for(int j=0;j<n;j++){
            m3[i][j]=m1[i][j]-m2[i][j];
        }
    }
     /* to print the resultant matrix */
    cout<<"M1 - M2: "<<endl;
    for(int i=0;i<m;i++){
        for(int j=0;j<n;j++)
            cout<<m3[i][j]<<" ";
        cout<<endl;
    }
    cout<<endl;
}

void multiply(int m1[10][10],int m2[10][10],int m,int n)
{
    int m3[10][10];
    for(int i=0;i<m;i++)
    for(int j=0;j<n;j++)
    m3[i][j]=0;
    /* sum of product fo each row of m1 with each column of m2 */
    for(int i=0;i<m;i++){
        for(int j=0;j<n;j++){
           for(int k=0;k<n;k++)
            m3[i][j]+=m1[i][k]*m2[k][j];
        }
    }
     /* to print the resultant matrix */
    cout<<"M1 * M2: "<<endl;
    for(int i=0;i<m;i++){
        for(int j=0;j<n;j++)
            cout<<m3[i][j]<<" ";
        cout<<endl;
    }
    cout<<endl;
}

/* Main code */
int main()
{
    cout<<"MENU\n1) Add\n2) Subtract\n3) Multiply\n4) Exit\n\n";
    int choice;
    while(true){
        cout<<"Enter your choice: ";
        cin>>choice;
        if(choice==4)
        break;
        int m,n;
        cout<<"Enter column size: "; cin>>m;
        cout<<"Enter row size: "; cin>>n;
        int m1[10][10],m2[10][10];
        cout<<"Enter m1:\n";
        for(int i=0;i<m;i++)
        for(int j=0;j<n;j++)
        cin>>m1[i][j];
        cout<<"Enter m2:\n";
        for(int i=0;i<m;i++)
        for(int j=0;j<n;j++)
        cin>>m2[i][j];
        if(choice==1){
            add(m1,m2,m,n);
        }
        else if(choice==2){
            subtract(m1,m2,m,n);
        }
        else if(choice==3){
            multiply(m1,m2,m,n);
        }
        else if(choice==4)
        break;
        else 
        cout<<"Invalid choice!"<<endl;
    }
}
}

Output

choice the matrix operation

 

Also read, Implement a task finding the root of a function f(x). You are to implement the Bisection method.

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *