Thread: Calculation problem....

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    4

    Calculation problem....

    I have this problem.... when running this code , I get strange results.. unexpected ones... I cant seam to figure this one out ( I am still learning C++ )[actualy just started out]
    Code:
    #include <cstdlib>
    #include <iostream>
    #include <math.h>
    
    using namespace std;
    
    void main()
    {
    int m=4 , n=4;
    
    int a[m][n] , min , max , b[m][2] ,c[n][2];
    int r, k ,i ,j , mini=0, minj=0, maxi=0, maxj=0;
    do
    {
    cout<<"Do you want a random matrix [0/1]?";
    cin>>r;
    } while ((r!=0)&&(r!=1));
    cout<<endl<<endl;
    if (r==0) { //manual matrix
       for (i=0;i<m;i++){
           for(j=0;j<n;j++){
                            cout<<"enter element "<<i<<" "<<j<<endl;
                            cin>>a[i][j];
                            cout<<endl;
                            }}}
    else { //PC generated
       for ( i = 0 ; i < m ; i++ ){
           for( j = 0 ; j < n ; j++ ){
                            a[i][j]= rand()/1000;
                            }}}
    //find max...
    
    for (j=0;j<n;j++){
                      max = a[0][j]; //take 1st elem of row....
                      for ( i = 0 ; i < m ; i++ ){
                       if (max < a[j][i]){
                           max = a[j][i];
                           //set a list of maximums in matrix b
                           b[j][0] = max;
                           b[j][1] = i;
                           }
                      }
    }
    //find min...
       
    for (i=0;i<m;i++){
                      min = a[i][0]; //take 1st elem of col....   
                      for ( j = 0 ; j < n ; j++ ){
                          if (min > a[j][i] ){
                             min = a[j][i];
                             //set a list of minimums in matrix c
                             c[i][0]= min;
                             c[i][1]= j;
                             }
                          }
    }
           
    for (i = 0 ; i < m ; i++)
        {
        for(j=0;j<n;j++){
                         cout<<a[i][j]<<"\t";
                         }
        cout<<endl<<endl;
        }
    cout<<"\t"<<"Elem"<<"\t"<<"col"<<"\t"<<"row"<<endl;  //table headings
    cout<<"Max [b]---------------------"<<endl;
    for ( k = 0 ; k < m ; k++){
        cout<<"\t"<<b[k][0]<<"\t"<<(k+1)<<"\t"<<(b[k][1]+1)<<endl;
        }
    cout<<"Min [c]---------------------"<<endl;
    for (j=0;j<n;j++){
        cout<<"\t"<<c[j][0]<<"\t"<<c[j][1]+1<<"\t"<<(j+1)<<endl;
        }
    }

  2. #2
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    1 void main() change this to int main ( void )

    2 Is that all the code? Where is the return 0?

    3 What errors are you getting? What should the program produce as an output?

    4 Your indentation is a little off - what IDE are you using?

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    4
    I did fixed # 1 & 2 ...

    as for 4 .. I am using dev-c++ .

    Code:
    E:\C++\prog3>main2.exe
    Do you want a random matrix [0/1]?1
    
    
    0       18      6       26
    
    19      15      11      29
    
    26      24      5       28
    
    23      16      9       0
    
            Elem    col     row
    Max [b]---------------------
            26      1       4
            29      2       4
            28      3       4
            4       4       4199256
    Min [c]---------------------
            2009288233      1       1
            15      2       2
            5       3       3
            0       4       4
    
    E:\C++\prog3>
    the parts in red are the strange results ....

  4. #4
    Registered User
    Join Date
    Jun 2006
    Posts
    29
    int a[m][n]
    that doesn't seem to make any sense. you dont need it, as much as i understand, it won't even compile. anyway, usually rand() is used with modulus (%) and the highest number you will accept+1. then it may return 0 to N-1.
    Last edited by gftmc; 10-16-2006 at 08:14 AM.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    4

    Post

    I have fixed the program... it now works....

    I givenup on arrays.. and used a couple of vectors... and fixed up the code in the loops... cleaned things up a bit too..
    Code:
    #include <cstdlib>
    #include <iostream>
    #include <math.h>
    #include <vector>
    
    using namespace std;
    
    int main(void){
    
    int m=4 , n=4;
    int val , x,y;
    int r, k ,i ,j ;
    vector<int> B1,C1;
    int a[m][n];
    
    
    do{
    cout<<"Do you want a random matrix [0/1]?";
    cin>>r;
    } while ((r!=0)&&(r!=1));
    cout<<endl<<endl;
    if (r==0) { //manual matrix
    for (i=0;i<m;i++){
        for(j=0;j<n;j++){
                         cout<<"enter element "<<i<<" "<<j<<endl;
                         cin>>a[i][j];
                         cout<<endl;
                         }}}
                         else { //PC generated
                         for ( i = 0 ; i < m ; i++ ){
                             for( j = 0 ; j < n ; j++ ){
                                  a[i][j]= rand()/1000;
                                  }}}
    
    
    for (i = 0 ; i < m ; i++)//output the matrix
        {
        for(j=0;j<n;j++){
                         cout<<a[i][j]<<"\t";
                         }
        cout<<endl<<endl;
        }
                            
    cout<<"\t\t\t\t"<<"Elem"<<endl;  //table headings                        
    //find max...
    for (j=0;j<n;j++){x=0;y=0;
                      val = a[0][j]; //take 1st elem of row....
                      for ( i = 1 ; i < m ; i++ ){
                          x++;
                          if (val < a[i][j])
                             {
                                  val = a[i][j];
                                  y=x;
                                  }
                      }
                      B1.push_back(y);
    }
    cout<<"Max [b]---------------------"<<endl;//output
    for ( k = 0 ; k < m ; k++){
        cout<<" row of max in col " << k+1 << " is " << B1[k]+1 <<"\t"<<a[B1[k]][k]<<endl;
        }
    //find min...
    for (i=0;i<m;i++){x=0;y=0;
                      val = a[i][0]; //take 1st elem of col.... 
                      for ( j = 1 ; j < n ; j++ ){
                          x++;
                          if (val > a[i][j])
                             {
                                  val = a[i][j];
                                  y=x;
                                  }
                          }
                          C1.push_back(y);
    }
    cout<<endl<<endl;
    cout<<"Min [c]---------------------"<<endl;//output
    for (j=0;j<n;j++){
        cout<<" Col of Min in row "<< j+1 << " is "<< C1[j]+1 <<"\t"<<a[j][C1[j]] <<endl;
        }
    cout<<endl<<endl;       
    
    //comp engine.....
    cout<<"-------------------------------------------"<<endl;
    for (i=0;i<m;i++){
        if (C1[B1[i]]==i){
                          cout<<"Elem of row "<<B1[i]+1<<" and col "<<i+1<<" is max/min"<<endl;
                          }}
    //enter val to exit
    cin>>r;
        return 0;
    }
    Code:
    Do you want a random matrix [0/1]?1
    
    
    0       18      6       26
    
    19      15      11      29
    
    26      24      5       28
    
    23      16      9       0
    
                                    Elem
    Max [b]---------------------
     row of max in col 1 is 3       26
     row of max in col 2 is 3       24
     row of max in col 3 is 2       11
     row of max in col 4 is 2       29
    
    
    Min [c]---------------------
     Col of Min in row 1 is 1       0
     Col of Min in row 2 is 3       11
     Col of Min in row 3 is 3       5
     Col of Min in row 4 is 4       0
    
    
    -------------------------------------------
    Elem of row 2 and col 3 is max/min

  6. #6
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    If you find arrays challenging and they can be, read up on them and keep practicing. Nothing in this language is "easy". Vectors are simular to arrays, they can grow and shrink as you need to. Perhaps somthing you can do later is come back to this when you have more knowledge on arrays and attempt it again,
    Then you know you have learnt arrays better and know there machanics more than you did the first time around

  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    4
    Quote Originally Posted by swgh
    If you find arrays challenging and they can be, read up on them and keep practicing. Nothing in this language is "easy". Vectors are simular to arrays, they can grow and shrink as you need to. Perhaps somthing you can do later is come back to this when you have more knowledge on arrays and attempt it again,
    Then you know you have learnt arrays better and know there machanics more than you did the first time around
    sure thing... I am still learning this language... I will read up on the subjects in need... I still need to work on my skills...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  2. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  3. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  4. Trigonometry Sin rule calculation problem
    By Harry in forum C++ Programming
    Replies: 3
    Last Post: 02-06-2006, 12:14 PM
  5. beginner problem
    By The_Nymph in forum C Programming
    Replies: 4
    Last Post: 03-05-2002, 05:46 PM