View Single Post
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