14.9k views
0 votes
Assignment Ahead of an expected drought this winter, the National Weather Service would like you to write a program that records the rainfall totals for 5 California cities and calculates a some statistics on the data. They would like to use your program in their branch offices across the state, so it needs to be able to accept rainfall data for any 5 cities, specified by the user. Using values stored in arrays, write a program that does the following: 1. Prompts the user for the names of 5 California cities. You can use any 5 cities of your choosing. Here are some to help you get started: (note that some cities have spaces in their names, this should be allowed). 2. Prompts the user in a loop) to enter rainfall totals (in inches, as a decimal number) for each of the 5 cities. Validate the input so the rainfall must be re-entered if it is less than zero or greater than 100. 3. Using 3 value-returning functions, calculate the locations with the highest and lowest rainfall and the average rainfall across the 5 cities. 4. Output the 3 calculated values with a precision of 2 digits past the decimal point.

1 Answer

5 votes

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;

}

Step-by-step explanation:

User Tom Porat
by
5.4k points