Friday, November 29, 2019

#include Essays - C, Computer Programming, Namespace

#include iostream #include string using namespace std; int main() double avgRainfall = 0; double totalRainfall = 0; double lowestRainfall; double highestRainfall; string lowestMonth; string highestMonth; string monthNames[] = "January", "Febuary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }; double monthlyTotals[12]; char repeat; do cout "Enter the rainfall for each month, in inches: " endl; //Input and positive number validation for (int i = 0; i 12; i++) cout monthNames[i] ": "; cin monthlyTotals[i]; while (monthlyTotals[i] 0) cout "Please enter a positive number for " monthNames[i] endl; cin monthlyTotals[i]; } } //Sum for (int i = 0; i 12; i++) totalRainfall = totalRainfall + monthlyTotals[i]; } //Average avgRainfall = totalRainfall / 12; //Highest and Lowest months highestRainfall = monthlyTotals[0]; highestMonth = monthNames[0]; lowestRainfall = monthlyTotals[0]; lowestMonth = monthNames[0]; for (int i = 0; i 12; i++) if (monthlyTotals[i] highestRainfall) highestRainfall = monthlyTotals[i]; highestMonth = monthNames[i]; } if (monthlyTotals[i] lowestRainfall) lowestRainfall = monthlyTotals[i]; lowestMonth = monthNames[i]; } } //Output cout "The total rainfall throughout the year was " totalRainfall " inches." endl; cout "The average rainfall for the entire year is " avgRainfall " inches." endl; cout "The month with the lowest rainfall is " lowestMonth ", with a total of " lowestRainfall " inches." endl; cout "The month with the highest rainfall is " highestMonth ", with a total of " highestRainfall " inches." endl; //Reset the variables for re-run avgRainfall = 0; totalRainfall = 0; lowestRainfall = 0; highestRainfall = 0; cout "\nWould you like to run the program again? (Y/N) "; cin repeat; cout endl; } while (repeat == 'y' || repeat == 'Y'); system("pause"); return 0; }

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.