fork download
  1. // Andres Guzman CSC5 Chapter 8, P. 488, #5
  2. /******************************************************************************
  3.  *
  4.  * DISPLAY RAINFALL STATISTICS
  5.  * ____________________________________________________________________________
  6.  * This program will calculate and display the total, average, and highest and
  7.  * lowest months of rainfall as well as display the highest to lowest months of
  8.  * rain in order.
  9.  *
  10.  * Computation:
  11.  * Uses a selection sort to sort the values inside the months for rainfall in
  12.  * order from highest to lowest.
  13.  * total / 12
  14.  * ____________________________________________________________________________
  15.  *
  16.  * INPUT
  17.  *
  18.  * [SIZE] = Array Size
  19.  * rainfall[SIZE] = Holds the inputed values of rainfall for each month
  20.  *
  21.  * OUTPUT
  22.  *
  23.  * total = Displays the total accumulated rainfall
  24.  * months[highestmonth] = Displays highest month of rainfall
  25.  * months[lowestmonth] = Display lowest month of rainfall
  26.  *
  27.  ******************************************************************************/
  28. #include <iostream>
  29. #include <iomanip>
  30. #include <string>
  31. using namespace std;
  32. //
  33. //Function Prototype
  34. void selectionsort(double a[], string b[], int c);
  35.  
  36. int main()
  37. {
  38. const int SIZE = 12; //Array size
  39. string months[SIZE] = {"January", "February", "March", "April", "May", "June",
  40. "July", "August", "September", "October", "November",
  41. "December"}; //Initialization List for month
  42. double rainfall[SIZE]; //Subscript to input values for rainfall
  43. double total = 0; //Accumulator for rainfall
  44. int i; //Cycles through subscript in for loops
  45. //
  46. //Initializing For Loop
  47. for(i = 0; i < SIZE; i++)
  48. {
  49. cout << months[i] << " Rainfall: ";
  50. cin >> rainfall[i];
  51. cout << endl;
  52. while(rainfall[i] < 0) //Input Validiation
  53. {
  54. cout << "Invalid Rainfall Amount, Please Try Again." << endl;
  55. cout << months[i] << " Rainfall: ";
  56. cin >> rainfall[i];
  57. cout << endl;
  58. }
  59. total += rainfall[i]; //Adds rainfall input to total
  60. }
  61. double highest = rainfall[0]; //Assigns beginning of subscript to highest
  62. double lowest = rainfall [0]; //Assigns beginning of subscript to lowest
  63. int highestmonth = 0; //Assigns variable to subscript as a name
  64. int lowestmonth = 0; //Assigns variable to subscript as a name
  65. //
  66. //Initializing For Loop
  67. for(i = 0; i < SIZE; i++)
  68. {
  69. if(rainfall[i] > highest) //Finds which subscript is bigger than [0]
  70. {
  71. highest = rainfall[i]; //Assigns found subscript to highest
  72. highestmonth = i; //Assigns highestmonth to subscript [0]
  73.  
  74. }
  75. if(rainfall[i] < lowest) //Finds which subscirpt is lower than [0]
  76. {
  77. lowest = rainfall[i]; //Assigns found subscript to lower
  78. lowestmonth = i; //Assigns lowestmonth to subscript [0]
  79. }
  80. }
  81. cout << "\nTotal Rainfall: " << total << endl;
  82. cout << fixed << setprecision(2); //Allows 2 decimal points for average
  83. cout << "Average Monthly Rainfall: " << (total / 12) << endl;
  84. cout << "Highest Month of Rainfall: " << months[highestmonth] << endl;
  85. cout << "Lowest Month of Rainfall: " << months[lowestmonth] << endl;
  86. //
  87. //Display Next section contents
  88. cout << "\nMonths Sorted in Order of Rainfall" << endl;
  89. //
  90. //Function Call
  91. selectionsort(rainfall, months, SIZE);
  92. //
  93. //Output Each Month in order using for loop
  94. for(i = 0; i < SIZE; i++)
  95. {
  96. cout << months[i] << ": " << rainfall[i] << endl;
  97. }
  98. return 0;
  99. }
  100. /*******************************************************************************
  101.  * Adding definition to Function selectionsort. This function sorts the rainfall
  102.  * amounts from highest to lowest while keeping each month paired with its
  103.  * corresponding rainfall amount. Uses selection sort to find the highest
  104.  * rainfall value and moves it into the correct position.
  105.  ******************************************************************************/
  106. void selectionsort(double a[], string b[], int c)
  107. {
  108. int startscan, minIndex; //Used to scan through the array
  109. double minValue; //Holds highest value found
  110. string temp; //Holds found value during swap
  111. //
  112. //Initializing For Loop
  113. for (startscan = 0; startscan < (c - 1); startscan++)
  114. {
  115. minIndex = startscan; //Assigns starting position as highest
  116. minValue = a[startscan]; //Assigns starting rainfall as highest
  117. //
  118. //Searches for highest rainfall value
  119. for (int index = startscan + 1; index < c; index++)
  120. {
  121. if(a[index] > minValue)
  122. {
  123. minValue = a[index]; //Updates highest rainfall value
  124. minIndex = index; //stores position of highest value
  125. }
  126. }
  127. //
  128. //Swaps Values
  129. a[minIndex] = a[startscan];
  130. a[startscan] = minValue;
  131. //
  132. //Swaps months to match swapped values
  133. temp = b[minIndex];
  134. b[minIndex] = b[startscan];
  135. b[startscan] = temp;
  136. }
  137. }
  138.  
Success #stdin #stdout 0.01s 5356KB
stdin
12.6 30.2 20 20 20 2 2 2 2 2 2 2
stdout
January Rainfall: 
February Rainfall: 
March Rainfall: 
April Rainfall: 
May Rainfall: 
June Rainfall: 
July Rainfall: 
August Rainfall: 
September Rainfall: 
October Rainfall: 
November Rainfall: 
December Rainfall: 

Total Rainfall: 116.8
Average Monthly Rainfall: 9.73
Highest Month of Rainfall: February
Lowest Month of Rainfall: June

Months Sorted in Order of Rainfall
February: 30.20
March: 20.00
April: 20.00
May: 20.00
January: 12.60
June: 2.00
July: 2.00
August: 2.00
September: 2.00
October: 2.00
November: 2.00
December: 2.00