File read, select, average.

Pages: 12
Hi, my name is Jacob and I am in beginning CMPSCI. I have one last program and I just can't figure it out. I've had a little help from tutors but I didn't have the time to finish and now I'm stuck.

Here is the prompt.
2. (Data processing) a. Store the following data in a file, or download the numbers.dat file using this link. In order for your program to use this file, save it in the same project directory where your .cpp source code file is located.
5 96 87 78 93 21 4 92 82 85 87 6 72 69 85 75 81 73

b. Write a C++ program to calculate and display the average of each group of numbers in the file created in Exercise 2a. The data is arranged in the file so that each group of numbers is preceded by the number of data items in the group. Therefore, the first number in the file, 5, indicates that the next five numbers should be grouped together. The number 4 indicates that the following four numbers are a group, and the 6 indicates that the last six numbers are a group. (Hint: Use a nested loop. The outer loop should terminate when the end of file has been encountered.)
The output of your program, when run on the data shown above, should resemble the following:

The number of elements in this group is 5
The data in this group is: 96 87 78 93 21
Average = 75

The number of elements in this group is 4
The data in this group is: 92 82 85 87
Average = 86.5

The number of elements in this group is 6
The data in this group is: 72 69 85 75 81 73
Average = 75.8333

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <fstream>

using namespace std;

const char* FILENAME = "..\\numbers.dat";

int main() {
	int nextInt, total = 0, count = total;
	fstream file(FILENAME, ios_base::in);
	while(file >> nextInt) {
		total += nextInt;
		count++;
	} 
	file.close();
	total /= count;
	cout << "The average from the file is: " << total << endl;
	system("pause");
	return 0;
}
  


I have stored the file in the correct place, but this program I have written only averages all of the numbers....not what I need. I need to be able to select the specific sets and average them accordingly. I appreciate the help in advance :)
Inside your while loop, you need another loop
This loop will take the first number and count up until 5 is reached, spit out the average then reset the total and count to zero and then get the next number.
Thank you very much for that, but I have to admit...I am very much a beginner...and I can't tell you that I know what I would use to create that. What could I use inside the second loop to count up to five to do that? Along the lines of...count, take that many, count, take that many, count, take that many. that would work because it would then just be zero. But what makes it do that?
Would it be . nvm
Last edited on
You need to change total to a double because INT doesnt use decimals, so the output will not show the fractions.

change the comments below to code and you'll be all set.

1
2
3
4
5
6
7
8
9
10
11
12
13
while ( file.good() )
{

// Read first value, I used InPut, Example file >> InPut;

	for (int i=0; i < InPut; i++)
	{
		// Read nextInt
		// add nextInt to total
	}
// Display Results
cout << "The average from the file is: " << total/InPut << endl;
}


Take out system("pause");
If you want to pause the output use getch(), I'll let you google that...

PS. you won't use count because the for loop does that for you.
Actually visual studio isn't recognizing getch(). But it is VERY cool and I thank you for all of this :D This project is actually due tomorrow. It isn't my final but it is the last quiz question I have. Should I #include something else for getch() to work?
you need this for getch
#include <conio.h>
Last edited on
So this is starting to look cleaner now all I have left is to read it and average the values right? in between the for loop. And this is going to read all three?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <fstream>

using namespace std;

const char* FILENAME = "..\\numbers.dat";

int main() {
	double nextInt, total = 0, input;
	fstream file(FILENAME, ios_base::in);
	while (file >> nextInt){
		for (int i = 0; i < input; i++){
			
		}
	}
	file.close();
	system("pause");
	return 0;
}
Last edited on
It will read as many as there are.... If you do it right... keep going your getting closer

I'll be back in 15 mins, then I have to go to bed, post what you have by then and lets see what else you need.
Last edited on
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
#include <iostream>
#include <fstream>

using namespace std;

const char* FILENAME = "..\\numbers.dat";

int main() {
	int num;
	double nextInt, total = 0, input, avg;
	fstream file(FILENAME, ios_base::in);
	while (file >> nextInt){
		for (int i = 0; i < input; i++){
			cin >> input;
			total += input;
		}

		avg = total / num;
		cout << "average = " << avg << endl;

	}
	file.close();
	system("pause");
	return 0;
}

this is what it looks like right now, but I still can't get it to run.
Tell us about the loop condition on line 13.
The loop condition in line 13 selects the information for what I want to do, which is basically set an array for the terms of the first number it sees then takes the next number after that amount of number and then again until it no longer can. This is perfect for what I am trying to do. I actually found it right before he posted it here, but ya know, I can barely keep my eyes open anymore too. All I have to do is keep my common sense and add each selected array together and divide by the size of the array to find my three values.
here is a better explanation...
That's a loop that says, okay, for every time that i is smaller than "input", I'm going to do whatever is in the code block. Whenever i reaches "input", I'll stop. After each iteration of the loop, it increments i by 1 (i++), so that the loop will eventually stop when it meets the i < "input" (i becomes "input", so no longer is smaller than) condition.
sometimes you have to put in some print statements to see what is going on.

Maybe this will get you closer

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

const char* FILENAME = "file.txt";

int main() 
{
	int num=0, nextInt=0, input=0, avg=0;
	double total = 0;
	fstream file(FILENAME, ios_base::in);
	while ( file.good() )
	{
		file >> num;
			cout << num << endl;
		for (int i = 0; i < num; i++)
		{
			file >> nextInt;
			cout << "\t" << nextInt << endl;
		}
 		
	}
	file.close();

	cout << "Press any Key" << endl;
	getch();
	return 0;

}


PS. your driving me nuts with that system pause.
Last edited on
And since your really close to being finished now, go back and read the directions on what the output should look like.... This is a good time to do that.
lol Visual studio wasn't liking that, but I read it and it does like the form of _getch() for some reason. Also at this point it looks good, but I'm getting an output of four arrays. The fourth array is
6
73
73
73
73
73
73
I have no idea why.

This is weird because the last number in the file is 73. Why would it repeat 6 and just the last number in the file? o.0
Last edited on
check your data file and make sure there are no blank lines at the end.

The old saying is Garbage in Garbage out....
Last edited on
http://puu.sh/i2bvw/fa03ddf034.png

this is just a link to what it looks like in command prompt.
I am done though. All I have left is the write the equation for the average after the end bracket of the for loop and it will look beautiful. You were a huge help. Thank you so much for this and I know I'll have to add something like this to my final. I'm sure it will be a breeze next time!
you have a << endl in there that needs to be taken out.
Pages: 12