Results 1 to 3 of 3

Thread: Two dimensional array - Have to print out smallest number

  1. #1
    Join Date
    Dec 2011
    Posts
    19
    Mentioned
    0 Post(s)
    Quoted
    3 Post(s)

    Default Two dimensional array - Have to print out smallest number

    Pay attention to line: 33. Script is supposed to print out smallest number in two dimensional array. It doesnt work - how can i fix it?

    Code:
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    using namespace std;
    int main ()
    {
    int i, j, min = 0, ri = 0, ki = 0, m[4][4];
    double s = 0;
    bool highest;
    
    srand ( time(NULL) );
    
    for (i=0; i<4; i++){
        for (j=0; j<4; j++) 
             m[i][j] = (rand() % 50 + 1);
    }
    
        cout <<"Elements:" <<endl;
        
    for (i=0; i<4; i++){
        for (j=0; j<4; j++) 
           cout<< m[i][j] <<" ";
           cout <<endl;
    }
    
    if (highest == true){
          min = 51;  
    } 
        
        
    for (i=0; i<4; i++){
        for (j=0; j<4; j++) 
            if((min < m[i][j]) == highest){
                   min = m[i][j];
                   ri = i;
                   ki = j;
            }
    }
    
            cout << "Smallest number " <<min <<" and its on " <<ri <<" row and " <<ki <<" column" <<endl;
    
    for (j=0; j<4; j++) 
        s = (s + m[0][j]);
        cout << "0. row avarage number " << s/4 <<endl;
        system ("pause");
    return 0;
    
    }

  2. #2
    Join Date
    Oct 2006
    Location
    Netherlands
    Posts
    3,285
    Mentioned
    105 Post(s)
    Quoted
    494 Post(s)

    Default

    c++ Code:
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    using namespace std;
    int main ()
    {
    int i, j, min = 0, ri = 0, ki = 0, m[4][4];
    double s = 0;
    bool highest = false;

    srand ( time(NULL) );

    for (i=0; i<4; i++){
        for (j=0; j<4; j++)
             m[i][j] = (rand() % 50 + 1);
    }

        cout <<"Elements:" <<endl;
       
    for (i=0; i<4; i++){
        for (j=0; j<4; j++)
           cout<< m[i][j] <<" ";
        cout <<endl;
    }

    if (highest == false){  //changed to false.
          min = 51;  
    }
       
       
    for (i=0; i<4; i++){
        for (j=0; j<4; j++)
            if((min < m[i][j]) == highest){
                   min = m[i][j];
                   ri = i;
                   ki = j;
            }
    }

            cout << "Smallest number " <<min <<" and its on " <<ri <<" row and " <<ki <<" column" <<endl;

    for (j=0; j<4; j++)
        s = (s + m[0][j]);
        cout << "0. row avarage number " << s/4 <<endl;
        system ("pause");
    return 0;

    }

    In my head it should work like this.
    Working on: Tithe Farmer

  3. #3
    Join Date
    Dec 2011
    Posts
    19
    Mentioned
    0 Post(s)
    Quoted
    3 Post(s)

    Default

    Yes works thank you

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •