I'm new to coding and I'm stumped on this last part of the code. I need to find the Largest/Smallest number in user input on rainfall amounts.
#include <iostream>
using namespace std;
void displayMonthly(double rain[], int numElements);
void displayTotal(double rain[], int numElements);
void displayAverage(double rain[], int numElements);
void displayHigh(double rain[], int numElements);
void displayLow(double rain[], int numElements);
int main()
{
double rainfall[12] = {0.0};
int choice = 0;
//get input
for (int x = 0; x < 12; x += 1)
{
cout << "Enter the rainfall for month " << x + 1 << ": ";
cin >> rainfall[x];
}//end for
while (choice != -1)
{
//call appropriate function or end program
switch (choice)
{
case 1:
displayMonthly(rainfall, 12);
break;
case 2:
displayTotal(rainfall, 12);
break;
case 3:
displayAverage(rainfall, 12);
break;
case 4:
displayHigh(rainfall, 12);
break;
case 5:
displayLow(rainfall, 12);
break;
default:
cout << "default" << endl;
}
There are known ways to do this with the greater effieciency, these things have a name (sorting algorithms). I'm not familiar with them, so my suggestion may not be the optimum (you shouldn't notice for such a small amount of data, but it would cost time and resources if applied to larger chunks of data, say the rainfall of the century... in 1000 different cities)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
void displayHigh(constdouble& rain[], int numElements)
{
// prepare a variable to store the highest rainfall
double maxRain = 0;
// go through every value
for (int i = 0; i < numElements; i++)
{
// compare and check if it's higher than the previos maximum
if (maxRain < rain [i])
{
// if so, replace
maxRain = rain[i];
}
}
//display
cout << "The highest rainfall was : " << maxRain;
} //end of function