Reference no: EM1388414
Create a program that will read a file containing variable-length groups of data and report the average value of each group.
Begin by using a text editor to store the following data in a file named group.dat:
5 96 87 78 93 21 4 92 82 85 87 6 72 69 85 75 81 73
The data is arranged in the file so that each group of numbers is preceded by the number of data items in the group. Thus, the first number in the file, 5, indicates that the next five numbers should be grouped together.
The number 4 then indicates that the following four numbers are a group, etc.
Write a C++ program to read the file group.dat, calculate the average of each group, and display it on the screen. [Hint: use nested loops]
Your program must do the following:
• Prompt the user and enter the filename from the console
• Open the file and confirm that the file opened correctly. If not, inform the user and continue to solicit a filename until it opens correctly or the user indicates he/she does not want to continue
• Display the average value for each of the groups in the file
Note: Your program should work for any data file formatted in this fashion. You do not know how many groups of numbers will be included. To check this, you should create another test case with a different number of groups.
Example:
Enter filename: groups.dat
File groups.dat failed to open.
Abort? (y/n): n
Enter filename: group.dat
Group 1 has 5 numbers. Average = 75.00
Group 2 has 4 numbers. Average = 86.50
Group 3 has 6 numbers. Average = 75.83