Reading data into arrays

INPUT: Redirect the input from the file HR.txt.

The file consists of three columns, with six lines of data for each person. The first line stores each person’s ID number (integer), clinically measured maximum heart rate (integer), and age (integer). The following five lines contain the day’s average commuting heart rate, maximum commuting heart rate, and exercise heart rate for five consecutive working days. Then, the six line sequence is repeated for the next person. At times the heart rate monitors reacted to nearby power lines or other objects, and gave false readings. These incorrect measurements were rejected and show up in the data file as
-1. On the days the person did not exercise, the exercise heart rate value is zero.

PROCESSING; Use precisely six parallel arrays: one for subject numbers and five for the calculated values as described below.

Using this information, calculate
1. Average of the average commuting heart rates for each person.
2. Number of days that the person exercised on his/her own.
3. Estimated maximum heart rate = 220 – age.
4. Ratio (%) of measured maximum heart rate to estimated maximum heart rate.
The maximum heart rate for each person has been measured during a maximum oxygen uptake test in the laboratory. During the test, the person exercised with increasing intensity until exhaustion, and then the maximum heart rate was measured close to the end of the test. Using this measured maximum heart rate and the estimated maximum heart rate, calculate the percentage of the measured maximum heart rate with respect to the estimated maximum heart rate. Note that this percentage can exceed 100%.
5. Ratio (%) of highest commuting heart rate to measured maximum heart rate. Find the highest maximum commuting heart rate for each person, and then calculate the percentage of this value with respect to the measured maximum heart rate.

6231 181 28
140.1 170 101.8
128.4 156 0.0
145.2 171 106.2
131.3 165 0.0
144.8 170 0.0
1124 178 36
122.8 139 138.4
119.6 142 0.0
117.8 134 133.4
115.3 145 134.2
87.2 109 120.5
7345 195 36
128.4 151 104.6
120.5 153 0.0
134.5 166 140.4
127.4 156 0.0
150.3 169 158.5
9439 197 21
121.5 143 112.2
128.9 145 0.0
126.1 159 134.5
123.0 152 0.0
131.5 147 0.0
4545 190 52
114.8 130 113.1
102.6 131 0.0
117.4 129 149.1
-1 -1 114.0
114.1 123 119.5
2438 200 26
123.0 165 105.4
118.2 130 122.0
121.7 136 124.5
116.1 130 0.0
111.0 152 103.5
2776 178 45
110.3 120 129.1
107.8 132 0.0
111.5 145 135.0
-1 -1 0.0
95.5 119 0.0
8762 186 28
122.7 146 151.9
116.0 137 0.0
119.9 145 0.0
123.3 148 150.0
121.0 142 156.3
4915 185 27
120.3 169 0.0
130.2 150 0.0
123.0 158 0.0
133.4 152 0.0
131.6 159 0.0
3521 181 51
108.3 133 119.3
-1 -1 0.0
117.6 147 125.3
106.7 131 0.0
122.7 159 0.0

I am confused where to start, so do I write the data from the file into an array? The teacher's directions are a bit confusing. Which numbers go to which array?
Any help? I don't how to do all the looping.
It looks as though there is no requirement to do any calculation of statistics etc. for the file as a whole. All you need to consider is one person at a time, if I read it correctly.

First person:
6231 181 28
140.1 170 101.8
128.4 156 0.0
145.2 171 106.2
131.3 165 0.0
144.8 170 0.0

So that would be read as follows:
ID number           6231 
maximum heart rate  181 
age                 28

Then there are five days of data
average commuting heart rate: 140.1 128.4 145.2 131.3 144.8 
maximum commuting heart rate: 170 156 171 165 170
exercise heart rate:  101.8 0.0 106.2 0.0 0.0 

So those are the three arrays, each having five elements.
First (attempt to) read the person's ID number etc.
If that is successful, repeat five times, read into the three arrays.
Do all the processing with that data.

Then repeat the whole process.

When the program fails to read an ID number, there is no more data to be read.




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

int getdata(ifstream& inputfile, int [], int [], int [], double [], double [], double []);
int main()
{
    
    int id[MAX]; 
    int max_hr[MAX];
    int age[MAX];
    double avg_com_hr[MAX];
    double max_com_hr[MAX];
    double exercise_hr[MAX];
    
    
    ifstream inputfile;
    inputfile.open("HR.txt");
    
    getdata(inputfile, id, max_hr, age, avg_com_hr, max_com_hr, exercise_hr);
    cout << "done" << endl;
    system("pause");
    return 0;
}
int getdata(ifstream& inputfile, int id[], int max_hr[], int age[], double avg_com_hr[], double max_com_hr[], double exercise_hr[])
{
     int count = 0;
     
     while(!inputfile.eof())
     {
          inputfile >> id[count];
          inputfile >> age[count];
          inputfile >> max_hr[count];
          
          for(int num = 0; num < 5; num==num)
          {
                  inputfile >> avg_com_hr[num] >> max_com_hr[num] >> exercise_hr[num];
          cout << avg_com_hr[count] << endl;
          cout << max_com_hr[count] << endl;
          cout << exercise_hr[count] << endl;
          cout << num << endl;
          num++;
          system("pause");
          }
          
          cout << id[count] << endl;
          cout << age[count] << endl;
          cout << max_hr[count] << endl;
        
          
          system("pause");
          count++;
     }
     return count;
}


I wrote some code.

