Question
C++ /* Introduction to files This program writes data to a file, then uses the same file to read data from it. - Generates n
C++
/* Introduction to files This program writes data to a file, then uses the same file to read data from it.
- Generates n random numbers within a given range, displays them to the screen, and writes them to a file. - Reads the numbers from the file, displays them to the screen, and calculates their average. - Displays the average of the random numbers. Finish the program following the specifications given as comments in the program. NAME: */ #include
// constants for the range of the random numbers const int MIN_R = -25; const int MAX_R = 25;
using namespace std;
int main() { ofstream outputFile; int rNum; int n; // Enter the number of random numbers to be written to a file cout > n; while ( n 20) // Input validation loop { cout > n; } // Open an output file. outputFile.open("Numbers.txt"); // Write n random numbers to the file for (int i = 0; i > rNum) { cout (sum) / n
return 0; }
6.11 LAB: Files (input and output) This program writes data to a file, then uses the same file to read data from it. Generates n random numbers within a given range, displays them to the screen, and writes them to a file. Reads the numbers from the file, displays them to the screen, and calculates their average. Displays the average of the random numbers. Finish the program following the specifications given as comments in the program. Note: The call for srand() has been omitted on purpose, to make sure that your output matches zyBook's output. Because there is no call for srand(), each time you run the program, it will generate the same random numbers. If you run this program on your own computer or use the online compiler, you are more likely going to get different numbers than the zyBook's numbers, because of the way random numbers are generated. For instance, when input is 8, the zy Book's solution generates the following random numbers: - 15 24 -16 9 7 12 12 23 When I run the program on my computer I got: 6 0 -20 -23 -3 -17 -1 1 When I run the program using the online C++ compiler I got: -6 9 -16 -18 -20 -18 -15 -13Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started