How do I pull out the data file to find Average? Logic Help

How do I calculate the average for the data? The issue I came across was how do I pull those blood pressure readings out of the text file and add them all up to divide them by my variable howMany.

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

int main(){
	//Initialize Required Variables For Program
	int patientCount = 0;
	string id;
	int avg = 0;
	int howMany = 0;
	ifstream reader ("data.txt"); //Open The Data File To Be Read From

	while( reader >> id && reader >> howMany ){ //as long as you can read another pacient data
		int sum = 0; //sum accumulates the pressure readings per pacient
		cout << "The Patient ID Is: " << id << endl;
		cout << "The Number Of Blood Pressure Record This Patient Has Is: " << howMany << endl;
		for(int K = 0; K < howMany; ++K){
			int number;
			reader >> number;
			sum += number;

		}
		//going to complete average here but I don't know how to pull out the data

	}
	system("pause");
	return 0;
}





the text file looks like this


1
2
3
4
5
6
7
8
9
10
11
12
4567 4 180 140 170 150
4693 1 119
4690 4 200 120 135 136
4693 2 149 133
4783 3 133 123 140
4824 3 130 155 120
4833 2 119 186
4690 4 172 143 189 142
4693 2 149 133
4783 3 133 123 140
5643 2 125 150
5643 2 125 150


1st column is Patient ID
2nd column is How Many Tests the patient had
3rd column and beyond is all the blood pressure readings.

UPDATE: This is what I came up with but it is still not working. Can somebody please help me?

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

int main(){
	//Initialize Required Variables For Program
	int patientCount = 0;
	string id;
	int avg = 0;
	int howMany = 0;
	ifstream reader ("data.txt"); //Open The Data File To Be Read From

	while(reader >> id && reader >> howMany ){ //as long as you can read another pacient data
		int sum = 0; //sum accumulates the pressure readings per pacient
		cout << "The Patient ID Is: " << id << endl;
		cout << "The Number Of Blood Pressure Records This Patient Has Is: " << howMany << endl;
		for(int K = 0; K < howMany; ++K){
			int number;
			reader >> number;
			sum += number;

		}

	//going to complete average here but I don't know how to pull out the data
	while( reader >> avg){
		avg = (avg + avg + avg + avg + avg) / howMany;
		cout << "The Patient Average BP Reading Is: " << avg << endl;
		}
	


	}
	system("pause");
	return 0;
}
Hey there.

Well, you implemented the reading from the file, but you did not STORE the data anywhere to allow you to do what you want.

My suggestion would be to implement a structure, like this.

1
2
3
4
5
6
struct patient 
{
       int ID;
       int NumberOfReadings;
       int Reading[10];
};


Then, you make a array of patients:

patient Patients[50];

And then you read the file and fill Patients[] with the goodies:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int patientCount = 0;
ifstream reader ("data.txt"); //Open The Data File To Be Read From

for (i = 0; i<50; i++)
{
      if ( ! (reader >> Patient[i].ID && reader >> Patient[i].NumberOfReadings ) ) //*same as your while
      {
              patientCount = i; //*if cant read, then the number of patients is the current i
              break; //*if cant read, breaks the for loop
       }


       for (j=0; j<Patient[i].NumberOfReadings; j++)
       {
             reader >> Patient[i].Reading[j];
       }
}



* I did not test this code, this is just to give you a idea of how to do what you want.

** This is not very elegant, since we are allocating a hard number of memory for patients and readings that may not be used, and may not be adequate for all situations. If your files have sometimes thousands of patients, maybe with hundreds of readings, its best to implement a dynamic method to allocate the resources, instead of arrays. But, I recommend getting this one done first.
Last edited on
@shoyoninja Thanks for the reply but this is just for a project for one of my college classes and our professor didn't explain how to take things out of the file. The situation I'm having is that I HAVE TO USE nested loops like my code above. There isn't another possible way to just take those averages one by one and add them up then divide them?


EDIT: I also need only a minimum of 20 patients.
Last edited on
So it wouldn't be possible without an array?
Well, the sample I gave uses a nested loop.

Its possible without arrays, but in my opinion would make it more complicated and less organized. You cant use arrays?
Topic archived. No new replies allowed.