Logic error, simple stat prog

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Oct 2008
Posts: 22
Reputation: Foe89 is an unknown quantity at this point 
Solved Threads: 0
Foe89 Foe89 is offline Offline
Newbie Poster

Logic error, simple stat prog

 
0
  #1
Dec 1st, 2008
I've got executable code that is partially working, so i know i've done something wrong in it. In it you type in the rainfall for each month in inches and will display the total, average, largest and smallest rainfalls(with the month). I have the the total, average and largest(except the month shows 13 everytime) and the smallest along with month being 13 are not working. It's probably some simple error here and there but I can't seem to find what I am doing wrong.

I'm assuming that after I've gone through the array the index just adds another value that is making the months both 13 on the smallest/largest stats and I'm not sure what I'm to do to change it.

The main function is fine as it is, it's the others that need help.

  1. #include<iostream>
  2. #include <iomanip>
  3. using namespace std;
  4.  
  5. // Function prototypes
  6. double getTotal(double [], int);
  7. double getAverage(double [], int);
  8. double getLargest(double [], int, int &);
  9. double getSmallest(double [], int, int &);
  10.  
  11. int main()
  12. {
  13. const int NUM_MONTHS = 12;
  14. double rainFall[NUM_MONTHS]; // Stores total rainfall for each month
  15.  
  16. // Input rainfall amounts and store them in the 12 array locations
  17. for (int month = 0; month < NUM_MONTHS; month++)
  18. {
  19. cout << "Enter the rainfall (in inches) for month #";
  20. cout << (month + 1) << ": ";
  21. cin >> rainFall[month];
  22.  
  23. while (rainFall[month] < 0)
  24. { cout << "Rainfall must be 0 or more. Please re-enter: ";
  25. cin >> rainFall[month];
  26. }
  27. }
  28.  
  29. // Display the total rainfall
  30. cout << fixed << showpoint << setprecision(2) << endl;
  31. cout << "Total rainfall for the year was ";
  32. cout << setw(5) << getTotal(rainFall, NUM_MONTHS) << " inches." << endl;
  33.  
  34. // Display the average rainfall
  35. cout << "Average rainfall for the year was ";
  36. cout << setw(5) << getAverage(rainFall, NUM_MONTHS) << " inches." << endl << endl;
  37.  
  38. // Display the months with the largest & smallest amounts of rain.
  39. // The variable index is passed by reference to the getLargest &
  40. // getSmallest functions, so they can assign it the subscript of the
  41. // array element having the largest, or smallest, amount of rainfall.
  42. int index;
  43. cout << "The largest amount of rainfall was " << setw(5);
  44. cout << getLargest(rainFall, NUM_MONTHS, index) << " inches in month ";
  45. cout << (index + 1) << "." << endl;
  46.  
  47. cout << "The smallest amount of rainfall was " << setw(5);
  48. cout << getSmallest(rainFall, NUM_MONTHS, index) << " inches in month ";
  49. cout << (index + 1) << "." << endl;
  50. return 0;
  51. }
  52.  
  53.  
  54.  
  55. double getTotal(double array[], int size)
  56. {
  57. double total = 0;
  58.  
  59. for(int count = 0; count < size; count++)
  60. {
  61. total += array[count];
  62. }
  63. return total;
  64. }
  65.  
  66. double getAverage(double array[], int size)
  67. {
  68. double total;
  69. double average;
  70.  
  71. total = 0;
  72.  
  73. for(int count = 0; count < size; count++)
  74. {
  75. total += array[count];
  76. }
  77.  
  78. for (count = 0; count < size; count++)
  79. {
  80. average = total / array[count];
  81. }
  82. return average;
  83. }
  84.  
  85.  
  86. double getLargest(double array[], int size, int &count)
  87. {
  88. double largest = array[100];
  89.  
  90. for (count = 0; count < size; count++)
  91. { if (array[count] > largest)
  92. largest = array[count];
  93.  
  94. }
  95. return largest;
  96. }
  97.  
  98. double getSmallest(double array[], int size, int &count)
  99. {
  100. double smallest = array[100];
  101.  
  102. for (count = 0; count < size; count++)
  103. { if (array[count] < smallest)
  104. smallest = array[count];
  105. }
  106. return smallest;
  107. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Logic error, simple stat prog

 
0
  #2
Dec 1st, 2008
Only getTotal code is correct. All three other fuctions are wrong.
1. getAverage: use getTotal to obtain a sum of all elements then divide sum by the number of elements!
2. getLargest and getSmallest: why double largest = array[100]; ?! No such element in the array at all. Start from array[0] value then loop from index 1.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 22
Reputation: Foe89 is an unknown quantity at this point 
Solved Threads: 0
Foe89 Foe89 is offline Offline
Newbie Poster

Re: Logic error, simple stat prog

 
0
  #3
Dec 1st, 2008
Originally Posted by ArkM View Post
Only getTotal code is correct. All three other fuctions are wrong.
1. getAverage: use getTotal to obtain a sum of all elements then divide sum by the number of elements!
2. getLargest and getSmallest: why double largest = array[100]; ?! No such element in the array at all. Start from array[0] value then loop from index 1.
lol I forgot to change the array back to 0, I was just messing around with things to see what would happen. And for the getAverage I can't seem to get it to work properly. Everytime I call getTotal I get some sort of syntax error. I'll tinker around with it again and see if i can get it to work as you've described.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Logic error, simple stat prog

 
0
  #4
Dec 1st, 2008
Everytime I call getTotal I get some sort of syntax error.
???
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 22
Reputation: Foe89 is an unknown quantity at this point 
Solved Threads: 0
Foe89 Foe89 is offline Offline
Newbie Poster

Re: Logic error, simple stat prog

 
0
  #5
Dec 1st, 2008
I fixed the function call, now the only problem is that it won't display the month with the largest/smallest data. Here's what it shows, along with the code following.

Enter the rainfall (in inches) for month #1: 2.5
Enter the rainfall (in inches) for month #2: 10.7
Enter the rainfall (in inches) for month #3: 32
Enter the rainfall (in inches) for month #4: 231
Enter the rainfall (in inches) for month #5: 7.8
Enter the rainfall (in inches) for month #6: 5.4
Enter the rainfall (in inches) for month #7: 2.5
Enter the rainfall (in inches) for month #8: 4.87
Enter the rainfall (in inches) for month #9: 5.8
Enter the rainfall (in inches) for month #10: 20.0
Enter the rainfall (in inches) for month #11: 2.6
Enter the rainfall (in inches) for month #12: 3.8

Total rainfall for the year was 328.97 inches.
Average rainfall for the year was 86.57 inches.

The largest amount of rainfall was 231.00 inches in month 13.
The smallest amount of rainfall was 2.50 inches in month 13.
Press any key to continue


  1. #include<iostream>
  2. #include <iomanip>
  3. using namespace std;
  4.  
  5. // Function prototypes
  6. double getTotal(double [], int);
  7. double getAverage(double [], int);
  8. double getLargest(double [], int, int &);
  9. double getSmallest(double [], int, int &);
  10.  
  11. int main()
  12. {
  13. const int NUM_MONTHS = 12;
  14. double rainFall[NUM_MONTHS]; // Stores total rainfall for each month
  15.  
  16. // Input rainfall amounts and store them in the 12 array locations
  17. for (int month = 0; month < NUM_MONTHS; month++)
  18. {
  19. cout << "Enter the rainfall (in inches) for month #";
  20. cout << (month + 1) << ": ";
  21. cin >> rainFall[month];
  22.  
  23. while (rainFall[month] < 0)
  24. { cout << "Rainfall must be 0 or more. Please re-enter: ";
  25. cin >> rainFall[month];
  26. }
  27. }
  28.  
  29. // Display the total rainfall
  30. cout << fixed << showpoint << setprecision(2) << endl;
  31. cout << "Total rainfall for the year was ";
  32. cout << setw(5) << getTotal(rainFall, NUM_MONTHS) << " inches." << endl;
  33.  
  34. // Display the average rainfall
  35. cout << "Average rainfall for the year was ";
  36. cout << setw(5) << getAverage(rainFall, NUM_MONTHS) << " inches." << endl << endl;
  37.  
  38. // Display the months with the largest & smallest amounts of rain.
  39. // The variable index is passed by reference to the getLargest &
  40. // getSmallest functions, so they can assign it the subscript of the
  41. // array element having the largest, or smallest, amount of rainfall.
  42. int index;
  43. cout << "The largest amount of rainfall was " << setw(5);
  44. cout << getLargest(rainFall, NUM_MONTHS, index) << " inches in month ";
  45. cout << (index + 1) << "." << endl;
  46.  
  47. cout << "The smallest amount of rainfall was " << setw(5);
  48. cout << getSmallest(rainFall, NUM_MONTHS, index) << " inches in month ";
  49. cout << (index + 1) << "." << endl;
  50. return 0;
  51. }
  52.  
  53.  
  54.  
  55. double getTotal(double array[], int size)
  56. {
  57. double total = 0;
  58.  
  59. for(int count = 0; count < size; count++)
  60. {
  61. total += array[count];
  62. }
  63. return total;
  64. }
  65.  
  66. double getAverage(double array[], int size)
  67. {
  68. double total;
  69. double average;
  70.  
  71.  
  72. for(int count = 0; count < size; count++)
  73. {
  74. total += array[count];
  75. }
  76.  
  77. for (count = 0; count < size; count++)
  78. {
  79. average = getTotal(array, size) / array[count];
  80. }
  81. return average;
  82. }
  83.  
  84.  
  85. double getLargest(double array[], int size, int &count)
  86. {
  87. double largest = array[0];
  88.  
  89. for (count = 1; count < size; count++)
  90. { if (array[count] > largest)
  91. largest = array[count];
  92.  
  93. }
  94. return largest;
  95. }
  96.  
  97. double getSmallest(double array[], int size, int &count)
  98. {
  99. double smallest = array[0];
  100.  
  101. for (count = 1; count < size; count++)
  102. { if (array[count] < smallest)
  103. smallest = array[count];
  104. }
  105. return smallest;
  106. }
Last edited by Foe89; Dec 1st, 2008 at 1:55 pm.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Logic error, simple stat prog

 
1
  #6
Dec 1st, 2008
1. That's because the modification (improvement) of getSmallest and getLargest functions was incorrect. It was wrong solution to assign a role of for loop counter to output parameter count (why "count" if it's an index of? ). Think: the 1st count value is 1 (must be 0) and the last is 12 (then you add 1 and print 13). Make obvious correction.
2. Look again to getAverage code: it's WRONG!!!
Ooh, that's right code:
  1. double getAverage(double array[], int size)
  2. {
  3. return getTotal(array,size) / size;
  4. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 22
Reputation: Foe89 is an unknown quantity at this point 
Solved Threads: 0
Foe89 Foe89 is offline Offline
Newbie Poster

Re: Logic error, simple stat prog

 
0
  #7
Dec 1st, 2008
I was using count because the examples that were shown used count as the name. I understand that that count should be 0, so the loop will end at 12, but i'm still struggling to understand how to get the correct index number with the largest and smallest values.

I should have know the getAverage function.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,688
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 265
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Logic error, simple stat prog

 
0
  #8
Dec 1st, 2008
In getlargest() count will act as the index of the element with the largest rainfall for that year. Since count is passed to getlargest() by reference you don't need to return anything if there are no other requirements of the function. Use some counter variable other than count to control the loop and keep track of count separately.
  1. void getLargest(double array[], int size, int &count)
  2. {
  3. count = 0; //assume first element is largest
  4.  
  5. for (int i = 1; i < size; i++)
  6. {
  7. if (array[i] > array[count])
  8. {
  9. count = i;
  10. }
  11. }
  12. }
and back in main() use count to display the result;
cout << "the largerst rainfall was " << array[count];
Klatu Barada Nikto
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 22
Reputation: Foe89 is an unknown quantity at this point 
Solved Threads: 0
Foe89 Foe89 is offline Offline
Newbie Poster

Re: Logic error, simple stat prog

 
0
  #9
Dec 1st, 2008
the getLargest function needs to get both the index for the month and the largest value. I can't change anything in main, as it was given, so it needs to be a double. Do I need a nested for loop?
Last edited by Foe89; Dec 1st, 2008 at 6:39 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 22
Reputation: Foe89 is an unknown quantity at this point 
Solved Threads: 0
Foe89 Foe89 is offline Offline
Newbie Poster

Re: Logic error, simple stat prog

 
0
  #10
Dec 1st, 2008
Figured it out

  1. double getLargest(double array[], int size, int &i)
  2. {
  3. double largest = array[0];
  4.  
  5. for (int count = 0; count < size; count++)
  6. { if (array[count] > largest)
  7. {largest = array[count];
  8. i = count;}
  9. }
  10. return largest;
  11.  
  12. }
  13.  
  14.  
  15. double getSmallest(double array[], int size, int &i)
  16. {
  17. double smallest = array[0];
  18.  
  19. for (int count = 0; count < size; count++)
  20. { if (array[count] < smallest)
  21. {smallest = array[count];
  22. i = count; }
  23. }
  24. return smallest;
  25. }
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC