Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,105 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,699 people online right now. Registration is fast and FREE... Join Now!




matrices

 
Reply to this topicStart new topic

matrices

celticprincezz
16 Oct, 2007 - 03:36 PM
Post #1

New D.I.C Head
*

Joined: 23 Sep, 2007
Posts: 4


My Contributions
CODE

//Heather McMahon

#include <iostream>
using namespace std;

void printarray(int** q, int r, int s) {
    int o, p;
    for (o=0; o<r; o++) {
        for(p=0; p<s; p++) {
            cout << q[o][p] << " ";
        }
        cout <<endl;
    }
}

void modifymatrix(int** b) {
    b[3][3] = 7;
    return;
}

double** transpose(double** c, int o) {
    double** trans;
    trans = new double* [o];
    int d;
    for(d=0; d<o; d++) {
        trans[d] = new double[o];
    }
    return trans;
}

int main()
{
    float A[3][3], B[3][3], C[3][3]; //the three matrices
    double X[3][3]; //the inverse
    int option; //to input your option from the menu
    int i, j, k, l, m, n;
    float h = 0;
    cout<<"Which option would you like to choose?"<<endl;
cout<<"0. Quit "<<endl;
cout<<"1.Input Matrix"<<endl;
cout<<"2.Print Matrix"<<endl;
cout<<"3.Trace "<<endl;
cout<<"4. Multiply "<<endl;
cout<<"5. Add "<<endl;
cout<<"6. Invert "<<endl;
cout<<"7. Determinant "<<endl;
cout<<"8. Transpose "<<endl;
cout<<"Enter your option";
cin>>option;
while (option > 0)
{
    if (option = 1) {
        cout<<"Which Matrix would like to input the values for, 1.matrix A, 2.matrix B, 3.matrix C?";
        int input;
        cin>>input;
            if (input = 1) {
            cout<<"Enter the values of Matrix A";
        for(i=0;i<3;i++)
        {    cout<<endl;
        for(j=0;j<3;j++)
        {
            cout<<" A["<<i<<"]["<<j<<"]= ";
        cin>>A[i][j];
          }
     }
        }
        else if (input = 2) {
            cout<<"Enter the values of Matrix B";
        for(k=0;k<3;k++)
        {    cout<<endl;
        for(l=0;l<3;l++)
        {
            cout<<" B["<<k<<"]["<<l<<"]= ";
        cin>>B[k][l];
          }
        }
        }
        else  {
            cout<<"Enter the values of Matrix C";
        for(m=0;m<3;m++)
        {    cout<<endl;
        for(n=0;n<3;n++)
        {
            cout<<" C["<<m<<"]["<<n<<"]= ";
        cin>>C[m][n];
          }
        }
    }
    }
else if (option = 2){
        cout<<"Which matrix would you like to print, 1.Matrix A 2. Matrix B 3. Matrix C?";
    int print;
    if (print = 1) {
     for(i=0;i<3;i++)
          cout<<endl;
          for(j=0;j<3;j++)  
               cout<<" A["<<i<<"]["<<j<<"]= "<<A[i][j];
    }
    else if (print = 2){
     for(k=0;k<3;k++)
          cout<<endl;
          for(l=0;l<3;l++)  
               cout<<" B["<<k<<"]["<<l<<"]= "<<B[k][l];
    }
    else {
     for(m=0;m<3;m++)
          cout<<endl;
          for(n=0;n<3;n++)  
               cout<<" C["<<m<<"]["<<n<<"]= "<<C[m][n];
    }
}
else if (option = 3){
     A[i][j]= A[1][1] + A[2][2] + A[3][3];
     B[k][l] = B[1][1] + B[2][2] + B[3][3];
     C[m][n] = C[1][1] + C[2][2] + C[3][3];
}
else if (option = 4){
    for(i=0;i<3;i++)
          for(j=0;j<3;j++)
          for(k=0;k<3;k++)
          C[m][n] =A[i][j]*B[k][l];
}

else if (option = 5){
    for(i=0;i<3;i++)
    for(j=0;j<3;j++)
          C[i][j] = A[i][j] + B[i][j];
}  
else if (option = 6){
    cout<<"The inverse of Matrix A:";
      for(i=0;i<3;i++)
     {     cout<<endl;
          for(j=0;j<3;j++)
          {    
               cout<<" X["<<i<<"]["<<j<<"]= "<<X[i][j];
          
          }
     }
     cout<<endl<<endl;
}

else if (option = 7){
    if(h!=0) {
            A[i][j]=1/h;
        cout<<"The determinant of Matrix A is:";
}
    else if(h!=0) {
            B[i][j]=1/h;
            cout<<"The determinant of Matrix B is:";
}
else  {
            B[i][j]=1/h;
        cout<<"The determinant of Matrix C is:";
}
}
else {
        cout<<"The transpose of Matrix A is:";
        for(i=0;i<3;i++)
     {
          cout<<endl;
          for(j=0;j<3;j++)
          {    
                    
               B[i][j]=A[j][i];
               cout<<" B["<<i<<"]["<<j<<"]= "<<B[i][j];
              
          }
     }
     cout<<endl<<endl;


     C[0][0]=B[1][1]*B[2][2]-(B[2][1]*B[1][2]);
     C[0][1]=(-1)*(B[1][0]*B[2][2]-(B[2][0]*B[1][2]));
     C[0][2]=B[1][0]*B[2][1]-(B[2][0]*B[1][1]);
    
     C[1][0]=(-1)*(B[0][1]*B[2][2]-B[2][1]*B[0][2]);
     C[1][1]=B[0][0]*B[2][2]-B[2][0]*B[0][2];
     C[1][2]=(-1)*(B[0][0]*B[2][1]-B[2][0]*B[0][1]);

     C[2][0]=B[0][1]*B[1][2]-B[1][1]*B[0][2];
     C[2][1]=(-1)*(B[0][0]*B[1][2]-B[1][0]*B[0][2]);
     C[2][2]=B[0][0]*B[1][1]-B[1][0]*B[0][1];
}

for(i=0;i<3;i++){
    for(j=0;j<3;j++){
cout<<C[i][j];
    }
}
}
return 0;
}





