You doubt about loop for

0

I have two weeks, there is an exercise where the program has to say the lowest temperature and the highest of all that you have set, how would it be done? . Thank you.

#include<iostream>

using namespace std;
 float t1,temperatura_media,t2,t3,t4;
int main(){
    for(int x=0;x<1;x++){
        cout<<"Pon la temperatura 00:00: ";cin>>t1;
        cout<<"Pon la temperatura 06:00: ";cin>>t2;
        cout<<"Pon la temperatura 12:00: ";cin>>t3;
        cout<<"Pon la temperatura 18:00: ";cin>>t4;

    }
    temperatura_media= (t1+t2+t3+t4)/4;
    cout<<"la temperatura media es: "<<temperatura_media<<endl;
    return 0;
}
    
asked by user102246 03.10.2018 в 19:42
source

1 answer

0

I do not know exactly why you put the count into a for that only runs once. It's illogical. But in the end, for what you need to do there are many ways, some include the use of structures, arrays, vectors or simple conditions. The simplest is to do it by conditions, type: if(t1>t2) , but it is also the most tedious since you have to consider all the cases without mentioning that it is not very efficient.

The simplest form and at the same time the one that I consider most effective is the following:

#include <algorithm>
#include <iostream>
int main()
{
   float temperaturas[4] = {25.35, 18.40, 21.29, 25.14};
   sort(temperaturas, temperaturas+4);

}

Source: link

Now once ordered to obtain the minimum temperature you can do it with temperaturas[0] and the maximum temperature with temperaturas[3] .

Greetings!

    
answered by 03.10.2018 в 20:08