Click here to Skip to main content
15,921,990 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
this below represents the main of my class Matrix source code.from my main i want to do mathemetical operations such as add ,subtract, multiple, IF the operation was any of the previous mention then 2 or more matrices are needed, if i am doing inverse or determinant, then only i matrix is needed.My question is , how may i restructure this code to request the user to determine operation then request size and elements of the needed matrix??
as is it will request the matrix dimention request elements and crash
I tried to prompt the user to enter the dimention of the matrix to be worked on, and thereafter enter the specific elements,
what happend next it BOOM!!!! it crashes, WHY?
IF I compiles and run my main independent of my "class Matrix" it executes, would some error in my class contribute?


  cout<<"\nTHIS ASSIGNMENT IS TO COVER THE FOLLOWING: "<<endl;
      cout<<"\n                         > Find and displaying Determinant"<<endl;
      cout<<"\n                         > Find and displaying the Inverse"<<endl;
      cout<<"\n                           matrix"<<endl;
      cout<<"\n                         > Carrying out row transformation"<<endl;
      cout<<"\n                         > Addition & Subtraction"<<endl;
      cout<<"\n                         > Multiplicationn & division"<<endl;
      cout<<"\n"<<endl;
    cout<<"\n1.Add matrices\n2.Subtract matrices\n3.Multiplymatrices\n4.Find determinant\n5.Find INverse"<<endl;
    cout<<"Select any one above , then hit ENTER"<<endl;
    cin>>x;
    if(x==1)
    {
    cout<<"enter the dimension of matrix A:";
     Matrix A = Matrix(rows,cols);
     cin>>rows ;
    cout<<"enter cols:";
    cin>>cols ;
    //cout<<rows<<cols;


    cout<<"enter the elements of matrix A:";
    for (int r = 0; r < rows; r++)
     { for (int c = 0; c < cols; c++)
       {
        cin>>A(r,c);
       }
     }
    cout<<"enter the dimension of matrix B:";
    Matrix B = Matrix(cols, rows);
    cout<<"enter the elements of matrix B:";
    for (int r=0;r<rows;r++)
     { for (int c=0;c<cols;c++)
       {
        cin>>B(r,c);
       }
     }
     Matrix C;
    C = A + B;
    printf("A + B = \n");
    C.Print();
    printf("\n");
    }
    else if(x==2)
  {  cout<<"enter the dimension of matrix A:";
    cin>>rows>>cols;
    Matrix A = Matrix(cols, rows);
    cout<<"enter the elements of matrix A:";
    for (int r=0;r<rows;r++)
     { for (int c=0;c<cols;c++)
       {
        cin>>A(r,c);
       }
     }


    Matrix B = Matrix(cols, rows);
    cout<<"enter the elements of matrix B:";
    for (int r=0;r<rows;r++)
     { for (int c=0;c<cols;c++)
       {
        cin>>B(r,c);
       }
     }
    Matrix C;
     C = A - B;
    printf("A - B = \n");
    C.Print();
    printf("\n");
  }
   else if (x==3)
   {
     cout<<"enter the dimension of matrix A:";
    cin>>rows>>cols;
    Matrix A = Matrix(cols, rows);
    cout<<"enter the elements of matrix A:";
    for (int r=0;r<rows;r++)
     { for (int c=0;c<cols;c++)
       {
        cin>>A(r,c);
       }
     }


    Matrix B = Matrix(cols, rows);
    cout<<"enter the elements of matrix B:";
    for (int r=0;r<rows;r++)
     { for (int c=0;c<cols;c++)
       {
        cin>>B(r,c);
       }
     }
     Matrix C;
     C = A * B;
    printf("A * B2 = \n");
    C.Print();
    printf("\n");
    }
    else if (x==4)
    {
    cout<<"enter the dimension of matrix for which you want to find its det:";
    cin>>rows>>cols;
    Matrix A = Matrix(cols, rows);
    cout<<"enter the elements of matrix A:";
    for (int r=0;r<rows;r++)
     { for (int c=0;c<cols;c++)
       {
        cin>>A(r,c);
       }
     }
    printf("A = \n");
    A.Print();
    printf("Det(A) = %f\n\n", Det(A));
    }
    else if(x==5)
    {
    cout<<"enter the dimension of matrix A:";
    cin>>rows>>cols;
    Matrix A = Matrix(cols, rows);
    cout<<"enter the elements of matrix A:";
    for (int r=0;r<rows;r++)
     { for (int c=0;c<cols;c++)
       {
        cin>>A(r,c);
       }
     }
    Matrix A_inv = Inv(A);
    printf("Inv(A) = \n");
    A_inv.Print();
    printf("\n");
    }
    else


    cout<<"wrong choice";


   /* rows = 2;
    cols = 5;
    Matrix H = Matrix(rows, cols);
    for (int r = 1; r <= rows; r++)
    {
      for (int c = 1; c <= cols; c++)
      {
        count ++;
        H(r, c) = count;
      }
    }
    printf("H = \n");
    H.Print();
    printf("\n");*/
    cout<<"\nContinue(y/n)";
  cin>>ch;
  } while (ch=='y'||ch=='Y');
   cin.get();






  PAUSE;


  getch ();
}
Posted
Updated 5-Dec-11 2:04am
v2

Well, it crashes because you:
  • First build the matrix.
  • Then ask the user for matrix dimensions.

this is a logical error, you have to do the opposite.
 
Share this answer
 
Comments
Albert Holguin 5-Dec-11 9:25am    
Good catch....+5 (I didn't want to look through the code dump!)
You should spend some time learning more about definitions, loops, functions, inheritance etc, to save duplicating so much code. Any changes you make in one part of the above is likely to break other parts.
 
Share this answer
 
Some hints:

1. When you require input of more than a handful of values, use files! It's much easier to handle for the user, especially if he wants to rerun the program with slight adjustments. It's also much easier to spot and fix input mistakes. And, most importantly, it's much easier to keep track of what input refers to what value, once you know what's expected.

Also it's easier for the programmer to read all there is into memory and analyze whether or not everything is like it's expected to be.

Note that prompting users for specific input can be confusing at times, especially when you ask for multiple values at once. E. g. when you ask for the elements of a matrix, should they be entered row by row or column by column? The answer may appear obvious to you, but I can assure you there are others who may not share your interpretation.

2. If you want your program to be interactive, then maybe just ask the user to store matrix values for each matrix in individual files and ask for the file names instead of the values. You could also store the results in a file. That way, input matrices and results can be reused for further calculations.

3. Start with 2x2 matrices, and don't bother asking about dimensions for now. You could even use fixed size data structures if you like. The point is: get the program to work! Once you achieved that, you can revisit your code and consider what you need to change to make it work with other dimensions. On a sidenote: remember that many matrix operations only work for square matrices (determinant, transposition, inverse, and - to some degree - multiplication), so it would help a lot to restrict yourself to this type and not ask for two dimensions - just one will do!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900