Files assignment

This is an assignment given to me by my instructor, I have a good idea of what to do, I am just having trouble getting the temps1 files into the results file. Any help would be greatly appreciated, Thank you!

Here is the assignment:

Write a complete program that opens a file for input and a file for output, reads temperature data from the
input file and saves some statistics in the output file. The input file will look like this:

7
70.0 69.0 69.0 68.0 67.0 67.5 67.3 68.0 69.2 72.1 74.3 78.3 79.0 80.2 80.5 80.2 79.3 78.3 74.0 73.2 73.0 72.1 71.0 71.0
72.4 69.4 69.4 68.4 67.4 67.2 67.7 68.4 69.2 72.1 74.7 78.7 79.4 82.2 82.2 82.2 79.4 78.7 74.4 77.2 77.4 72.1 73.4 73.4
70.0 69.0 69.0 68.0 67.0 67.2 67.7 68.0 69.2 73.1 74.7 78.7 79.0 80.2 80.2 80.2 79.0 78.7 74.0 76.2 76.0 73.1 71.0 70.0
71.1 69.1 69.1 69.1 67.1 67.2 67.7 69.1 69.2 73.1 74.7 78.7 79.1 83.2 83.2 83.2 79.1 78.7 74.1 77.2 77.1 73.1 72.1 72.1
70.6 69.6 69.6 68.6 65.6 65.2 65.0 68.6 69.2 72.1 74.0 78.0 79.6 79.2 79.2 79.2 79.6 78.0 74.6 78.2 78.6 72.1 71.6 70.6
70.3 68.3 68.3 68.3 67.3 67.5 67.7 68.3 68.5 73.1 74.7 77.7 79.3 85.5 85.5 85.5 79.3 77.7 74.3 77.5 77.3 73.1 72.3 70.3
69.4 70.4 70.4 68.4 67.4 67.2 67.7 68.4 70.2 72.1 74.7 78.7 79.4 80.2 80.2 80.2 79.4 78.7 74.4 77.2 77.4 72.1 71.4 73.4

What you see is an integer at the beginning that represents how many rows will follow (7 rows for 7 days
of temperature data). Then each of the 7 rows has exactly 24 doubles.
The input file represents temperature readings over a 24 hour period for seven days (your program
should work for input files with more or fewer days; perhaps the input files has 2 days of data, or 600
days of data; there will always be 24 doubles for each day, however). Your program should read this file
and, for each day, find the average temperature, the high temperature, and the low temperature. This
information, for each day, should be saved in the output file.


Thanks again!!
Is it actually structured as an integer, then doubles? Or is it structured as newlines and spaces?
It is structured as an integer then doubles.
Have you written any code so far? Have you thought about the problem? From your post, it sounds like you just wanted to post the assignment and get someone to do your homework for you.
Last edited on
This is what I have written so far, I am completely confused about how to store files into an output file, I have thought about the problem.



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
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
string filename;
    ifstream input;
    cout<<"Enter filename for temperature data:"<<endl;
    cin>>filename;
    input.open(filename.c_str());
    if(!input.is_open())
    {
        cout<<"Error opening temperature data file."<<endl;
    }
    
    string filename;
    ofstream output;
    cout<<"Enter filename for results:"<<endl;
    cin>>filename;
    ouput.open(filename.c_str());
    if(!output.is_open())
    {
        cout<<"Error opening results file."<<endl;
    }
    
Ok. That won't even compile because there's two strings in the same scope with the same identifier ("filename"). And all you're doing is opening two files.

EDIT: the code you posted also checks whether the output file exists (line 22), and it will fail since you still haven't written anything to it.

This is what I suggest. You need some data structure to hold the data you want to work with. It could be a vector of vectors of doubles. I'll show you a way to read the data, and hopefully you can pick up from there.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
    ifstream ifs(filename.c_str());
    int rows;
    if(!(ifs >> rows))
    {
        // deal with error
    }
    double n;
    std::vector< std::vector<double> > temps(rows);
    for(int i = 0; i < rows; ++i)
    {
        for(int j = 0; j < 24; ++j)
        {
            if(!(ifs >> n))
            {
                // deal with error
            }
            temps[i].push_back(n);
        }
    }

This could also be done without the need to know how many rows are to read in advance, but since the assignment seems to expect you to use it, I'm using it. Once that code is executed, you'll have a vector of vectors of doubles holding all the data you need.
Last edited on
I caught the filename error, however in my class we have not studied vectors, I think that this assignment was meant to be written with just files
You need a data structure to be able to compute the average and the extreme temperatures for each day. It's either a STL container like std::vector or a raw dynamically allocated array (since you don't know at compile time how many rows are going to be read).

What you could do (though it would be a little sloppy) would be to use an array of 24 doubles, read a day in, sort the data and write the output to a file, then read another day into the same array and repeat the process. Reading everything before doing computations would be preferred because if anything goes wrong with the reading you can just abort the whole thing, instead of being left with a half written output file.
Last edited on
You need a data structure to be able to compute the average and the extreme temperatures for each day.

I was thinking and actually that's wrong. If you want to read and write one day at a time, you just need three variables: average, max and min. You can accumulate the sum in average and set max and min to the initial value and compare and substitute as needed. And start all over everytime you read 24 temperatures.
Last edited on
Topic archived. No new replies allowed.