#include<stdio.h>
#include<conio.h>
#include<iostream.h>
void main(void)
{
    clrscr();
    int nr,nc,i,j,s;
    int mat1[10][10],mat2[10][10],mat3[10][10];
    cout<<"Enter The Number Of Rows";
    cin>>nr;
    cout<<"Enter The Number Of Column";
    cin>>nc;
    for(i=0;i<nr;i++)
    {
  for(j=0;j<nc;j++)
  {
   cout<<"No. of rows"<<(i+1)<<"No. of column"<<(j+1)<<endl;
   cin>>mat1[i][j];
   }
    }
    cout<<"matrix 2";
    for( i=0;i<nr;i++)
    {
  for( j=0;j<nc;j++)
  {
   cout<<"No. of rows"<<(i+1)<<"No. of column"<<(j+1)<<endl;
   cin>>mat2[i][j];
   }
    }
    for(i=0;i<nr;i++)
    {
  for(j=0;j<nc;j++)
  {
   for(s=0;s<nr;s++)
   {
   mat3[i][j]=mat1[i][j]+(mat2[i][s]*mat2[s][j]);
   cout<<mat3[i][j];
   cout<<"\t";
   }
    //cout<<mat3[i][j];
    cout<<endl;
  }
    }
 getch();
}

Recommended Answers

All 3 Replies

Apart from using void main (this is wrong), what's your question?

Jnabeel, the problem in your case is arising because of faulty logic. You need to place three loops to create a generic matrix multiplier. Read up on matrix multiplications here.

A sample implementation I came up with:

// UNTESTED

#include <iostream>
#include <iomanip>

int main()
{
    using namespace std;

    const int ROWS_FIRST_MATRIX = 3 ;
    const int COLS_SECOND_MATRIX = 3 ;
    const int COLS_FIRST_MATRIX = 3 ;

    int mat1[3][3] = { {1,2,3}, {4,5,6}, {7,8,9} } ;
    int mat2[3][3] = { {1,2,3}, {4,5,6}, {7,8,9} } ;
    int mat3[3][3] = { 0 };

    for (int i = 0; i < ROWS_FIRST_MATRIX; ++i)
    {
        for (int j = 0; j < COLS_SECOND_MATRIX; ++j)
        {
            for (int k = 0; k < COLS_FIRST_MATRIX; ++k)
            {
                mat3[i][j] += mat1 [i][k] * mat2 [k][j] ;
            }
        }
    }

    for (int i = 0; i < ROWS_FIRST_MATRIX; ++i)
    {
        for (int j = 0; j < COLS_SECOND_MATRIX; ++j)
        {
            cout <<  setw (5) << mat3 [i][j];
        }
        cout << endl ;
    }

    getchar ();
    return 0;
}

Apart from using void main (this is wrong), what's your question?

it did not give the correct answer

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.