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 :)