472,374 Members | 1,133 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,374 software developers and data experts.

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
Expand|Select|Wrap|Line Numbers
  1. #include <cstdlib>
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. const int MONKEYS = 3;
  7. const int DAYS = 7;
  8. double average = 0;
  9.  
  10. int getSum (int [][DAYS]);
  11. void getChart (int [][DAYS]);
  12. void dayAvg (int table [][DAYS]);
  13. int findLowest (int table [][DAYS]);
  14. int findHighest (int table [][DAYS]);
  15.  
  16. int main(int argc, char *argv[])
  17. {
  18.     int table [MONKEYS][DAYS];
  19.  
  20.     cout << "The diet of 3 monkeys over 7 days, please input your info: \n\n";
  21.  
  22.     getChart(table);
  23.  
  24.     cout << "The total amount of food consumed by the monkeys: "
  25.          << getSum(table) << " Pounds\n\n";
  26.  
  27.     dayAvg(table);
  28.  
  29.     cout << "The lowest amount of food eaten was: " << findLowest(table) << " Pounds\n";
  30.     cout << "The highest amount of food eaten was: " << findHighest(table) << " Pounds\n";
  31.  
  32.     //ofstream outData;
  33.     //outData.open("results.txt");
  34.  
  35.     system("PAUSE");
  36.     return EXIT_SUCCESS;
  37. }
  38.  
  39. int getSum (int table [][DAYS])
  40. {
  41.     int sum = 0;
  42.     for ( int monkey = 0;  monkey < MONKEYS; monkey++)
  43.     {
  44.         for (int day = 0; day < DAYS; day++)
  45.             sum += table [monkey][day];
  46.     }
  47.     return sum;
  48. }
  49.  
  50. void getChart (int table[][DAYS])
  51. {
  52.     for (int monkey = 0; monkey < MONKEYS; monkey++)
  53.     {
  54.         for (int day = 0; day < DAYS; day++)
  55.         {
  56.             cout << "Monkey " << (monkey+1) << ", ";
  57.             cout << "Day " << (day+1) << ": ";
  58.             cin >> table [monkey][day];
  59.         }
  60.         cout << endl;
  61.     }
  62. }
  63.  
  64. void dayAvg (int table [][DAYS])
  65. {
  66.     for ( int day = 0;  day < DAYS; day++)
  67.     {
  68.         int total = 0;
  69.         for (int monkey = 0; monkey < MONKEYS; monkey++)
  70.         {
  71.             total += table [monkey][day];
  72.         }
  73.         average = total/MONKEYS;
  74.         cout << "Average food consumed on day " << (day+1)
  75.              << " by all 3 monkeys is: " << average << " Pounds" << endl;
  76.     }
  77. }
  78.  
  79. int findLowest (int table [][DAYS])
  80. {
  81.     int lowest = table [MONKEYS][DAYS];
  82.     for (int monkey = 0; monkey < MONKEYS; monkey++)
  83.     {
  84.         for (int day = 0; day < DAYS; day++)
  85.         {
  86.             if (table [monkey][day] < lowest)
  87.                 lowest = table[MONKEYS][DAYS];
  88.         }
  89.  
  90.         cout << endl;
  91.     }
  92.     return lowest;
  93. }
  94.  
  95. int findHighest (int table [][DAYS])
  96. {
  97.     int highest = table [MONKEYS][DAYS];
  98.     for (int monkey = 0; monkey < MONKEYS; monkey++)
  99.     {
  100.         for (int day = 0; day < DAYS; day++)
  101.         {
  102.             if (table [monkey][day] > highest)
  103.                 highest = table[MONKEYS][DAYS];
  104.         }
  105.  
  106.         cout << endl;
  107.     }
  108.     return highest;
  109. }
Nov 11 '10 #1

✓ answered by Banfa

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

