-2

So basically, I'm new to programming and can't figure it out how to add certain numbers generated by the user. I want a program, capable of collecting for n days the temperature, and at the end, inform what the peak heat occurred, as well as the average of the temperatures collected.

#include <iostream>
using namespace std;

int main()
{
    int days, day0 = 0, degrees, degrees_max = 0;
    float degrees_average;

    cout << "Days of the study" << endl;
    cin >> days;

    while (days > day0)
    {
        cout << "How many Degrees?" << endl;
        cin >> degrees;

        if (degrees < 999)
        {
            day0++;
        }

        if (degrees > degrees_max)
        {
            degrees_max = degrees;
        }
    }
    
    degrees_average = degrees / days;
    
    cout << "The max temperature was: " << degrees_max << "C"
         << " And the average was : " << degrees_average << endl;

    
}

SO the only thing left is how to calculate the average. All the help is appreciated! Thanks

Afas
  • 11
  • 1
  • 2
    An average is calculated by adding up all the values, then dividing it by the number of values. The shown code handles each value, as its entered, and it can be trivially adjusted to add them up. What exactly is your question? You already have everything needed to do this. – Sam Varshavchik Nov 21 '20 at 23:06
  • 2
    The great secret of computer programming is how you solve a problem with a pencil and paper will still work. It may not be the fastest solution, but it always gets you moving forward – user4581301 Nov 21 '20 at 23:10
  • For example: I say there are 3 days in the study (where the program asks for "..days..") , the program asks for the first temperature, the second and a third. But it only shows correctly the max temperature of these three but it fails when adding all the temperatures and diving by 3, cause there isn't any code for that. Hope I can make myself understand – Afas Nov 21 '20 at 23:12
  • The solution is simple: one has just to add the code for that. – YSC Nov 21 '20 at 23:14
  • In C++, you should be using `std::vector` for creating a dynamically sized list (if you wanted to _track_ the temperatures). Otherwise, since you're only going for the statistics here, you can just do them as you go. But as it is now, `degrees` is just your "currently read degrees", not the "sum of degrees". You need another variable (for "current sum"), or apply different math so you could continually update `degrees_average` – Rogue Nov 21 '20 at 23:28

1 Answers1

1
#include <iostream>

using namespace std;

int main()
{
    int days, max=-1;
    double current, total = 0;
    cin >> days;
    for (int i=0; i<days; i++){
        cin >> current;
        if (current > max) max = current;
        total = total + current;
    }
    cout << "max: " << max << endl;
    cout << "average: " << total/days << endl;

    return 0;
}
ZBay
  • 352
  • 1
  • 6
  • 17
  • The use of ```using namespace std;``` is [not recommended](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). Solution works fine anyway. – Tom Gebel Nov 21 '20 at 23:45