Pages

Write a program to add general matrix in C language.

Wednesday 24 July 2013

#include <iostream>
using namespace std;

int main()
{
    int a1[100][100], a2[100][100], sum[100][100], m, n, r, c;  
    cout<<"Enter number of rows: ";
    cin>>m;
    cout<<"Enter number of columns: ";
    cin>>n;
    cout<<"Enter elements of 1st metrix: ";
    for(r=0; r<m; r++)
    {
          for(c=0; c<n; c++)
          {
                   cin>>a1[r][c];
          }
    }
    cout<<"\nEnter elements of 2nd metrix: ";
    for(r=0; r<m; r++)
    {
          for(c=0; c<n; c++)
          {
                   cin>>a2[r][c];
          }
    }
    cout<<"\nElements of metrix 1: \n";
    for(r=0; r<m; r++)
    {
          for(c=0; c<n; c++)
          {
                   cout<<a1[r][c]<<"\t";
          }
          cout<<endl;
    }
    cout<<"\nElements of matrix 2: \n";
    for(r=0; r<m; r++)
    {
          for(c=0; c<n; c++)
          {
                   cout<<a2[r][c]<<"\t";
          }
          cout<<endl;
    }
    //addition
    for(r=0; r<m; r++)
    {
          for(c=0; c<n; c++)
          {
                   sum[r][c]=a1[r][c]+a2[r][c];
          }
          cout<<endl;
    }
    cout<<"Addition of matrix is: \n";
    for(r=0; r<m; r++)
    {
          for(c=0; c<n; c++)
          {
                   cout<<sum[r][c]<<"\t";
          }
          cout<<endl;
    }
    system("pause");
}

 

Search Box

Most Reading

Contact Form

Name

Email *

Message *