7 7228
Banfa
9,065 Expert Mod 8TB
Line 87 and 103 have the array indexes in the wrong case.
Nov 11 '10 #2
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:
Expand|Select|Wrap|Line Numbers
  1. #include <cstdlib>
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. const int MONKEYS = 3;
  7. const int DAYS = 7;
  8. double average = 0;
  9.  
  10. int getSum (int [][DAYS]);
  11. void getChart (int [][DAYS]);
  12. void dayAvg (int table [][DAYS]);
  13. int findLowest (int table [][DAYS]);
  14. int findHighest (int table [][DAYS]);
  15.  
  16. int main(int argc, char *argv[])
  17. {
  18.     int table [MONKEYS][DAYS];
  19.  
  20.     cout << "The diet of 3 monkeys over 7 days, please input your info: \n\n";
  21.  
  22.     getChart(table);
  23.  
  24.     cout << "The total amount of food consumed by the monkeys: "
  25.          << getSum(table) << " Pounds\n\n";
  26.  
  27.     dayAvg(table);
  28.  
  29.     cout << "The lowest amount of food eaten : " << findLowest(table) << " Pounds\n";
  30.     cout << "The highest amount of food eaten : " << findHighest(table) << " Pounds\n";
  31.  
  32.     //ofstream outData;
  33.     //outData.open("results.txt");
  34.  
  35.     system("PAUSE");
  36.     return EXIT_SUCCESS;
  37. }
  38.  
  39. int getSum (int table [][DAYS])
  40. {
  41.     int sum = 0;
  42.     for ( int monkey = 0;  monkey < MONKEYS; monkey++)
  43.     {
  44.         for (int day = 0; day < DAYS; day++)
  45.             sum += table [monkey][day];
  46.     }
  47.     return sum;
  48. }
  49.  
  50. void getChart (int table[][DAYS])
  51. {
  52.     for (int monkey = 0; monkey < MONKEYS; monkey++)
  53.     {
  54.         for (int day = 0; day < DAYS; day++)
  55.         {
  56.             cout << "Monkey " << (monkey+1) << ", ";
  57.             cout << "Day " << (day+1) << ": ";
  58.             cin >> table [monkey][day];
  59.         }
  60.         cout << endl;
  61.     }
  62. }
  63.  
  64. void dayAvg (int table [][DAYS])
  65. {
  66.     for ( int day = 0;  day < DAYS; day++)
  67.     {
  68.         int total = 0;
  69.         for (int monkey = 0; monkey < MONKEYS; monkey++)
  70.         {
  71.             total += table [monkey][day];
  72.         }
  73.         average = total/MONKEYS;
  74.         cout << "Average food consumed on day " << (day+1)
  75.              << " by all 3 monkeys is: " << average << " Pounds" << endl;
  76.     }
  77. }
  78.  
  79. int findLowest (int table [][DAYS])
  80. {
  81.     int lowest = table [MONKEYS][DAYS];
  82.     for (int monkey = 0; monkey < MONKEYS; monkey++)
  83.     {
  84.         for (int day = 0; day < DAYS; day++)
  85.         {
  86.             if (table [monkey][day] < lowest)
  87.                 lowest = table[monkey][day];
  88.         }
  89.  
  90.         cout << endl;
  91.     }
  92.     return lowest;
  93. }
  94.  
  95. int findHighest (int table [][DAYS])
  96. {
  97.     int highest = table [MONKEYS][DAYS];
  98.     for (int monkey = 0; monkey < MONKEYS; monkey++)
  99.     {
  100.         for (int day = 0; day < DAYS; day++)
  101.         {
  102.             if (table [monkey][day] > highest)
  103.                 highest = table[monkey][day];
  104.         }
  105.  
  106.         cout << endl;
  107.     }
  108.     return highest;
  109. }
  110.  
Nov 11 '10 #3
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?
Nov 11 '10 #4
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.
Expand|Select|Wrap|Line Numbers
  1. #include <cstdlib>
  2. #include <iostream>
  3. #include <fstream>
  4.  
  5. using namespace std;
  6.  
  7. const int MONKEYS = 3;
  8. const int DAYS = 7;
  9. double average = 0;
  10.  
  11. int getSum (int [][DAYS]);
  12. void getChart (int [][DAYS]);
  13. void dayAvg (int table [][DAYS]);
  14. int findLowest (int table [][DAYS]);
  15. int findHighest (int table [][DAYS]);
  16.  
  17. int main(int argc, char *argv[])
  18. {
  19.     int table [MONKEYS][DAYS];
  20.     ofstream outfile;
  21.  
  22.     cout << "The diet of 3 monkeys over 7 days, please input your info: \n\n";
  23.  
  24.     outfile.open("MonkeyBuisnessFile.txt");
  25.  
  26.     getChart(table);
  27.  
  28.     cout << "The total amount of food consumed by the monkeys: "
  29.          << getSum(table) << " Pounds\n\n";
  30.     outfile << "The total amount of food consumed by the monkeys: "
  31.          << getSum(table) << " Pounds\n\n";
  32.  
  33.     dayAvg(table);
  34.  
  35.     cout << "The lowest amount of food eaten : " << findLowest(table) << " Pounds\n";
  36.     outfile << "The lowest amount of food eaten : " << findLowest(table) << " Pounds\n";
  37.     cout << "The highest amount of food eaten : " << findHighest(table) << " Pounds\n";
  38.     outfile << "The highest amount of food eaten : " << findHighest(table) << " Pounds\n";
  39.  
  40.     outfile.close();
  41.  
  42.     system("PAUSE");
  43.     return EXIT_SUCCESS;
  44. }
  45.  
  46. int getSum (int table [][DAYS])
  47. {
  48.     int sum = 0;
  49.     for ( int monkey = 0;  monkey < MONKEYS; monkey++)
  50.     {
  51.         for (int day = 0; day < DAYS; day++)
  52.             sum += table [monkey][day];
  53.     }
  54.     return sum;
  55. }
  56.  
  57. void getChart (int table[][DAYS])
  58. {
  59.     for (int monkey = 0; monkey < MONKEYS; monkey++)
  60.     {
  61.         for (int day = 0; day < DAYS; day++)
  62.         {
  63.             cout << "Monkey " << (monkey+1) << ", ";
  64.             cout << "Day " << (day+1) << ": ";
  65.             cin >> table [monkey][day];
  66.         }
  67.         cout << endl;
  68.     }
  69. }
  70.  
  71. void dayAvg (int table [][DAYS])
  72. {
  73.     for ( int day = 0;  day < DAYS; day++)
  74.     {
  75.         int total = 0;
  76.         for (int monkey = 0; monkey < MONKEYS; monkey++)
  77.         {
  78.             total += table [monkey][day];
  79.         }
  80.         average = total/MONKEYS;
  81.         cout << "Average food consumed on day " << (day+1)
  82.              << " by all 3 monkeys is: " << average << " Pounds" << endl;
  83.     }
  84. }
  85.  
  86. int findLowest (int table [][DAYS])
  87. {
  88.     int lowest = table [0][0];
  89.     for (int monkey = 0; monkey < MONKEYS; monkey++)
  90.     {
  91.         for (int day = 0; day < DAYS; day++)
  92.         {
  93.             if (table [monkey][day] < lowest)
  94.                 lowest = table[monkey][day];
  95.         }
  96.  
  97.         cout << endl;
  98.     }
  99.     return lowest;
  100. }
  101.  
  102. int findHighest (int table [][DAYS])
  103. {
  104.     int highest = table [0][0];
  105.     for (int monkey = 0; monkey < MONKEYS; monkey++)
  106.     {
  107.         for (int day = 0; day < DAYS; day++)
  108.         {
  109.             if (table [monkey][day] > highest)
  110.                 highest = table[monkey][day];
  111.         }
  112.  
  113.         cout << endl;
  114.     }
  115.     return highest;
  116. }
  117.  
