Question: PART B - Intro to files This program writes n random numbers to a file. Then reads the numbers from the file, displays them to

PART B - Intro to files This program writes n random numbers to a file. Then reads the numbers from the file, displays them to the screen, calculates and displays the number of positive numbers. Finish the program following the specifications listed below as comments: In addition to calculating and displaying the number of positive numbers, calculate and display the number of negative numbers, and 0s. Run the program and save the output as a comment at the end of the source file. NAME: */ #include #include

const int MIN_R = -15; const int MAX_R = 15;

using namespace std;

int main() { ofstream outputFile; int rNum; int n; // Enter the number of random numbers to be written to a file cout << "Please enter an integer within the range 1 to 20: "; cin >> n; while ( n < 1 || n > 20) { cout << "Wrong input! Try again!" << endl; cout << "Please enter an integer within the range 1 to 20: "; cin >> n; } // Open an output file. outputFile.open("Numbers.txt"); // Write n random numbers to the file for (int i = 0; i < n; i++) { // generate a random number within the range MIN_R to MAX_R inclusive rNum = rand() % (MAX_R - MIN_R + 1) + MIN_R; outputFile << rNum << " "; } // Close the file. outputFile.close(); cout << " \t\t\"Numbers.txt\" has been created! \tIt contains " << n << " random numbers within the range \t\t\t\t" << MIN_R << " and " << MAX_R << endl << endl; // Open the same file to read from; //string fileName = "Number.txt"; // <==== Try to open this file! string fileName = "Numbers.txt"; ifstream inFile; inFile.open(fileName); // another way of opening the input file if (!inFile) // could not open the input file // <=== Always check this! { cout << "Error opening " << fileName << " for reading! "; } else { // calculate the number of positive numbers, negative numbers, and 0s. int posN = 0; // ... more code goes here // while ((bool) (inFile >> rNum)) //<==== if Xcode is your IDE use this line instead of the next line while (inFile >> rNum) { cout << rNum << " "; // show the random number // ... more code goes here } // Close the file. inFile.close(); // Show results // Display the number of positive numbers cout << " The number of positive numbers is: " << posN << endl; // ... more code goes here } return 0; } /**~*~*~*~*~*~*~*~*~* OUTPUT */

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!