What's wrong with this?

This is a discussion on What's wrong with this? within the C++ Programming forums, part of the General Programming Boards category; Code: #include<iostream> #include<string> using namespace std; int main(){ int a[3][3]; int b[3][3]={{0,0,0},{0,0,0},{0,0,0}}; int x1,x2,x3,y1,y2,y3,Matrica; for(int i=1;i<4;i++){ for(int j=1;j<4;j++){ cout<<"Molim unesite ...

  1. #1
    Argo Argo_Jeude's Avatar
    Join Date
    Jul 2005
    Location
    Bihać, Bosnia
    Posts
    21

    What's wrong with this?

    Code:
    #include<iostream>
    #include<string>
    using namespace std;
    int main(){
        int a[3][3];
        int b[3][3]={{0,0,0},{0,0,0},{0,0,0}};
        int x1,x2,x3,y1,y2,y3,Matrica;
        for(int i=1;i<4;i++){
                for(int j=1;j<4;j++){
                        cout<<"Molim unesite element A ["<<i<<"]["<<j<<"]"<<endl;
                        cin>>a[i][j];
                        cout<<endl;
                        }
                }
       
        for(int i=1;i<2;i++){
                for(int j=1;j<4;j++){
                        b[1][1]=b[1][1]+(a[i][j]*a[j][i]);
                        }
                }
        for(int i=1;i<2;i++){
                for(int j=1;j<4;j++){
                        b[1][2]=b[1][2]+(a[i][j]*a[j][i+1]);
                        }
                }
        for(int i=1;i<2;i++){
                for(int j=1;j<4;j++){
                        b[1][3]=b[1][3]+(a[i][j]*a[j][i+2]);
                        }
                }
                
        for(int i=2;i<3;i++){
                for(int j=1;j<4;j++){
                        b[2][1]=b[2][1]+(a[i][j]*a[j][i-1]);
                        }
                }
        for(int i=2;i<3;i++){
                for(int j=1;j<4;j++){
                        b[2][2]=b[2][2]+(a[i][j]*a[j][i]);
                        }
                }
        b[2][3]=0;
        for(int i=2;i<3;i++){
                for(int j=1;j<4;j++){
                        b[2][3]=b[2][3]+(a[i][j]*a[j][i+1]);
                        }
                }
        b[3][1]=0;
        for(int i=3;i<4;i++){
                for(int j=1;j<4;j++){
                        b[3][1]=b[3][1]+(a[i][j]*a[j][i-2]);
                        }
                }
        b[3][2]=0;
        for(int i=3;i<4;i++){
                for(int j=1;j<4;j++){
                        b[3][2]=b[3][2]+(a[i][j]*a[j][i-1]);
                        }
                }
        b[3][3]=0;
        for(int i=3;i<4;i++){
                for(int j=1;j<4;j++){
                        b[3][3]=b[3][3]+(a[i][j]*a[j][i]);
                        }
                }
        
        x1=b[1][1]*b[2][2]*b[3][3];
        x2=b[1][2]*b[2][3]*b[3][2];
        x3=b[1][3]*b[2][2]*b[3][3];
        y1=-(b[1][3]*b[2][2]*b[3][1]);
        y2=-(b[1][2]*b[2][3]*b[3][2]);
        y3=-(b[1][3]*b[2][2]*b[3][3]);
        Matrica=x1+x2+x3+(y1)+(y2)+(y3);
        cout<<endl<<Matrica<<endl;
        if(Matrica>0){
                      cout<<"    "<<b[1][2]<<"    "<<b[1][3]<<endl;
                      cout<<"           "<<b[2][3]<<endl;
                      cout<<"                  "<<endl;
                      }
        else if(Matrica<0){
             cout<<"                 "<<endl;
             cout<<b[2][1]<<"          "<<endl;
             cout<<b[3][1]<<" "<<b[3][2]<<"    "<<endl;
             }
        else if(Matrica==0){
             cout<<endl<<b[1][1]<<"          "<<endl;
             cout<<"    "<<b[2][2]<<"    "<<endl;
             cout<<"          "<<b[3][3]<<endl;
             }
        system("pause");
        return 0;
    }
    This one works...


    But this one...


    Code:
    #include<iostream>
    #include<string>
    using namespace std;
    int main(){
        int a[3][3];
        int b[3][3]={{0,0,0},{0,0,0},{0,0,0}};
        int x1,x2,x3,y1,y2,y3,Matrica;
        for(int i=1;i<4;i++){
                for(int j=1;j<4;j++){
                        cout<<"Molim unesite element A ["<<i<<"]["<<j<<"]"<<endl;
                        cin>>a[i][j];
                        cout<<endl;
                        }
                }
       
        for(int i=1;i<2;i++){
                for(int j=1;j<4;j++){
                        b[1][1]=b[1][1]+(a[i][j]*a[j][i]);
                        }
                }
        for(int i=1;i<2;i++){
                for(int j=1;j<4;j++){
                        b[1][2]=b[1][2]+(a[i][j]*a[j][i+1]);
                        }
                }
        for(int i=1;i<2;i++){
                for(int j=1;j<4;j++){
                        b[1][3]=b[1][3]+(a[i][j]*a[j][i+2]);
                        }
                }
                
        for(int i=2;i<3;i++){
                for(int j=1;j<4;j++){
                        b[2][1]=b[2][1]+(a[i][j]*a[j][i-1]);
                        }
                }
        for(int i=2;i<3;i++){
                for(int j=1;j<4;j++){
                        b[2][2]=b[2][2]+(a[i][j]*a[j][i]);
                        }
                }
      
        for(int i=2;i<3;i++){
                for(int j=1;j<4;j++){
                        b[2][3]=b[2][3]+(a[i][j]*a[j][i+1]);
                        }
                }
    
        for(int i=3;i<4;i++){
                for(int j=1;j<4;j++){
                        b[3][1]=b[3][1]+(a[i][j]*a[j][i-2]);
                        }
                }
     
        for(int i=3;i<4;i++){
                for(int j=1;j<4;j++){
                        b[3][2]=b[3][2]+(a[i][j]*a[j][i-1]);
                        }
                }
    
        for(int i=3;i<4;i++){
                for(int j=1;j<4;j++){
                        b[3][3]=b[3][3]+(a[i][j]*a[j][i]);
                        }
                }
        
        x1=b[1][1]*b[2][2]*b[3][3];
        x2=b[1][2]*b[2][3]*b[3][2];
        x3=b[1][3]*b[2][2]*b[3][3];
        y1=-(b[1][3]*b[2][2]*b[3][1]);
        y2=-(b[1][2]*b[2][3]*b[3][2]);
        y3=-(b[1][3]*b[2][2]*b[3][3]);
        Matrica=x1+x2+x3+(y1)+(y2)+(y3);
        cout<<endl<<Matrica<<endl;
        if(Matrica>0){
                      cout<<"    "<<b[1][2]<<"    "<<b[1][3]<<endl;
                      cout<<"           "<<b[2][3]<<endl;
                      cout<<"                  "<<endl;
                      }
        else if(Matrica<0){
             cout<<"                 "<<endl;
             cout<<b[2][1]<<"          "<<endl;
             cout<<b[3][1]<<" "<<b[3][2]<<"    "<<endl;
             }
        else if(Matrica==0){
             cout<<endl<<b[1][1]<<"          "<<endl;
             cout<<"    "<<b[2][2]<<"    "<<endl;
             cout<<"          "<<b[3][3]<<endl;
             }
        system("pause");
        return 0;
    }
    Notice that in the second example there is no seting the b[][] again to zero.
    The program gets a matrix (?)with elements that user enters.
    Than it multiplay's(?) that matrix with itself.
    Matriux on squaire.
    Than it gets the resyult of the matrix and if the value of matrix is bigger than 0, it displays the upper triangle elements of the matrix,
    for example:
    2 3
    1

    -if it's smaller,it displays the lower triangle,exmpl.:

    2
    2 4

    -and if the result of the matrix is equal to zero(0) than it displays the diagonal values,ex.:


    2
    3
    7


    But somethings wrong with that b[2][3]and all next becouse they get starting value of couple of millions rather than the value that I set them, and that is zero,0.

    I solve the problem but with sting again every b after [2][2].

    Why?
    Thanx in advace!

  2. #2
    Registered User
    Join Date
    Jan 2006
    Location
    Europe/Belgrade
    Posts
    78
    As I can see, you use indexes out of range. Arrays are indexed from 0 not from 1.

    Pozdrav iz Beograda.

  3. #3
    Argo Argo_Jeude's Avatar
    Join Date
    Jul 2005
    Location
    Bihać, Bosnia
    Posts
    21
    As I can see, you use indexes out of range. Arrays are indexed from 0 not from 1.
    Dobro šta trebam uraditi onda?
    Početnik sam.

  4. #4
    Registered User
    Join Date
    Jan 2006
    Location
    Europe/Belgrade
    Posts
    78
    Let's stay on english, because of others.
    First thing I noticed are loops that start from 1 instead from 0. Each array in C/C++ is indexed from 0 to length-1. For example, first loop that fills A has to be indexed from 0 to 3 instead from 1 to 4. This notice is for each loop in the code. Try to fix this first, then you can see is there more problems.
    Regards.

    Imaš dobar forum na našem jeziku, www.elitesecurity.org, pa dođi ako te mrzi da pišeš na engleskom.
    Pozdrav!

    P.S.
    Je l' to Mirjana Joković na slici?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2004, 04:30 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 10:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 01:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 02:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21