Answer:
#include "stdafx.h"
#include <string>
#include <iostream>
#include <iomanip> //setprecision and fixed is located in this namespace
using namespace std;
const int Max = 5;
string Cities[Max];
double CitiesRainfall[Max];
int getIndexOfLowest(double[], int);
int getIndexOfHighest(double[], int);
double getAverage(double[], int);
void Menu();
void InputCities();
void InputCitiesRainfall();
void PrintOutput();
int main()
{
InputCities();
InputCitiesRainfall();
PrintOutput();
system("pause");
return 0;
}
void InputCities()
{
bool loop = false;
for (int i = 0; i < Max; i++)
{
string city;
do
{
cout << "Please enter city #" << i + 1 << ": ";
getline(cin, city);
if (city == "")
{
cout << "City cannot be empty!" << endl;
loop = true;
}
else
{
loop = false;
}
} while (loop);
Cities[i] = city;
}
}
void InputCitiesRainfall()
{
bool loop = false;
for (int i = 0; i < Max; i++)
{
double rainfallTotal;
do
{
cout << "Please enter the rainfall total for " << Cities[i] << ": ";
cin >> rainfallTotal;
if (rainfallTotal<0 || rainfallTotal>100)
{
cout << "Rainfall must be greater than 0 or less than 100." << endl;
loop = true;
}
else
{
loop = false;
}
} while (loop);
CitiesRainfall[i] = rainfallTotal;
}
}
int getIndexOfLowest(double arr[], int size)
{
int index = 0;
int lowest = arr[0];
for (int i = 0; i<Max; i++)
{
if (lowest>arr[i])
{
lowest = arr[i];
index = i;
}
}
return index;
}
int getIndexOfHighest(double arr[], int size)
{
int index = 0;
int highest = arr[0];
for (int i = 0; i < Max; i++)
{
if (highest < arr[i])
{
highest = arr[i];
index = i;
}
}
return index;
}
double getAverage(double arr[], int size)
{
double avg, total;
avg = total = 0;
for (int i = 0; i < Max; i++)
{
total += arr[i];
}
avg = total / size;
return avg;
}
void PrintOutput()
{
int highestRainfallIndex = getIndexOfHighest(CitiesRainfall, Max);
cout << "The city with the highest rainfall is " << Cities[highestRainfallIndex] << "." << endl;
int lowestRainfallIndex = getIndexOfLowest(CitiesRainfall, Max);
cout << "The city with the lowest rainfall is " << Cities[lowestRainfallIndex] << "." << endl;
cout << "The average rainfall across all cities is " << setprecision(2) << fixed << getAverage(CitiesRainfall, Max) << " inches." << endl;
}
Explanation: