Please can anyone tell me why that program crashes when I enter the first matrix?

#include<iostream>
#include <stdlib.h>
#include <windows.h>
using namespace std;


void menu()

{
    cout<<" \t\t\t\t*-*-UNIATIVE  Matrix calculator-*-*\t\t\t\t   "<<"\n";
    cout<<"        "<<(char)1<<"         NNNN    MMMM       AAAA     "<<(char)1<<"         "<<"\n";
    system ("color 08");
    _sleep(1000);
    cout<<" \t\t\t  *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-\t\t\t"<<"\n";
    cout<<"Ahlan washlan ya user Ya Habibi,Wahashteni Awi Awi"<< (char)1 <<"\n";
    cout<<"                                          "<<"\n";
    cout<<"Please choosen from the following menu."<<"\n";
    cout<<"                                              "<<"\n";
    system ("color 05");
    cout<<"1.  Matrix Addition            "<<"\n";
    cout<<"2.  Matrix subtraction         "<<"\n";
    cout<<"3.  Matrix  Multiplication     "<<"\n";
    cout<<"4.  Matrix Transpose           "<<"\n";
    cout<<"5.  check  Matrix properties   "<<"\n";
    cout<<"6.  Matrix Inversion           "<<"\n";
    cout<<"7.  Matrix power               "<<"\n";
    cout<<"8.  Exit                       "<<"\n";
    cout<<"please Enter your choice==>"<<"\n"<<"\n";
}
void read( int **matr1,int **matr2,int& rows,int& cols,int& rows2,int& cols2){
 
        cout<<"please enter the dimensions of the matrix one==>"<<"\n";
        cin>>rows>>cols;
        cout<<"please enter the first matrix"<<"\n";
        for(int i=0;i<rows;i++)
        {
        for(int j=0;j<cols;j++)
        cin>>matr1[i][j];
        }
        cout<<"please enter the dimensions of the matrix one==>"<<"\n";
        cin>>rows2>>cols2;
        cout<<"please enter the second matrix"<<"\n";
        for(int i=0;i<rows2;i++)
        {
        for(int j=0;j<cols2;j++)
        cin>>matr2[i][j];
        
        }       
       delete matr1;
       delete matr2;
}


void read2 ( int **matr1,int& rows,int& cols){
  
     cout<<"please enter the dimensions of the matrix==>"<<"\n";
        cin>>rows>>cols;
        cout<<"please enter the  matrix"<<"\n";
        for(int i=0;i<rows;i++)
        {
        for(int j=0;j<cols;j++)
        cin>>matr1[i][j];
        }
}
void writeMat( int **matr1,int &rows,int &cols)
{
    for(int i=0;i<rows;i++)
    {
       for(int j=0;j<cols;j++)
       {
               cout<<matr1[i][j]<<"  ";     
       }
       
       cout<<endl;
    }
           delete matr1;
} 

void sumMat(int **matr1 ,int **matr2 ,int &rows,int& cols,int& rows2,int& cols2)
    {    
     int ** result= new int *[rows];
     for(int i=0;i<rows;i++)
     {
       result[i]=new int [cols];
     }  
read( matr1,matr2,rows,cols,rows2,cols2);
writeMat( matr1,rows,cols);
cout<<endl<<"+"<<endl;
writeMat( matr1,rows,cols);
cout<<endl<<"="<<endl;
        for(int i=0;i<rows;i++)
        {
        for(int j=0;j<cols;j++)
        result[i][j]=matr1[i][j]+matr2[i][j];
        }
        for(int i=0;i<cols;i++)
        {
        for(int j=0;j<rows;j++)
        {
        cout<<result[i][j]<<"  ";
        }
        cout<<"\n";
        }
 delete result;
 delete matr1;
 delete matr2;
   }  
 void subtraction(int **matr1,int **matr2,int rows,int cols,int& rows2,int& cols2)
    {   
          
    int ** result= new int *[rows];
     for(int i=0;i<rows;i++)
     {
             result[i]=new int [cols];
     } 
read( matr1,matr2,rows,cols,rows2,cols2);
writeMat( matr1,rows,cols);
cout<<endl<<"-"<<endl;
writeMat( matr1,rows,cols);
cout<<endl<<"="<<endl;
        for(int i=0;i<rows;i++)
        {
        for(int j=0;j<cols;j++)
        result[i][j]=matr1[i][j]-matr2[i][j];
        }
        for(int i=0;i<rows;i++)
        {
        for(int j=0;j<cols;j++)
        {
        cout<<result[i][j]<<"  ";
        }
        cout<<"\n";
        }
         delete matr1;
         delete matr2;
         delete result;
}
 void transpose(int **matr1,int rows,int cols)
   {
         read2 ( matr1, rows,cols);
         writeMat( matr1,rows,cols);
         cout<<"after transposing ";
        for(int i=0;i<rows;i++)
        {
        for(int j=0;j<cols;j++)
          cout<<matr1[j][i]<<" ";      
        }
      
                cout<<"\n";
        
          delete matr1;
   }