I have this code to where I believe it is easy to read and know what to do with just the menu but basically my question is that for the while (option) statement and the if statements, everytime I input an option number, say, 4, it is only bringing up the first option to input no matter what then the program terminates. And even if I do put my option to be 1 to input the matrices, it will tell me to input matrices A, B, and C where as I only want to input one. This program is giving me a headache!! Hopefully someone can help? blink.gif


******EDIT****** Never mind, I finally figured out what I was doing wrong. But now maybe can someone check over my code and give me guidelines as to making it actually work perfectly? Now when I input an option it goes to that option, but as far as option 1 when I need to input the matrices, it is supposed to store it for later use. So just now I did option 1 and said I wanted to change matrix C well it did continuing loop for that and after I inputed the values for that it asked me again what matrix would I like to input the values for. Well I don't want to do that.

Also, does anyone know how to first initialize the matrices to the all-zero nxn matrix in the beginning and you set the dimensions?

This post has been edited by celticprincezz: 16 Oct, 2007 - 03:43 PM
User is offlineProfile CardPM
+Quote Post

jjhaag
RE: Matrices
16 Oct, 2007 - 03:54 PM
Post #2

me editor am smartastic
Group Icon

Joined: 18 Sep, 2007
Posts: 1,789



Thanked: 2 times
Dream Kudos: 775
Expert In: C,C++

My Contributions
QUOTE(celticprincezz @ 16 Oct, 2007 - 05:36 PM) *

Also, does anyone know how to first initialize the matrices to the all-zero nxn matrix in the beginning and you set the dimensions?

Not without doing a bunch of looping, and then I guess you're not really "initializing" so much as just assigning.

You may want to try using an STL container like a vector instead of just raw arrays; they can be nested to produces matrices, and you wouldn't have to worry about explicit memory allocation if you want to resize them or anything. They can also be easily initialized to a single value.

-jjh
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 09:10PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month