Barometric Pressure Problem


You are working on a project that requires climate data for a location. The maximum daily change in barometric pressure is one aspect of the climate that your company needs. You have a file (barometric.dat) with hourly barometric readings taken over a course of a year. Each line in the file contains the readings for a single day, separated by blanks. Each reading is expressed in inches or mercury so it is a decimal number ranging from about 28.00 to 32.00. For each line of data you need to determine the max and min reading and output the difference between those reading to file differences.dat. Each output value should be on a separate line of the file. Once the file has been read, the program also output the greatest and least difference for the year on cout. Develop the program using functional decomposition and use proper style and documentation for your code. Your code should make appropriate use of value returning functions in solving this problem.Write your question here.
Also, your code should output the maximum and minimum barometric readings for each day on file differences.dat, and then output the maximum and minimum readings for the year on cout at the end of the run.



I feel like using an array for the numbers would be helpful. I am very new at this and am hoping I wont fail this class. I have some code, but its not much.


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

using namespace std;

int main()
{
float Max; // Daily maximum
float Min; // Daily minimum
float Diff; // Daily difference
float YearMax; // Year maximum
float YearMin; // Year minimum

ifstream inData;
inData.open ("barometric.dat");

ofstream outData;
outData.open ("differences.dat")
http://www.cplusplus.com/doc/tutorial/files/
See the section called "text files" for an example on reading a file.
The each line will have to be parsed and perhaps read each value into an array (or better a vector if you are allowed to use them).

Do that first.
When you say you are new at programming I would ask have you been shown how to read/write files and search lists and so on? If not why would you be asked to undertake such a project?

Programming is simply writing down (or imagining) the steps you would take to do the task and then translating them into steps written in some computer language which you must learn first.

Try first to work out, step by step, the logic required and then figure out how to convert each step into C++ code that would carry out the logic.

1
2
3
4
5
6
7
8
9
10
11
12
13
For each day (outer loop)
   Read from file line 
   max = 0:min = 999
   For each item in the line (inner loop)
     extract item from line
     if item > max then max = item
     if item < min then min = item
   Next item
   diff = max - min
   Write to file diff
Next day
if max > yearlyMax then yearlyMax = max
if min < yearlyMin then yearlyMin = min

Last edited on
We have not technically been taught to use arrays or vectors yet, but I saw one in a forum in my hunt for answers and it seemed helpful. I think my book might have brushed over reading a file but I can never get my outward files to work right with my code. I am taking a summer coding class, which is why I need the help. I can use loops and just learned void functions. I can do the logic behind the code just fine, I just struggle with the "getting it to computer language part", I can talk through what I need to do.

I know I need to find the largest number on each line, and mark it as the daily max; then i need to find the smallest on each line, and mark it as the daily min; however, I know that each daily max should be saved separately so you can take each of the daily max(s), and find the largest and the smallest. You would then mark those as the yearly max and yearly min respectively.
It is unclear what you are asking? Someone to do the assignment for you? If you have not been taught to use arrays or write/read files then how does the tutor expect you to do the assignment? You will have to learn the basics of c++ first and then come back to the assignment.
Last edited on
If you have not been taught to use arrays or write/read files then how does the tutor expect you to do the assignment?

After reading the problem statement, it appears to me that this assignment can be done without the need of arrays.

The output file should contain the differences between the minimum and maximum reading for the day. So you can just look for the minimum and maximum value in each line and then write the difference between the minimum and maximum to the file

For the yearly figures you need find the minimum and maximum values. You can do this by comparing the daily minimum and maximums to a yearly minimum and maximum value.

Yes I agree you do not need arrays but tcanna's problem appears to be a general lack of knowledge of even file i/o so I don't know what they are teaching in class. It is not a hard assignment but it not a beginner piece of code either. You need to build up your knowledge in working stages with lots of practice.
Anyway this is a little file demo of generating some random barometric data in the 28.00 to 32.00 range which it saves and then loads and prints the result. Modified you can process the data and save the result of the processing instead of just printing the data.

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
// writing on a text file
#include <iostream>
#include <fstream>
#include <cstdlib>  // for rand()
using namespace std;

int main () {
    float pressure;

    //  ***   create a data base  ***
    ofstream newData ("barometric.dat");
    if (newData.is_open())
    {
        for (int i=0; i<=365; i++)  // 365 days
        {
            for (int j=0; j<24; j++)   // 24 hours per day
            {
                pressure = rand()%400+2800;
                pressure = pressure / 100;
                newData << pressure << " ";   // send a pressure value to file
            }
            newData << endl;  // send a new line to file
        }

        newData.close();
  }
  else cout << "Unable to open file";

  // ****  FINISHED CREATING DATA BASE *****

  string line;
  ifstream inData ("barometric.dat");
  if (inData.is_open())
  {
    while ( getline (inData,line) )
    {
      cout << line << endl;
    }
    inData.close();
  }
  else cout << "Unable to open file";

  return 0;
}

Last edited on
I am struggling to understand how the code from (CodeWriter) uses subfunctions. The pressures are given to me, I need to find the largest on a line of the file and the smallest on the line of the file. I need to do that for all of the lines and then compare the largest and smallest from each to get the overall largest and smallest. Is there a way to do the largest and smallest of each line in one step (rather than comparing each value to the next)?
closed account (48T7M4Gy)
No, there isn't.
My example does not use functions. You can take the segments of code out of the main and call them as functions. Surely they have explained how to do that!!

My assumption here is that the numbers are stored in a text file as characters? I have changed the code to generate a complete text file of 365 days of 24 hours of data as text so you can view it with NotePad. Is your barometric.dat file a text file? There are other types of files called binary files. In the code below the 24 values per day are extracted within the j loop to do with what you want.

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
// writing on a text file
#include <iostream>
#include <fstream>
#include <cstdlib>  // for rand()
using namespace std;

int main () {
    float pressure;

    //  ***   create a data base  ***
    ofstream newData ("barometric.dat");
    if (newData.is_open())
    {
        for (int i=0; i<=365; i++)  // 365 days
        {
            for (int j=0; j<24; j++)   // 24 hours per day
            {
                pressure = rand()%400+2800;
                pressure = pressure / 100;
                newData << pressure << " ";   // send a pressure value to file
            }
            newData << endl;  // send a new line to file
        }

        newData.close();
  }
  else cout << "Unable to open file";

  // ***************************


    // read data and print numbers
  string line;
  ifstream inData ("barometric.dat");
  if (inData.is_open())
  {
    while ( getline (inData,line) )
    {
        for (int j=0; j<24; j++)
        {
            inData >> pressure;
            cout << j << pressure << endl; // << " ";
        }
        cout << "*****************" << endl;
    }
    inData.close();
  }
  else cout << "Unable to open file";

  return 0;
}


Last edited on
Wow, getting the data, I never even thought of that. I guess its a text file? We haven't learned about binary files yet. Do you have any advice on what subfunctions to use to create the program. Is there a subfunction to find the least or greatest number on a line of text? I am very lost with this assignment and my teacher is on vacation.
On the code that was given, I am having difficulties getting it to even build. I get an error involving "getline" it claims it is undefined along with several of my braces claiming "Expected a statement" Any clue how to fix it? The assignment is due this Sunday and I still don't know where to start!
tcanna,
Assignments are there to show what you know and clearly you don't know enough to complete the assignment. For anyone to do it for you (and I have come close) would defeat the whole purpose of the assignment. You should not be concerned about the assignment you should be concerned about the failure of the course to teach you what you would need to know to complete the assignment.

I am teaching myself c++ from books. I use the code::blocks IDE and select the console application project template as a starting point. The code compiles and runs ok for me. I am not an expert and had to rummage through my books and find solutions when writing the code I have given you.


Thanks for the advice, I read over some powerpoints and while my code isn't very condense, I was able to get about 3/5 of the way through the assignment. I am using int subfunctions to do the daily min and max, and then a separate int subfunction for the difference. Now I just have to figure out how to use my original output file as an input file to compare the mins and maxes to get the overall. My cousin, who is really good at code, suggested I start with a step-by-step plan of what I wanted to accomplish and then begin deciding what I need to say to the computer to accomplish it. Thanks for all the help and I'll be sure to post my finished code when I figure out how to write the rest of it. Thanks for a great starting point!
tcanna,
Best wishes with your efforts.
May I just add wanting to be able to code isn't the same as enjoying the process of coding.

My cousin, who is really good at code, suggested I start with a step-by-step plan of what I wanted to accomplish and then begin deciding what I need to say to the computer to accomplish it

Very good advice. Advance planing and thoroughly understanding the problem is essential prior to starting to write your program.

Now I just have to figure out how to use my original output file as an input file to compare the mins and maxes to get the overall.

While that may be one way to accomplish the objectives it isn't the only method and maybe not the most efficient method either. Why don't you just keep track of the yearly minimum and maximum as you go along?

If you posted your current code, perhaps someone might point you in the right direction.

Here is the code I got to work
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

float MinMaxDiff (ifstream& inData, // To establish the subfunction
float& yearlymin,
float& yearlymax);
int main()
{
static float yearlymin;
static float yearlymax;
yearlymin = 32.0;
yearlymax = 28.0;

ifstream inData; //Opens the input file
inData.open ("C:\\Users\\Documents\\barometric.dat");

ofstream outputFile; //Opens the output file
outputFile.open ("C:\\differences.dat");

for (int i = 0; i < 365&& inData; i++)
{
float diff;
diff = MinMaxDiff(inData, yearlymin, yearlymax);
outputFile << diff << endl;

}
cout << "The Yearly Max is: " << yearlymax << endl;
cout << "The Yearly Min is: " << yearlymin << endl;

system ("PAUSE");

return 0;


}

float MinMaxDiff (ifstream& inData,
float& yearlymin,
float& yearlymax)
{
float min = 32.0;
float max = 28.0;
float num = 0;
inData >> num;

for (int i=0; i < 24&& inData; i++)
{
if(num > max)
{
max = num;
}
if(num < min)
{
min = num;
}
if(num > yearlymax)
{
yearlymax = num;
}
if(num < yearlymin)
{
yearlymin = num;
}
inData >> num;
}
return max - min;
}
Just because something works doesn't mean the logic is right you need a lot of test data.
1
2
yearlymin = 32.0;
yearlymax = 28.0;

You need to change that to,
1
2
yearlymin = 9999.0;
yearlymax = 0.0;

Because in another data set the min may be greater then 32 and the max might be less then 28.

Also when you click new topic you will see (code) Put the code you need help with here. (/code) except the () brackets will be [] brackets because can't use the square brackets in this text without triggering the action. Leave them there and replace "Put the code you need help with here." with your source code.
Last edited on
Topic archived. No new replies allowed.