Showing posts with label 2D array. Show all posts
Showing posts with label 2D array. Show all posts

Sunday, 8 May 2016

C++ Program of Multiplying Two Matrices using 2D Array

#include<iostream>
using namespace std;
int main(){
//Using const int for array size
    const int row=2,col=2;
// if not use const error found
cout<<"Size of Matrices : "<<row<<" X "<<col<<endl;
cout<<"Enter Value For FirstMatrix Matrix:"<<endl;

    int firstMatrix[row][col];
    int secondMatrix[row][col];
    int resultantMatrix[row][col], var;

int i,j;
    for( i=0;i<row;i++){
        cout<<"Enter value for row number: "<<i+1<<endl;
        for( j=0;j<col;j++){
            cin>>firstMatrix[i][j];
        }
    }
cout<<"\n\n\nEnter Value For SecondMatrix Matrix:"<<endl;
    for( i=0;i<row;i++){
        cout<<"Enter value for row number: "<<i+1<<endl;
        for( j=0;j<col;j++){
            cin>>secondMatrix[i][j];
        }
    }
var=0;
        // ResultantMatrixant Matrix
        for( i=0;i<row;i++){
            for( j=0;j<col;j++){
                for(int k=0;k<row;k++){
                   var=var+(firstMatrix[i][k]*secondMatrix[k][j]);
                   cout<<var<<endl;
                    }
                 resultantMatrix[i][j]=var;
                 var=0;
            }
        }
cout<<"\n\n\t\tResultant Matrix:"<<endl;

    for( i=0;i<row;i++){
        cout<< endl;
        for( j=0;j<col;j++){
            cout<<"\t\t"<<resultantMatrix[i][j]<<"    ";
        }
    }
return 0;
}

Share:

C++ Program to add two matrix using 2-D arrays

#include<iostream>
using namespace std;

int main(){

//Using const int for array size
    const int row=2,col=2;
// if not use const error found

cout<<"Size of Matrices : "<<row<<" X "<<col<<endl;
cout<<"Enter Value For First Matrix:"<<endl;

int first[row][col], second[row][col];

int i,j;
for( i=0;i<row;i++){

    cout<<"Enter value for row number: "<<i+1<<endl;
    for( j=0;j<col;j++){
        cin>>first[i][j];
    }

}


cout<<"\n\n\nEnter Value For Second Matrix:"<<endl;
for( i=0;i<row;i++){

    cout<<"Enter value for row number: "<<i+1<<endl;

    for( j=0;j<col;j++){
        cin>>second[i][j];

    }

}

// Resultant Matrix
for( i=0;i<row;i++){

    for( j=0;j<col;j++){
        first[i][j]=first[i][j]+second[i][j];

    }

}

cout<<"\n\n\t\tResultant Matrix:"<<endl;
for( i=0;i<row;i++){
    cout<< endl;
    for( j=0;j<col;j++){
        cout<<"\t\t"<<first[i][j]<<"    ";

    }

}
return 0;
}
Share: