Hey Everyone,

I have written this Matrix Multiplication program. But The Program seems to crash when during the filling of the first matrix, exactly after the completion of the first row.

Here is my implementation

#include <iostream>
using namespace std;
void generate (int ** ,int ,int);
void fill (int ** ,int,int);
int** compute(int **,int **,int,int,int,int);
void dellocate(int **val,int x,int y);
void display(int **val,int x,int y);

int main()
{
   int **r, **p , **q,row1,row2,col1,col2;
   cout<<"Matrix Multiplication Program :\n";
   cout<<"Please Enter Number of Rows for first matrix :";
   cin>>row1;
   cout<<"Please Enter Number of Columns for first matrix :";
   cin>>col1;
   cout<<"Please Enter Number of Rows for 2nd matrix:";
   cin>>row2;
   cout<<"Please Enter Number of Columns for 2nd matrix :";
   cin>>col2;
   if(col1!=row2)
   {
   cout<<"Multiplication not possible ";
   return 0;
   }
   else
   {
     generate(p,row1,col1);
     generate(q,row2,col2);
     fill(p,row1,col1);
     fill(q,row2,col2);
     r=compute(p,q,row1,col1,row2,col2);
     cout<<"Matrix ONE: "<<endl;
     display(p,row1,col1);
     cout<<"Matrix TWO: "<<endl;
     display(q,row2,col2);
     cout<<"The Output Matrix after Multiplication "<<endl;
     display(r,row1,col2);
     dellocate(p,row1,col1);
     dellocate(q,row2,col2);
     dellocate(r,row1,col2);
   }
}

void generate ( int **x,int a ,int b)
{
   x=new int*[a];
   for (int i=0;i<a;i++)
   {
   x[i]=new int[b];
   }
}

void fill(int **x,int a , int b)
{
   for(int i=0;i<a;i++)
      for(int j=0;j<b;j++)
      {
      cout<<"Enter element no ("<<i<<","<<j<<") :";
      cin>>x[i][j];
      }
}

int** compute(int **x,int **y,int r1,int col1,int r2,int col2)
{
   int ** temp;
   generate(temp,r1,col2);
   for(int i=0;i<r1;i++)
      for(int j=0;j<col2;j++)
       {
         temp[i][j]=0;
           for(int k=0;k<col1;k++)
            {
               temp[i][j]+=(x[i][k])*(y[k][j]);
            }
    }
   return temp;
}

void display(int **val,int x,int y)
{
   for(int i=0;i<x;i++)
   {  
       for(int j=0;j<y;j++)
              cout<<val[i][j]<<" ";
   cout<<endl;
   }
}   

void dellocate(int **val,int x,int y)
{
      for(int i=0;i<x;i++)
      {
         delete [] val[i] ;
      }
      delete [] val;
}

Recommended Answers

You're working with uninitialized pointers. Pass the matrices as references:
[code]
void generate(int&, int, int);
void fill(int
&, int, int);
int compute(int& ,int&, int, int, int, int);
void dellocate(int
& val, int x, int y);
void display(int**& val, int x, int y);
[/code]
Match that in the definitions, and that's the only …

Jump to Post

All 2 Replies

Re: Matrix Multiplication - Dynamic Pointers (Seg Fault) 80 80

You're working with uninitialized pointers. Pass the matrices as references:

void generate(int**&, int, int);
void fill(int**&, int, int);
int** compute(int**& ,int**&, int, int, int, int);
void dellocate(int**& val, int x, int y);
void display(int**& val, int x, int y);

Match that in the definitions, and that's the only change you need to make.

Re: Matrix Multiplication - Dynamic Pointers (Seg Fault) 80 80

Thank You Narue!!
I didn't realise that :)

Be a part of the DaniWeb community

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