I'm having an extremely difficult time figuring out this programming problem. Basically I have an external file called "numbers.dat" which contains this data "5 96 87 78 93 21 4 92 82 85 87 6 72 69 85 75 81 73". **Problem**
Write a program to calculate and display the average of each group of numbers in the file. The 5, 4, and 6 are the beginnings of each group and also identify how many numbers are 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.)
**Output should resemble this**
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
**The algorithm my teacher provided is this:
1. Open file for reading.
2. Initialize "groupSize" with the first value read from file.
3. Enter a loop which repeats until EOF, while (inFile.good()).
-within this loop:
a. Enter a nested loop which repeats "groupSize" number of times.
-within this loop:
i. Read the next number from the file into "number".
ii. Display "number".
iii. Add number to "sum" using an accumulation statement: sum+= number.
b. After exiting the nested loop, calculate "average" as average = sum / groupSize.
c. Display "average".
d. set "sum" to zero.
e. Read the next number from the file into "groupSize".
**And this is what I have so far... been trying combinations of almost everything I can think of and read through in the book... Any and all help would be much appreciated!**
[code so far]
#include <stdafx.h>
#include <iostream>
#include <fstream>
#include <cstdlib> // needed for exit()
#include <string>
using namespace std;
int main()
{
string filename = "numbers.dat"; //put filename up front
double sum, num, average, groupSize; //declare variables
ifstream inFile;
inFile.open(filename.c_str()); // open file for reading
if (inFile.fail()) //check for successful open
{
cout << "\nThe file was not successfully opened"
<< "\n Please check that the file currently exists."
<< endl;
exit(1);
}
inFile >> groupSize; //initialize "groupSize" with the first value read from file.
while (inFile.good()) //Enter loop which repeats until EOF, while (inFile.good())
{
for (groupSize = 5; groupSize <= 5; groupSize++)
{
cout << "\nThe number of elements in this group is " << groupSize;
inFile >> groupSize >> num;
cout << "\nThe data in this group is " << num;
}
#include <iostream>
#include <fstream>
#include <cstdlib> // needed for exit()
#include <string>
usingnamespace std;
int main()
{
string filename = "numbers.dat"; //put filename up front
double sum=0, num=0, groupSize=0; //declare variables
ifstream inFile;
inFile.open(filename.c_str()); // open file for reading
if (inFile.fail()) //check for successful open
{
cout << "\nThe file was not successfully opened"
<< "\n Please check that the file currently exists."
<< endl;
exit(1);
}
while (inFile.good()) //Enter loop which repeats until EOF, while (inFile.good())
{
inFile >> groupSize; //initialize "groupSize" with the first value read from file.
cout << "\nThe number of elements in this group is " << groupSize;
cout << "\nThe data in this group is ";
for (int count = 0; count < groupSize; count++)
{
inFile >> num; // Get next number in group
sum=sum+num; // Add number to SUM
cout << num << " ";
}
cout << "\nAverage = " << sum/groupSize; // Calculate Average
sum=0; // Reset SUM
}
inFile.close();
return 0;
}
PS. I like that you put in comments, I'm sure that it helps you learn but it helped me a great deal to quickly review what you were doing and why.