Finding lowest/highest values in 2D array help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Alberto Fortuny
    New Member
    • Nov 2010
    • 5

    Finding lowest/highest values in 2D array help

    Hey guys, any idea as to how to find the lowest value and the highest value in this 2D array? I made the functions to find the lowest and highest, but all im getting on execution is a 1 on both, no matter what number i input.

    Here's my code
    Code:
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    const int MONKEYS = 3;
    const int DAYS = 7;
    double average = 0;
    
    int getSum (int [][DAYS]);
    void getChart (int [][DAYS]);
    void dayAvg (int table [][DAYS]);
    int findLowest (int table [][DAYS]);
    int findHighest (int table [][DAYS]);
    
    int main(int argc, char *argv[])
    {
        int table [MONKEYS][DAYS];
        
        cout << "The diet of 3 monkeys over 7 days, please input your info: \n\n";
        
        getChart(table);
        
        cout << "The total amount of food consumed by the monkeys: "
             << getSum(table) << " Pounds\n\n";
             
        dayAvg(table);
        
        cout << "The lowest amount of food eaten was: " << findLowest(table) << " Pounds\n";
        cout << "The highest amount of food eaten was: " << findHighest(table) << " Pounds\n";
        
        //ofstream outData;
        //outData.open("results.txt");
        
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    
    int getSum (int table [][DAYS])
    {
        int sum = 0;
        for ( int monkey = 0;  monkey < MONKEYS; monkey++)
        {
            for (int day = 0; day < DAYS; day++)
                sum += table [monkey][day];
        }
        return sum;
    }
    
    void getChart (int table[][DAYS])
    {
        for (int monkey = 0; monkey < MONKEYS; monkey++)
        {
            for (int day = 0; day < DAYS; day++)
            {
                cout << "Monkey " << (monkey+1) << ", ";
                cout << "Day " << (day+1) << ": ";
                cin >> table [monkey][day];
            }
            cout << endl;
        }
    }
    
    void dayAvg (int table [][DAYS])
    {
        for ( int day = 0;  day < DAYS; day++)
        {
            int total = 0;
            for (int monkey = 0; monkey < MONKEYS; monkey++)
            {
                total += table [monkey][day];
            }
            average = total/MONKEYS;
            cout << "Average food consumed on day " << (day+1)
                 << " by all 3 monkeys is: " << average << " Pounds" << endl;
        }
    }
    
    int findLowest (int table [][DAYS])
    {
        int lowest = table [MONKEYS][DAYS];
        for (int monkey = 0; monkey < MONKEYS; monkey++)
        {
            for (int day = 0; day < DAYS; day++)
            {
                if (table [monkey][day] < lowest)
                    lowest = table[MONKEYS][DAYS];
            }
            
            cout << endl;
        }
        return lowest;
    }
    
    int findHighest (int table [][DAYS])
    {
        int highest = table [MONKEYS][DAYS];
        for (int monkey = 0; monkey < MONKEYS; monkey++)
        {
            for (int day = 0; day < DAYS; day++)
            {
                if (table [monkey][day] > highest)
                    highest = table[MONKEYS][DAYS];
            }
            
            cout << endl;
        }
        return highest;
    }
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Line 87 and 103 have the array indexes in the wrong case.

    Comment

    • Alberto Fortuny
      New Member
      • Nov 2010
      • 5

      #3
      Hmm ok well changing the case in line 103 for the findHighest function to [monkey][day] worked, but it did not work for the findLowest function. Any ideas why?

      Here's my code again just in case:
      Code:
      #include <cstdlib>
      #include <iostream>
      
      using namespace std;
      
      const int MONKEYS = 3;
      const int DAYS = 7;
      double average = 0;
      
      int getSum (int [][DAYS]);
      void getChart (int [][DAYS]);
      void dayAvg (int table [][DAYS]);
      int findLowest (int table [][DAYS]);
      int findHighest (int table [][DAYS]);
      
      int main(int argc, char *argv[])
      {
          int table [MONKEYS][DAYS];
          
          cout << "The diet of 3 monkeys over 7 days, please input your info: \n\n";
          
          getChart(table);
          
          cout << "The total amount of food consumed by the monkeys: "
               << getSum(table) << " Pounds\n\n";
               
          dayAvg(table);
          
          cout << "The lowest amount of food eaten : " << findLowest(table) << " Pounds\n";
          cout << "The highest amount of food eaten : " << findHighest(table) << " Pounds\n";
          
          //ofstream outData;
          //outData.open("results.txt");
          
          system("PAUSE");
          return EXIT_SUCCESS;
      }
      
      int getSum (int table [][DAYS])
      {
          int sum = 0;
          for ( int monkey = 0;  monkey < MONKEYS; monkey++)
          {
              for (int day = 0; day < DAYS; day++)
                  sum += table [monkey][day];
          }
          return sum;
      }
      
      void getChart (int table[][DAYS])
      {
          for (int monkey = 0; monkey < MONKEYS; monkey++)
          {
              for (int day = 0; day < DAYS; day++)
              {
                  cout << "Monkey " << (monkey+1) << ", ";
                  cout << "Day " << (day+1) << ": ";
                  cin >> table [monkey][day];
              }
              cout << endl;
          }
      }
      
      void dayAvg (int table [][DAYS])
      {
          for ( int day = 0;  day < DAYS; day++)
          {
              int total = 0;
              for (int monkey = 0; monkey < MONKEYS; monkey++)
              {
                  total += table [monkey][day];
              }
              average = total/MONKEYS;
              cout << "Average food consumed on day " << (day+1)
                   << " by all 3 monkeys is: " << average << " Pounds" << endl;
          }
      }
      
      int findLowest (int table [][DAYS])
      {
          int lowest = table [MONKEYS][DAYS];
          for (int monkey = 0; monkey < MONKEYS; monkey++)
          {
              for (int day = 0; day < DAYS; day++)
              {
                  if (table [monkey][day] < lowest)
                      lowest = table[monkey][day];
              }
              
              cout << endl;
          }
          return lowest;
      }
      
      int findHighest (int table [][DAYS])
      {
          int highest = table [MONKEYS][DAYS];
          for (int monkey = 0; monkey < MONKEYS; monkey++)
          {
              for (int day = 0; day < DAYS; day++)
              {
                  if (table [monkey][day] > highest)
                      highest = table[monkey][day];
              }
              
              cout << endl;
          }
          return highest;
      }

      Comment

      • Alberto Fortuny
        New Member
        • Nov 2010
        • 5

        #4
        Ok well i go this working, any idea where to start to write all this data that the program outputs to the screen, to a file?

        Comment

        • Alberto Fortuny
          New Member
          • Nov 2010
          • 5

          #5
          Here is what i have so far as to writing the data to a file, the problem im getting is writing the void functions to a file.
          Code:
          #include <cstdlib>
          #include <iostream>
          #include <fstream>
          
          using namespace std;
          
          const int MONKEYS = 3;
          const int DAYS = 7;
          double average = 0;
          
          int getSum (int [][DAYS]);
          void getChart (int [][DAYS]);
          void dayAvg (int table [][DAYS]);
          int findLowest (int table [][DAYS]);
          int findHighest (int table [][DAYS]);
          
          int main(int argc, char *argv[])
          {
              int table [MONKEYS][DAYS];
              ofstream outfile;
              
              cout << "The diet of 3 monkeys over 7 days, please input your info: \n\n";
              
              outfile.open("MonkeyBuisnessFile.txt");
              
              getChart(table);
              
              cout << "The total amount of food consumed by the monkeys: "
                   << getSum(table) << " Pounds\n\n";
              outfile << "The total amount of food consumed by the monkeys: "
                   << getSum(table) << " Pounds\n\n";
                   
              dayAvg(table);
              
              cout << "The lowest amount of food eaten : " << findLowest(table) << " Pounds\n";
              outfile << "The lowest amount of food eaten : " << findLowest(table) << " Pounds\n";
              cout << "The highest amount of food eaten : " << findHighest(table) << " Pounds\n";
              outfile << "The highest amount of food eaten : " << findHighest(table) << " Pounds\n";
              
              outfile.close();
              
              system("PAUSE");
              return EXIT_SUCCESS;
          }
          
          int getSum (int table [][DAYS])
          {
              int sum = 0;
              for ( int monkey = 0;  monkey < MONKEYS; monkey++)
              {
                  for (int day = 0; day < DAYS; day++)
                      sum += table [monkey][day];
              }
              return sum;
          }
          
          void getChart (int table[][DAYS])
          {
              for (int monkey = 0; monkey < MONKEYS; monkey++)
              {
                  for (int day = 0; day < DAYS; day++)
                  {
                      cout << "Monkey " << (monkey+1) << ", ";
                      cout << "Day " << (day+1) << ": ";
                      cin >> table [monkey][day];
                  }
                  cout << endl;
              }
          }
          
          void dayAvg (int table [][DAYS])
          {
              for ( int day = 0;  day < DAYS; day++)
              {
                  int total = 0;
                  for (int monkey = 0; monkey < MONKEYS; monkey++)
                  {
                      total += table [monkey][day];
                  }
                  average = total/MONKEYS;
                  cout << "Average food consumed on day " << (day+1)
                       << " by all 3 monkeys is: " << average << " Pounds" << endl;
              }
          }
          
          int findLowest (int table [][DAYS])
          {
              int lowest = table [0][0];
              for (int monkey = 0; monkey < MONKEYS; monkey++)
              {
                  for (int day = 0; day < DAYS; day++)
                  {
                      if (table [monkey][day] < lowest)
                          lowest = table[monkey][day];
                  }
                  
                  cout << endl;
              }
              return lowest;
          }
          
          int findHighest (int table [][DAYS])
          {
              int highest = table [0][0];
              for (int monkey = 0; monkey < MONKEYS; monkey++)
              {
                  for (int day = 0; day < DAYS; day++)
                  {
                      if (table [monkey][day] > highest)
                          highest = table[monkey][day];
                  }
                  
                  cout << endl;
              }
              return highest;
          }

          Comment

          • Alberto Fortuny
            New Member
            • Nov 2010
            • 5

            #6
            Also how do i use input validation n the getChart function so the program advertises the user that negative numbers aren't allowed?

            Comment

            • Banfa
              Recognized Expert Expert
              • Feb 2006
              • 9067

              #7
              You need to split functionality and interface.

              That is the code that performs the logic of the program should not perform IO operations. It should return a result that can be passed to a section of IO code that outputs the data.

              For example void dayAvg (int table [][DAYS]). It should not contain cout statements but rather should return its result, say as a std::vector.

              Additionally you can write your output code in a function that takes an std::ostream& as input (as well as the table). Since both std::cout and std::ofstream inherit from std::ostream they can both be passed to the function and you only have to write your output code once and just call it twice.

              For example

              Code:
              #include <string>
              #include <iostream>
              #include <fstream>
              
              void outputResults(std::ostream& out, const int& value);
              
              int main()
              {
                  int value = 5;
                  std::ofstream file("outfile.txt");
              
                  outputResults(std::cout, value);
                  outputResults(file, value);
              
                  return 0;
              }
              
              void outputResults(std::ostream& out, const int& value)
              {
                  out << "The value is: " << value << std::endl;
              }

              Comment

              • Banfa
                Recognized Expert Expert
                • Feb 2006
                • 9067

                #8
                Either check the values for negative numbers and report the error to the user if they are input or make the base type of table unsigned and check cin for errors (which you should be doing anyway what if a the user enters text?).

                Comment

                Working...