hat does the extraction and analysis described above except that the tasks shall be made somewhat simpler. The program shall read from a file "inputdata.txt" containing a name and exactly five values of floating point numbers per line in the file. The first line of the file shall contain the name of the person (first and last) that gathered the data i.e. Joe Jones. The second line of the file shall be the first row of data. Your program will process all the lines in the file as described below. As the program reads each row of data, it shall do two things: it shall keep a running sum of the values read for that row, and shall also write the average of that row to a file "averages.txt". The program will continue reading from the file until it reaches the end of the file. This will entail using a "while" loop and the eof() function that is talked about in Chapter 13 of the text book. The person's name is to be read in at one time using "getline" and stored under a single variable name.
create a
As the program reads each row of data, it shall do two things: it shall keep a running sum of the values read for that row, and shall also write the average of that row to a file "averages.txt". The program will continue reading from the file until it reaches the end of the file. This will entail using a "while" loop and the eof() function that is talked about in Chapter 13 of the text book. The person's name is to be read in at one time using "getline" and stored under a single variable name.
Note that the program shall have no interaction with a user except for starting the program using an IDE or the command line. This means that the program shall NOT prompt the user for the entry of any data. When the program is done, it shall print to the screen the name of the person that sent the data i.e Joe Jones and the name of the output file i.e. "averages.txt".
Software Design
The design of the program consists of two function definitions: main, rowAverage.
float rowAverage(float num1, float num2,float num3,float num4,float num5)
Declare five floating point variables to hold the five numbers that are being sent each time from a row in the input file. (Note: These five variables could be declared more appropriately using an array, but we are not covering that data structure until next unit, so do not use it here.) Set up the function to add up all the values sent and save them in a local variable. Then using that variable divide by 5 and save it in another local variable. Return this value back to the "main" function that has called this function. Now it would be much easier to do all this in the "main" but we are setting things up for the topic of the next unit.
Print just the averages the output file (nothing more or nothing less). i.e.
24.63
56.1
48.9
.
.
.
See inputData.txt (attached file) for a sample of what the input file would look like.
Complete each part of the program by translating the software design information (described in the section above) into C++ source code. Remember to place any local variable declarations at the top of the block of the function definition so that they occur before any of the executable statements.
Save the contents of the file
Use your IDE to compile the contents of your source code file
If any syntax errors are detected by the g++ compiler, or any warning messages appear, correct the errors, save the contents of your source code file, and recompile the file. Continue this step until all syntax errors in the program are corrected and no warning messages appear
Link the program. (The g++ compiler automatically performs this link step for you after you compile the program if your program contains no syntax errors)
Test your program using the input-data.txt file. (You can open a command prompt window and run the program there or from within your IDE.) If any logical errors occur (i.e., program does not fulfill the requirements or produces incorrect output), make corrections in your source code file, save the file, compile and link the file, and run the program again. Continue to do this step until the logical errors are corrected.
To test your error detecting statements in the main function, first use a text editor to change the numbers in the first line of the input-data.txt file. Then use that file as your input file to the program.
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 1 images