Wyatt Bush
Wyatt Bush

Reputation: 5

How can I find the highest and lowest values in this array?

What code can I use to print out the highest and lowest values that the user inputs in this array? This program needs to take the average of the user input (which I've already done). Now all I need to do is have the program print out the highest and lowest values that the user input.

#include <iostream>
using namespace std;
int main()
{
float days = 0;
float temperatures [50];
float temptotal = 0;
float average = 0; 
cout << "Enter the number of days: ";
cin >> days;
if (days > 50)
{
    cout << "You may only enter temperatures for 50 days." << endl;
    return 0;
}
for (int i = 1; i <= days; i++)
{
    cout << "Enter the temperature for day number " << i << ": "; 
    cin >> temperatures[50];
    temptotal += temperatures[50]; 
}
average = (temptotal / days);
cout << "The temperature average is: " << average << endl;
return 0;
}

Upvotes: 0

Views: 8699

Answers (3)

striving_coder
striving_coder

Reputation: 798

Please look here: C++ function to find max value in an array of doubles? Essentially, what you do is:

1) add

    #include <algorithm>

at the top of your code

2) use std::max_element and std::min_element in your code, e.g. like this

    double highest = *std::max_element(temperatures, temperatures + days);
    double lowest = *std::min_element(temperatures, temperatures + days);

You might want to look up these methods here: http://www.cplusplus.com/reference/algorithm/max_element/ and http://www.cplusplus.com/reference/algorithm/min_element/ (even though please be advised that many people look down upon cplusplus.com considering it not the best sourse of information on C++, but I guess in this case it's fine)

Alternatively, as Eugene Sh. pointed out and Brett showed, you can just do it yourself by storing current highest (lowest) value and comparing it with each new input value.

Also, as Brett noticed, you want to change temperatures[50] to temperatures[i] in your reading loop. But there is another correction: this loop should run from 0 up to less than days:

for (int i=0; i < days; i++)

since arrays are indexed starting from 0, and in case of maximum number of values (50), you will fill array's elements from 0 to 49 (which is days-1) inclusively, not from 1 to 50.

Upvotes: 0

EverydayLearner
EverydayLearner

Reputation: 302

There is a bunch of ways to do this. But an easy way to do it is to check if the current value that is added to the array is the lowest/highest. First, you need to assume that the min correspond to a very high number and the max correspond to a very low number.

float min = 9999999999999;
float max = -9999999999999;

After this, you can compare if the value is lower than the min or higher than the max. Since the initial values are the opposite of what they are supposed to be, the first value will be set as the min and max and then the others will be compared with this value. By doing so you will keep only the lowest and the highest value.

if(temperatures[i] > max)
    max = temperatures[i];
if(temperatures[i] < min)
    min = temperatures[i];

If you noticed, I used temparatures[i]. If you always add the value entered by the user at the index 50, you are always overwriting the last value entered. Also, the indexes of an array usually starts at 0 and not at 1, so your for loop will look like this : for (int i = 0; i < days; i++).

I edited your code and this is what I ended up with :

#include <iostream>
using namespace std;
int main()
{
    float days = 0;
    float temperatures [50];
    float temptotal = 0;
    float average = 0;
    cout << "Enter the number of days: ";
    cin >> days;
    if (days > 50)
    {
        cout << "You may only enter temperatures for 50 days max." << endl;
        return 0;
    }
    float min = 9999999999999;
    float max = -9999999999999;
    for (int i = 0; i < days; i++)
    {
        cout << "Enter the temperature for day number " << i << ": ";
        cin >> temperatures[i];
        temptotal += temperatures[i];

        if(temperatures[i] > max)
            max = temperatures[i];
        if(temperatures[i] < min)
            min = temperatures[i];
    }

    average = (temptotal / days);
    cout << "The temperature average is: " << average << endl;
    cout << "The highest temperature is: " << max << endl;
    cout << "The lowest temperature is: " << min << endl;


    return 0;
}

Hope this helps !

Upvotes: 1

Brett
Brett

Reputation: 256

Wouldn't you just keep a local variable with the max and min value seen? You are walking through all the items entered anyway?
`

float max=0;
float min=999;
for (int i = 1; i <= days; i++)
{
    cout << "Enter the temperature for day number " << i << ": "; 
    cin >> temperatures[50];
    temptotal += temperatures[50]; 
    if temperatures[50] > max
        max = temperatures[50];
    if temperatures[50] < min
        min = temperatures[50];
}

Please forgive my coding since this isn't my language. Also I don't totally get your code. Maybe you don't mean to be using temperatures[50] all the time. Maybe you mean temperatures[i]?

Good luck.

Upvotes: 0

Related Questions