This is what the professor's directions to do this, kind of don't understand step 2 and its not looping correctly
How should you do this?
STEP 1 Write main() and open file. Debug.
STEP 2 Write getdata(). At this stage there is a for loop to read each of five days nested inside a while not end of file type loop. Use debug cout statements to check that the data is being input correctly. These must be removed before final submission of project. Debug.
STEP 3 Computations including function as stipulated. Debug.
STEP 4 Write function to output heading. Debug.
STEP 5 Write output function. Debug.
STEP 6 Write function to sort using selection method. Debug.
STEP 7 “Doll up” the program documentation. Debug (just in case)


It looks like I misread the question. Previously I suggested all you have to do is consider one person at a time.

Here:
1. Average of the average commuting heart rates for each person.

Now I read that as meaning, calculate the overall average for the entire file, of a figure for each person.

But what about this?
2. Number of days that the person exercised on his/her own.

There's no hint of getting an average or overall total or anything like that. It just says a number.

I agree with your original assessment, "The teacher's directions are a bit confusing."

I'm not sure if I can add any value here - I'm more baffled now than I was before.
If I ignore the parts I don't fully understand, this is how I might read 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
#include <iostream>
#include <fstream>

const int MAX = 5;

using namespace std;

int main()
{
    int id; 
    int max_hr;
    int age;
    double avg_com_hr[MAX];
    double max_com_hr[MAX];
    double exercise_hr[MAX];
       
    ifstream inputfile("HR.txt");
    if (!inputfile)
    {
        cout << "Unable to open input file\n";
        return 1;
    }

    while (inputfile >> id >> max_hr >> age)
    {
        for (int i=0; i<MAX; ++i)
        {
            inputfile >> avg_com_hr[i] >> max_com_hr[i] >> exercise_hr[i];
        }   
         
        if (!inputfile)
        {
            cout << "error reading data for " << id << '\n';
        }
        
        cout << "person: " << id << "  age " << age << '\n';
    }

}


Note in C++ it is better to use a named constant instead of #define. Also i used the value 5 rather than 50.

I put minimal error checking in the code.

Hopefully this will give you some idea of how the loops can be structured.


By the way - it's not a good idea to loop on eof like this:
while(!inputfile.eof())
For one thing, you need to check the state of the file after the input operation, not before it, so it's a logic error to use it like that. For another thing, if there's a problem with reading from the file, an error flag such as fail() or bad() may be set rather than eof(), hence checking eof() is not safe, it may miss some important problem. In practice, this can lead to either the last row of a file being processed twice, or garbage values being processed, or sometimes an infinite loop. Best to avoid eof() altogether.
Last edited on
Why is the professor using eof to complete the code? That is what the professor wants.
Why is the professor using eof to complete the code?

At a guess, because the course syllabus was built from what the professor was taught as a student - which may also have been flawed. Another reason is that there are different programming languages, which may behave in subtly different ways.

That is what the professor wants.

This does happen. To some extent you have to tread two parallel paths, one is satisfying the course requirements and getting a good grade. The other is to at least have some awareness that there may be issues with what has been taught.
What is the purpose of eof?

I continued to write the code in that when each person's data is read from file i would do the calculations and then output it in the loop but now i found out the professor wants to sort and output the id in order. Now I don't know where to continue.
What is the purpose of eof?
I don't want to dwell too much on eof. However it stands for End of File. Usually end of file occurs after we have read all the data from the file, and we try to read one more time, but there is nothing there. That's the simple view, and right now I'll leave it there.

now i found out the professor wants to sort and output the id in order
OK, now the idea of using arrays (in addition to the ones already in use) makes some kind of sense. In the opening post it says, "Use precisely six parallel arrays" and itemises them. This is where you need to start using these arrays, and sort the data by ID - remembering to keep all of those arrays in step while you sort them.
Okay the professor told everyone how the beginning should look like.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
    int identification; int count = 0;
    double maxHR, age;
    double daysAvg, maxComHr;
    double exerHr;
    
    while (!fileIn.eof())
    {
        //inputs first line
        fileIn >> identification >> maxHR >> age;
        id[count] = identification;
        estMaxHR[count] = 220 - age;
        //gets input for next 5 lines
        for (int i = 0; i < 5; i++)
        {
            fileIn >> daysAvg;
            fileIn >> maxComHr;
            fileIn >> exerHr;
        }       
    }


She said add 3 more arrays, are the 3 arrays, day avg, maxcomhr, exerhr?

The part I don't understand is that if we are doing all the calculations, is all the information from the file being input in at once or per person and calculate from there.
Last edited on
Well, in order to try to understand this, I worked through the entire problem, including sorting, calculation, report headings and output of results.

I had to guess at what headings and what results were supposed to be output. Usually one has to navigate a path from the input of data to the output of something, but I'm not clear on what outputs are required.

I still feel I may not be following everything, even having got through to the end.

My approach was like this. I used a structure in order to hold the information for a single person. That looks like this:
1
2
3
4
5
6
7
8
struct Subject {
    int id; 
    int max_hr;
    int age;
    double avg_com_hr[MAX];
    double max_com_hr[MAX];
    double exercise_hr[MAX];    
};


Then I created an array of those objects, and read the information from the file into that array.

My main() now contains code like this:
1
2
3
4
5
    ifstream inputfile("HR.txt");
 
    Subject data[SIZE];
        
    int count = getdata(inputfile, data);

where I defined SIZE as
 
const int SIZE = 100;


Then the array data[SIZE] is the first of the six arrays. The other five arrays correspond to the five values which are to be calculated.

That gives, as specified,
Use precisely six parallel arrays: one for subject numbers and five for the calculated values as described below.


Sorry if this seems out of step with some of what you have been asked to do.
Last edited on
Topic archived. No new replies allowed.