Average of lines that start with certain letter.

I need a little help getting the average of only specific lines in a file. The file looks like this.


Day
2  23:06:26  2.937039  -64.9  -65.5  -66.9  
2  24:04:28  2.976262  -67.8  -68.5  -70.2  
3  00:51:55  3.035084  -69.3  -70.0  -71.1  
3  01:49:57  3.074307  -70.4  -70.7  -73.4  


how do i find the average of just day 2 and just day 3?

Any help would be appreciated. Thank you.
As you read through the file, check the first digit. If it is whatever number you want, include it in the average, otherwise just skip over it.
I know what i wanted but i have no idea HOW to do it.

i was thinking of doing something like

if (day=='2')
//find average
if (day=='3')
//find average

but for some reason its not working..

and i think i would have to have 2 sets of variables.
Last edited on
Do you know how to read from a file? If so, write some skeleton code that works and averages everything in the file, then try to modify it to work for particular lines. If not, start by reading a tutorial on file I/O.
Read it line by line. Whenever the first digit (or digits) represent the day you want, add their set to the average calculation. If the file is guaranteed to be ordered, you can stop reading when you find a different day.
I already did what you suggested Zhuge. I have all the averages from the file but when i try to modify it day 3 works(probably because the last number in the file is day '3') but day 2 gives me some funky numbers. here's my code so far.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/*make a program that reads different temperatures from a file 
from different distances off the ground and find the lowest temp, the highest
temp, and the average for each day. Then output them to a file */

#include <iostream>
#include <fstream>
#include <string>

using namespace std; 

int main()
{
    int day; 
    string local, name; 
    double sol, Day2MAvg, Day2HmAvg, Day2QmAvg, Day3MAvg, Day3HmAvg, Day3QmAvg;
    double totalDay2M, totalDay2Hm, totalDay2Qm, totalday3M, totalDay3Hm, totalDay3Qm; 
    int NumDay = 0; 
    int Days = 0; 
    ofstream outdata ("output.dat"); 
    ifstream indata ("pathfinder234X.dat");
    
    
    
    
   if ( !indata )//if file doesnt open identify the user of the problem. 
      {
      char cont; 
                 cout << "There is a problem opening the file" <<endl; 
                           
       }
       else 
       cout <<"The file opened for reading"  <<endl; 
       getline(indata, name);//skip the first four lines. 
       getline (indata, name); 
       getline (indata, name); 
       getline (indata, name);  
       
       while (!indata.eof()) //skip the first few variables then read the temperatures. 
       {
          
double DayM, DayHm, DayQm; 

          indata >> Days >> local >> sol >> DayM >>DayHm >>DayQm; 
                      NumDay++; //increment the number of days
                      
           totalDay2M+=DayM; //add and make the total 
           totalDay2Hm+=DayHm; 
           totalDay2Qm+=DayQm; 
                        
            Day2MAvg=totalDay2M/NumDay;//find average of the days
            Day2HmAvg=totalDay2Hm/NumDay;
            Day2QmAvg=totalDay2Qm/NumDay; 
    
    
}

cout <<Day2MAvg <<endl; //out put the days onto the screen
cout <<Day2HmAvg <<endl; 
cout <<Day2QmAvg;  system ("pause");
    return 0; 
    }


edit: I found the problem and fixed it. So now its working and it gives me averages.
Last edited on
Oh and sorry this is part of the file that i have to read from.

 Mars Pathfinder temperature data 
 ~ 24 hrs over two Solar days
Sol Local T  Dec Sol  Temperature (Celsius) 
                        1.0m   0.5   0.25m 
2  11:59:08  2.486035  -25.4  -20.8  -17.4  
2  12:28:08  2.505635  -23.4  -18.8  -14.9  
2  12:57:08  2.525235  -22.4  -17.8  -14.1
3  00:51:55  3.035084  -69.3  -70.0  -71.1  
3  01:49:57  3.074307  -70.4  -70.7  -73.4  
3  02:18:58  3.093918  -71.3  -71.5  -73.2  
3  02:47:59  3.113530  -71.9  -72.3  -73.9    

It looks like you are reading the first number into "Days", so I would simply put an if statement around the parts after you read in the data and only sum in the data if "Days" is equal to whatever you are looking for (in this case, 2).

Also, I'd personally move the average calculation outside of the loop. There is no reason to calculate it every loop when you only care about the average of all the data you've read.
I know this might be asking too much but can you show me how its done? I have the average for day 2..but the Day 3 average is giving me some weird numbers that i cannot seem to find. 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
22
23
24
25
 while (!indata.eof()) 
       {
          
double DayM, DayHm, DayQm; 

if (Days = '2')
{
             indata >> Days >> local >> sol >> DayM >>DayHm >>DayQm; 
          NumDay++;           

           totalDay2M+=DayM; //add and make the total 
           totalDay2Hm+=DayHm;
           totalDay2Qm+=DayQm;
           }
                  
if (Days = '3')
{
 indata >> Days >> local >> sol >> DayM >>DayHm >>DayQm; 
          NumDay++;  
          
           totalDay3M+=DayM; //add and make the total 
           totalDay3Hm+=DayHm; 
           totalDay3Qm+=DayQm; 
           }
           }
You need different NumDays for day 2 and day 3. Also, you need to set
1
2
3
4
5
6
totalDay2M,
totalDay2Hm,
totalDay2Qm,
totalDay3M,
totalDay3Hm,
totalDay3Qm

to 0 before you use them the first time.

PS: Seems like you didn't follow my advice to use arrays for the different averages for each day- but hey, at least your variables look a lot better now.
You also need to read the first number into "Days" before you try to check it. Also, since "Days" is an integer, you should check against integer literals and not character literals. And you need to use == for comparison, not = which is assignment.

In pseudocode, you need to do this:
1
2
3
4
5
6
7
Read data from the file into your variables
if Days is 2
    add to Day2 variables
else if Days is 3
    add to Day3 variables
end
repeat until finished with the file

And I also agree with hanst99, here, in that arrays would help reduce the number of variables you need.
Yea, but the professor hasn't covered those yet so it will probably make him ask questions.

By the way I got everything done by using help from all of you!

You all have been a big help. Thanks so much!
Honestly? Your professor is covering file i/o before arrays? That doesn't even begin to make sense.
Do not ask me.lol I have a hard time understanding the way he "teaches" also.
He jumps around chapters and confuses the heck out of people.
Most people dropped the class. But I can't afford to.
Which is why Im stuck taking the class.

I'm trying to learn all this programming the same time I'm learning from him.
Just in case I miss something. So now I know arrays is going on my to do list.
Topic archived. No new replies allowed.