average of numbers from a file.

Hi. I need a little help on a program that I am writing for school. So please do not give me the full blown answer. I would appreciate it if you just pointed me in the right direction. This is also a beginning c++ class so if you could take that into consideration while giving your answer.

I have to write a program that takes the lowest, the highest and the average of different temps from different distances off the ground. I know how to view the certain temperatures. I just do not know how to add the unknown amount of variables.I was thinking about using an array, because that might be the easiest way. But honestly I have no idea where to get started with that.

The file looks like this.

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  
3  12:57:08  2.525235  -22.4  -17.8  -14.1  


I pretty much have to take in the first 3 columns and ignore them. So for 1m I have to take the average, at half a meter take the average and so on.


This is 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
/*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()
{
  
    string name, local; 
    double mm=0;
    double hmm=0;
    double qm=0;  
    double sol;  
    int day; 
    int lines; 
    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 nummm, numhmm, numqmm; 
          indata >> day >> local >> sol >> mm >>hmm >>qm; 
         
        
          cout << mm ; //check to see the temperatures a meter off the ground
               
        
                   }
    
    outdata << ("output.dat");//save data as a file
    system ("pause");
    return 0; 
    }


Any help would be greatly appreciated. Thank you.
You can use std::vector (from the <vector> header) if you need something like an array, but with varying size.
well i added this code in the while loop instead and i got the average.


1
2
3
4
5
6
7
8
9
10
11
12
         double nummm, numhmm, numqmm;//amount of numbers
         double mmavg, hmavg, qmavg;//averages


        nummm+=mm; numm++; //incremented variables and declared them to a double 
        numhmm += hmm; numhm++;
         numqmm+= qm; numqm++; 

         //took the average of the variables
         qmavg=numqmm/numqm; // average of a quarter of a meter
          hmavg=numhmm/numhm;   //average of half a meter      
          mmavg=nummm/numm;//average of a meter 


i just incremented it and added em all up with the += operator.
then divided.
i know..a little confusing.
but my professor told me that as long as i have it done. even if its ugly.
and this is pretty ugly. haha
Actually, this is a good idea, but 2 things:
Amounts should be integers. You can't have 2.5 meter values, you can only have 2 or 3- using doubles for that doesn't make too much sense.

And change the way you name variables. For example,
dQmAvg //double quarter meter average is a lot more readible than qmavg
Ok. Thanks. That makes reading the code a lot easier to understand.
Also I had another question. Turns out that I have to take the average of both days. So the first one is day 2 and the second one is day 3. How would I just take the averages of the lines with that day and separate them?
I know I should probably use another while loop, such as

while (day=='2')
do this
else
do this.

but how i would actually do it is a little confusing to me..
any help would be appreciated.
You could use a map to create a new total value for each day. If you aren't allowed to use STL containers, you will have to implement the functionality yourself.
I don't think we've even gone over maps. So i'd proabably have to do it myself.

I'm gonna see if i can do if statements instead of while loops. since the days only have day 2 and day 3 i'm gonna see if i can do something like:

if (day=='2')
then do what i did in the while loop.
if (day =='3')
do what i did in the while loop.

i think it should work. i might have to create another set of variables though..

ill let you know if it works.
Yeah that didnt work. It just finds the average of everything instead of giving me the average for that specific day. Ill keep trying though.
The problem is that you can't rely on there only being 2 days, and those always being '2' and '3'. You can't even rely on them being single digit.

In this case, you can do something like this:

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
vector<string> days;
vector<double> average1, average2, average3;
string dayInput;
double metersInput, halfMetersInput, quarterMetersInput;
metersInput = halfMetersInput = quarterMetersInput = 0;
// here you do the file input and stuff
if(days.size()==0)
{
      days.push_back(dayInput);
      average1.push_back(metersInput);
      average2.push_back(halfMetersInput);
      average3.push_back(quarterMetersInput);
} else {
     int i;
     for(i = 0; i<days.size(); i++)
     {
          if(dayInput.compare(days[i])
          {
                average1[i] += metersInput;
                average2[i] += halfMetersInput;
                average3[i] + = quarterMetersInput;
                break; 
           }
     }
     if (i == days.size())
     {
           days.push_back(dayInput);
           average1.push_back(metersInput);
           average2.push_back(halfMetersInput);
           average3.push_back(quarterMetersInput);
      }
}


This is a bit ugly, but it should work. If you have questions about this, ask.
Well the thing is we havn't gone over vectors yet. And I know my professor will ask me how i know this when we havnt covered it. and the thing is he told me that the only thing that I need to follow was the directions. and the directions said to do the file. And the file only includes day 2 and day 3. So i'm sure that will work.

I just have no idea how to do it. When I output the days just doing cout it outputs '3'. instead of the entire list of days such as

2
2
2
2
2
2
3
3
3
3
3

this is why when code:

1
2
3
4
5
6
7
8
9
10
   
if (day =='2')
        nummm+=mm; numm++; //incremented variables and declared them to a double 
        numhmm += hmm; numhm++;
         numqmm+= qm; numqm++; 

         //took the average of the variables
         qmavg=numqmm/numqm; // average of a quarter of a meter
          hmavg=numhmm/numhm;   //average of half a meter      
          mmavg=nummm/numm;//average of a meter  


it gives me some funky numbers and answers.
but when i enter
if (day =='3')
it gives me the average for both days.

so to make this question short. how do i only find the average of the numbers with a certain number starting the line? with this im sure i can just easily find out day 3.
 
and the directions said to do the file. And the file only includes day 2 and day 3. So i'm sure that will work.  


If you already think like that, you might aswell declare a variable for each single input you get from the file and say "well, it works with that file, task solved". I don't think that's what you should be aiming for. Side note, can you really read all those nummmmms and omnomnoms? I know I can't O_o
Ok. Let me correct what I said. I know it will work like that because the directions stated that and he told us as long as the directions are followed we get full credit. He gave us the input file which only had 2's and 3's. This is why I know it will work.

And i changed everything up just now and my code looks a lot smoother than it once was thanks to you so it doesn't have all those nummmmms and omnomnoms.lol But it still is a little confusing..

I'm a beginner so idk how many variables I need. I'd rather have a lot more and have the program work than have a lot less and not work.

I don't have to go back to and read what I did it because once i get it done its over. I probably won't be using this program in the real world because I probably wont be reading temperatures from mars anytime soon. and if i do im sure that it will a lot more confusing than the sample of the file i posted. I know that it's essentially for you to understand so i understand where you are coming from.

I would probably take your advice about declaring each input. But...there are over 100 temperatures.lol

It's just rather confusing..so I'm sorry if I'm confusing you.

I'll just see if i can do it by reading several chapters ahead in the book.
Thanks for your advice.

Last edited on
Actually, the thing about declaring each input as a variable was an example of how you do NOT do it.

Alright, there is another (ugly) option that only requires arrays, and as little dynamic allocation as possible: You read the file twice, the second run is saving the averages, the first one parses only the days, converts them into integers, and then you substract the smallest day from the largest day to get the number of different days, create a double array of that size, and then write your various averages into that array. That's definitely gonna work, is easy to implement and should be on your level of knowledge (you DID cover pointers already, right?). That way you could deal with any numbers of days.
Topic archived. No new replies allowed.