void symmCheck(int **matr1,int& rows,int& cols)
{
 read2 ( matr1, rows,cols);
 if(rows==cols)
 {
for(int i=0;i<rows;i++)
     {
  for(int j=0;j<cols;j++)
        {
         if(matr1[i][j]==matr1[j][i]){
                                      
        cout<<"  this matrix is symmetric   "<<endl;     
        }
        else if(matr1[i][j]== -matr1[j][i])
        {
             cout<<"  this matrix is antisymmetric  "<<endl;
        }
     } 
     }
     }
     else {
          cout<<"da5al el rows wel columns zee ba3d ya user ya 7abeby :)"<<endl;
          return symmCheck(matr1, rows,cols);
          }
      delete matr1;    
}      
void digonalcheck(int **matr1,int& rows,int& cols) 
{    
     read2 ( matr1, rows,cols);
       if(rows==cols)
 {       
for(int i=0;i<rows;i++)
     {
  for(int j=0;j<cols;j++)
        {
         if(matr1[i][j]==0&&matr1[i][i]!=0)
         {
        cout<<"   this matrix is diagonal   "<<endl;
        }
        else {
             cout<<"   this matrix is not digonal  "<<endl;
             }
              if(matr1[i][j]==0&&matr1[j][j]!=0)
             {
                  cout<<"   this matrix is off digonal  "<<endl;
                  }
             }
             }
             }
             else {
                  cout<<"da5al el rows wel columns zee ba3d ya user ya 7abeby :)"<<endl;
          return digonalcheck(matr1, rows, cols);
          }
delete matr1;
}          
void   identityCheck(int **matr1,int& rows,int& cols)
{
          read2 ( matr1, rows,cols);
       if(rows==cols)
 {
        
        for(int i=0;i<rows;i++)
     {
  for(int j=0;j<cols;j++)
        {
       if(matr1[i][j]==0&&matr1[i][i]==1)
       {
          cout<<"   this matrix is Identity  "  <<endl;                             
       }
       else {
            
          cout<<"   this matrix is not Identity "  <<endl;
       }
       }
       }
  }
       
}
void tringLower( int **matr1,int& cols, int& rows)                  
{
     int count=0;
     if (cols==rows)
     {
          for(int i=0;i<rows;i++)
          {
               for(int j=0;j<cols;j++)
               { 
                   if((j>i) && (matr1[i][j]==0))                        
                   {
                         count++;
                   }
               }
          } 
          if(count==3)
          {
               cout<<"\n\tlower Tringular\n";
          } 
          else
          {
               cout<<"\n\tnot tringular\n"; 
          }
     }
                 

                       else{
                 cout<<" ya user ya amor da5al el rows wel columns zaee ba3d"<<endl;
                 tringLower( matr1,cols,rows);  
                     }
                     }
                     
                    
void  tringupper( int **matr1,int& cols, int& rows) 
  {
                int count=0;
     if (cols==rows)
     {
          for(int i=0;i<rows;i++)
          {
               for(int j=0;j<cols;j++)
               { 
                   if((j<i) && (matr1[i][j]==0))                        
                   {
                         count++;
                   }
               }
          } 
          if(count==3)
          {
               cout<<"\n\tUpper Tringular\n";
               }
          else
          {
               cout<<"\n\tnot tringular\n"; 
          }
         
     } 
                     else{
                 cout<<" ya user ya amor da5al el rows wel columns zaee ba3d"<<endl;
                 tringupper(matr1, cols, rows);
                     }
               
                     cout<<endl;
}   
 int power1(int** matr1,int** matr2,int& rows,int& cols)
{
     int power2;
     int **result;
     result=new int*[rows];
    for(int i=0;i<rows;i++)
    {
            result[i]=new int[cols];
    }
     read2( matr1,rows,cols);
     cout<<"please enter your power    "<<endl;
     cin>>power2;
     writeMat( matr1,rows,cols);
     cout<<endl<<"to the power "<<power2<<endl;
    
     cout<<"="<<endl;
     
     for(int i=0;i<power2;i++)
     {
           
           for(int j=0;j<rows;j++)
           {
               for(int k=0;k<cols;k++)
               {
                    result[j][i]=0;
                    for(int l=0;l<cols;l++)
                    {
                     result[j][i]+=matr1[j][l]*matr2[l][i];
                            
                    }
                    cout<<result[j][i]<<"\t";
               }
               cout<<endl;
           }
     }
}
int main()