Nov 11 '10 #5
Also how do i use input validation n the getChart function so the program advertises the user that negative numbers aren't allowed?
Nov 11 '10 #6
Banfa
9,065 Expert Mod 8TB
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

Expand|Select|Wrap|Line Numbers
  1. #include <string>
  2. #include <iostream>
  3. #include <fstream>
  4.  
  5. void outputResults(std::ostream& out, const int& value);
  6.  
  7. int main()
  8. {
  9.     int value = 5;
  10.     std::ofstream file("outfile.txt");
  11.  
  12.     outputResults(std::cout, value);
  13.     outputResults(file, value);
  14.  
  15.     return 0;
  16. }
  17.  
  18. void outputResults(std::ostream& out, const int& value)
  19. {
  20.     out << "The value is: " << value << std::endl;
  21. }
  22.  
  23.  
Nov 11 '10 #7
Banfa
9,065 Expert Mod 8TB
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?).
Nov 11 '10 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: sukh | last post by:
From the table i want everything highlighted with a * I wanted an SQl expression to look at values in Column 1 (ID), look at the corresponding values in the second column (F1) and select the row...
5
by: ritchie | last post by:
Hi, I am writing to ask if anyone can see why my array is not being sorted correctly? It's an array of 4 elements(ints 1,2,3,4) but after calling the selection sort it comes back sorted as...
3
by: kurrent | last post by:
i'm still new to php and programming and was hoping to get some help on a few questions i haven't had success in answering myself. I successfully created my first very simple script to accomplish a...
5
by: Stephen3776 | last post by:
I am doing an inventory control progam and trying to output a multiple array, I am getting an illegal conversion error java.lang.double !d. Can somebody tell me what I am doing wrong or if there is...
1
by: comedydave | last post by:
Hi guys, I'm new to ASP and need some array help. I need to have a shopping cart. When you visit the site it creates a session and an array. When you click add to basket it adds the item ID to...
5
by: davenet | last post by:
Hi, I'm new to Python and working on a school assignment. I have setup a dictionary where the keys point to an object. Each object has two member variables. I need to find the smallest value...
15
by: timothytoe | last post by:
Situation: I have an array of objects. I want to find the maximum value of a given numeric value that exists in each of the objects. Suppose the array of objects is called "stat" and the numeric...
5
by: ryuchi311 | last post by:
In C++ using arrays. I need to create a C Program that will ask for five integers input from the user, then store these in an array. Then I need to find the lowest and highest integers inside that...
2
by: yeshello54 | last post by:
so here is my problem...in a contact manager i am trying to complete i have ran into an error..we have lots of code because we have some from class which we can use...anyways i keep getting an error...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
1
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...
0
by: jack2019x | last post by:
hello, Is there code or static lib for hook swapchain present? I wanna hook dxgi swapchain present for dx11 and dx9.
0
by: F22F35 | last post by:
I am a newbie to Access (most programming for that matter). I need help in creating an Access database that keeps the history of each user in a database. For example, a user might have lesson 1 sent...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.