Logo 
Search:

C++ Programming Articles

Submit Article
Home » Articles » C++ Programming » Mathematics ProgramRSS Feeds

Program to add pairs of MxN matrices

Posted By: Easy Tutor     Category: C++ Programming     Views: 3118

Write a program to add pairs of MxN matrices. Where input should contain multiple test case. Each test case starts with two numbers R (0<R<=100) and C (0<C<=100) on a single line. The next 2R lines contain C numbers each, defining two RxC matrices to be added. Input terminates with a test case containing zero for R and C which should not be processed.

For each test case output the result of addition of the two matrices on R lines each containing C numbers. Separate each test case by a blank line.

Code for Program to add pairs of MxN matrices in C++ Programming

 # include <iostream.h>
 # include <fstream.h>
 # include <string.h>
 # include <stdlib.h>
 # include <conio.h>

 int r=0;
 int c=0;

 int matrix_a[100][100]={0};
 int matrix_b[100][100]={0};

 fstream file;

 void get_matrix_size( );
 void get_matrices_elements( );
 void add_and_show_matrices( );


 int main( )
    {
       clrscr( );

       file.open("CP-02.txt",ios::in|ios::nocreate);

       if(!file)
      {
         cout<<"\n Unable to open the input file."<<endl;
         cout<<"\n Press any key to exit.";

         getch( );
         exit(EXIT_FAILURE);
      }

       do
      {
         get_matrix_size( );
         get_matrices_elements( );
         add_and_show_matrices( );
      }
       while(r!=0 && c!=0);

       file.close( );

       getch( );
       return 0;
    }

 /*************************************************************************///---------------------------  get_matrix_size( )  ----------------------///*************************************************************************/void get_matrix_size( )
    {
       char R[5]={NULL};
       char C[5]={NULL};
       char Input[90]={NULL};

       file.getline(Input,80);

       int i=0;
       int j=0;

       do
      {
         R[i]=Input[i];
         i++;
      }
       while(Input[i]!=' ');

       r=atoi(R);
       i++;

       do
      {
         C[j]=Input[i];
         i++;
         j++;
      }
       while(Input[i]!=' ' && Input[i]!=NULL);

       c=atoi(C);
    }

 /*************************************************************************///-------------------------  get_matrices_elements( )  ------------------///*************************************************************************/void get_matrices_elements( )
    {
       for(int k=0;k<2;k++)
      {
         for(int i=0;i<r;i++)
        {
           char Elements[90]={NULL};

           file.getline(Elements,80);

           int l=0;

           for(int j=0;j<c;j++)
              {
             char Digit[5]={NULL};

             int m=0;

             do
                {
                   Digit[m]=Elements[l];
                   m++;
                   l++;
                }
             while(Elements[l]!=' ' && Elements[l]!=NULL);

             l++;

             if(k==0)
                matrix_a[i][j]=atoi(Digit);

             if(k==1)
                matrix_b[i][j]=atoi(Digit);
              }
        }
      }
    }

 /*************************************************************************///-----------------------  add_and_show_matrices( )  --------------------///*************************************************************************/void add_and_show_matrices( )
    {
       for(int i=0;i<r;i++)
      {
         for(int j=0;j<c;j++)
        cout<<" "<<(matrix_a[i][j]+matrix_b[i][j]);

          cout<<endl;
      }

       cout<<endl;
    }
  
Share: 


Didn't find what you were looking for? Find more on Program to add pairs of MxN matrices Or get search suggestion and latest updates.

Easy Tutor
Easy Tutor author of Program to add pairs of MxN matrices is from United States. Easy Tutor says

Hello Friends,

I am Free Lance Tutor, who helped student in completing their homework.

I have 4 Years of hands on experience on helping student in completing their homework. I also guide them in doing their final year projects.

I have share many programs on this website for everyone to use freely, if you need further assistance, than please contact me on easytutor.2ya [at the rate] gmail [dot] com

I have special discount scheme for providing tutor services. I am providing tutor service to students from various contries, currently most of my students are from United States, India, Australia, Pakistan, Germany, UK and Canada.

I am also here to expand my technical network to receive more opportunity in my career, make friends to help them in resolving their technical problem, learn and share my knowledge, If you like to be my friend, Please send me friend request.

Thanks,
Happy Programming :)

 
View All Articles

 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!