{
    int rows ,cols,rows2,cols2;
    int **matr1,**matr2;
    bool flag1,flag2,flag=true;
 
        int choice;
    while(flag==true)
    {    int choice;
       menu();
    cin>>choice;
switch (choice)
{
   
       case 1:    
            sumMat(matr1 ,matr2 , rows, cols,rows2,cols2); 
          break;
     case 2:  subtraction(matr1,matr2,rows,cols,rows2,cols2);
          break;
     case 3:
          break;
     case 4:
          transpose(matr1,rows,cols);
          break;
          
    case 5:digonalcheck(matr1, rows,cols);
            identityCheck(matr1,rows,cols);
            identityCheck(matr1, rows,cols);
            symmCheck(matr1, rows,cols);
            tringLower( matr1,cols,rows);                  
            tringupper( matr1,cols, rows);
          break;
     case 6:
          
         break;
     case 7:
         power1( matr1, matr2, rows, cols);
         break;
     case 8:
         system ("pause");
         return 0;
         break;
         
         default :
                 cout<<"  ya abeny homa 8 2odamak  ";
}
}
    delete matr1;
    delete matr2;
        system("pause");
    return 0;
    
    
}

Just some advices (after a first quick look at your code, not a solution to your problem):

  • In your main-function I encounter this line: while(flag==true) it's just the long way to write: while(flag)
  • I also encounter the nasty system("pause"); line, there's actually no need to use this, you could just use cin.get(); instead (why not to use system("pause"); ?)...
Comments
You should perhaps add that system() remark on your signature.

iN THE FUNCTION

void read( int **matr1,int **matr2,int& rows,int& cols,int& rows2,int& cols2){

What does mart1 point to?

I mean You have not Initialised The variable for the matrix anywhere.

If you want to pass an array of pointers to a function you should use: int *matr1[] (instead of int **matr1 ) for example :)

What does mart1 point to?

It points to an unknown location in memory.

To the OP:
In your main function you're declaring two pointers like this: int **matr1,**matr2; , there's nothing wrong with this line but you don't assign memory to them and then you're just passing matr1 to several functions like sumMat , subtraction , etc. without even have assigned memory to these pointers, that will definitely end in a crash :)

If you want to pass an array of pointers to a function you should use: int *matr1[] (instead of int **matr1 ) for example :)

Wouldnt int **matr1 do the trick as well?
And This is good because the size of the array is mentioned during Runtime.

int **matr1;
matr1=new int* [sizeofrows];
while
matr1[1]=new int[sizeofcol];

Wouldnt int **matr1 do the trick as well?

Well, actually it's the same, I meant that post as an advice only, it's just a matter of preference :) ...

int **matr1;
matr1=new int* [sizeofrows];
[B]while[/B]
[B]matr1[1]=new int[sizeofcol];[/B]

In case you forgot (I do NOT say you forgot, but in case you did): an array's first element is the 0th :)
By the way: what is the 'while' doing there without any condition?

Well, actually it's the same, I meant that post as an advice only, it's just a matter of preference :) ...

In case you forgot (I do NOT say you forgot, but in case you did): an array's first element is the 0th :)
By the way: what is the 'while' doing there without any condition?

Lol This is like a code battle.

Anyways That was just an example. And the while mentions a condition to be present there :)

And thanks for reminding me of the array lower bound ;)

So did anyone solve the problem and the program didn't crash with him? excuse me

I'm quoting what has been already said (cause you didn't read the thread carefully enough :():

In your main function you're declaring two pointers like this: int **matr1,**matr2; , there's nothing wrong with this line but you don't assign memory to them and then you're just passing matr1 to several functions like sumMat , subtraction , etc. without even have assigned memory to these pointers, that will definitely end in a crash :)

So what do you have to do first? Well, you've to assign some memory to both of them before you can let your functions perform operations on your matrices :)

So did anyone solve the problem and the program didn't crash with him? excuse me

Firstly in the function read1 or read2, The pointer to pointer do not point to a specific value.

That is:You have not provided a place to store the value .
When you get the rows and coloumns of your matrix. You should first create that rows and coloumns.

int **matr1;
matr1=new int* [sizeofrows];
int i=0;
while(i<sizeofrows)
matr1[i]=new int[sizeofcol];

That
After which you can read in data from the user.

In your main-function I encounter this line: while(flag==true) it's just the long way to write: while(flag)

Saving a few extra keystrokes isn't worth sacrificing code readability. If the OP finds it more understandable, it's his choice. Generally I only give advice on formatting if it's actually unreadable to other programmers, or is just very messy.

Saving a few extra keystrokes isn't worth sacrificing code readability. If the OP finds it more understandable, it's his choice. Generally I only give advice on formatting if it's actually unreadable to other programmers, or is just very messy.

Sure that it's his choice, I didn't say that he must use this way, it was only an alternative way to do it :)

This article has been dead for over six months. Start a new discussion instead.