How to print 3 values per line

My program reads 366 lines of data from a file, each line has 2 values; minimum temperature and maximum temperature. I want to output when there were three consecutive days where the temperature went above a certain number n, that the user enters.
This is what I have:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
    cout<<"Please enter a number to search: ";
    cin>>n;
    out<<endl;
    out<<"Occasions during the year when there were three consecutive days where the temperature went above "<<n<<" are:"<<endl;
    out<<"Days: ";
    for(int x=0; x<366; x=x+1){
                in>>minimum[x];
                in>>maximum[x];
                
            
                 if(minimum[x]>n){
                  day1=x;
                }

                 if(maximum[x]>n){
                    day1=x;
                }
                out<<"Days: "<<day1<<", "<<day2<<", "<<day3<<endl;
                }
                 
    }


I'm having trouble updating day 2 and day 3 to a different element of the array that satisfies the condition. When the condition is met, I want to store the days and print them like:

Occasions during the year (if any) when there were three consecutive days where the temperature went above 34 are:
Days:103, 104, 105
Days:107, 108, 109
Days:288, 289, 290

the days are the locations in the array.
Last edited on
1. If maximum is over the limit, then the minimum is over the limit too. You have to decide whether it is sufficient for maximum to exceed the limit, or do you require each days minimum to exceed the limit. In either case you need only one test.

2. It does look like that you read data into array(s), but you don't hint that you need those arrays anywhere else and you do use the values as they come. In other words, there is no apparent reason to use arrays at all. It would be different, if you would read data to array in one loop and do computations is separate loop.

3. What if days 42--46 are hot? Will you print:
Days: 42, 43, 44
Days: 43, 44, 45
Days: 44, 45, 46

4. What if you keep track of count of consecutive hot days?
hots=0 at start
IF day is hot, THEN ++hots, ELSE hots=0
IF 2<hots, THEN day-2, day-1, day were hot
Hello ElleJay,

A couple of comments first. The variable "n" could use a better name the reflects the number it holds. It makes the program easier to read and for others to better understand.

The two if statements inside do not seem to work well together. I am thinking they should be in separate for loops or maybe need to be done differently.

To the if statements you are checking only one day when you should be checking three days like:
if(minimum[x] > n && minimum[x + 1] > n && minimum[x + 2] > n).

After that I would try making the second if statement an else if statement and set it up like the first if.

Line 18 needs to be in an if statement or it will print in each iteration of the loop even if it should not. For the actual statement it would be <day1 + 1<<", "<<day1 + 2 .... There is no real need for the "day2" and "day3" variables. the "+1" on "day1" is because the array is zero based, so element zero would correspond to day 1. You are not wanting to print out the position in the array, but the day of the year.

I noticed that the arrays are filled at the beginning of the loop. This will not work because you will be trying to check elements of the array that do not yet exist. This input needs to be done before this for loop so that the arrays are filled before used.

Hope that helps,

Andy

Edit:
Last edited on
Hello ElleJay,

just curious if there is any way I could download the data file that you are using.

Andy
Topic archived. No new replies allowed.