One Dimensional Arrays (Highest Number)

Feb 28, 2013 at 11:47pm
Hey guys,

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

cout << endl;
cout << "--------------------------------" << endl;
cout << "1 - Display monthly amounts" << endl;
cout << "2 - Display total amount" << endl;
cout << "3 - Display average amount" << endl;
cout << "4 - Display high amount" << endl;
cout << "5 - Display low amount" << endl;
cout << "-1 - End program" << endl;
cout << "--------------------------------" << endl;
cout << "Enter your choice: ";

cin >> choice;

cout << endl;

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;
}

cout << endl;
cout << "--------------------------------" << endl;
cout << "1 - Display monthly amounts" << endl;
cout << "2 - Display total amount" << endl;
cout << "3 - Display average amount" << endl;
cout << "4 - Display high amount" << endl;
cout << "5 - Display low amount" << endl;
cout << "-1 - End program" << endl;
cout << "--------------------------------" << endl;
cout << "Enter your choice: ";
cin >> choice;
}

system("pause");
return 0;
} // end of function

void displayMonthly(double rain[], int numElements)
{
cout << "Monthly rainfall amounts:" << endl;
for (int x = 0; x < numElements; x += 1)
cout << rain[x] << endl;
//end for

} //end of function

void displayTotal(double rain[], int numElements)
{
double total = 0.0;

for (int x = 0; x < numElements; x += 1)
total += rain[x];
//end for
cout << "Total rainfall: " << total << endl;
} //end of function

void displayAverage(double rain[], int numElements)
{
double total = 0.0;
double average = 0.0;

for (int x = 0; x < numElements; x += 1)
total += rain[x];
//end for

average = total / numElements;

cout << "Average rainfall: " << average << endl;
} //end of function

void displayHigh(double rain[], int numElements)
{


} //end of function

void displayLow(double rain[], int numElements)
{




} //end of function
Last edited on Mar 1, 2013 at 12:02am
Feb 28, 2013 at 11:59pm
You could show some code to start with, something to build upon.
Mar 1, 2013 at 12:03am
I'm having trouble with the displayHigh and displayLow function definitions
Mar 1, 2013 at 12:15am
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(const double& 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 
Last edited on Mar 1, 2013 at 12:16am
Mar 1, 2013 at 12:18am
For the display high you could use:
void displayHigh(double rain[], int numElements)
{
double temp = 0.0;
double high;


for (int i=0; i<numElements; i++)
{
if(rain[i] > temp)
temp = rain[i];
}

high = temp;
cout << high;
}


or something of that sort... basically the same for displayLow:

void displayLow(double rain[], int numElements)
{
double temp = 1000.00;
double low;

for(int i =0; i<numElements; i++)
{
if(rain[i] < temp)
temp = rain[i];
}
low = temp;

cout << low;
Last edited on Mar 1, 2013 at 12:20am
Mar 1, 2013 at 12:28am
Then what would I do for the lowest amount of rainfall?

Thanks btw!:)
Mar 1, 2013 at 12:31am
Oops didn't see your reply atriumheart, Thank you so much guys.
Topic archived. No new replies allowed.