Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

File IO Write a program that creates an output file called “proj7.txt” containin

ID: 3676645 • Letter: F

Question

  File IO

Write a program that creates an output file called “proj7.txt” containing n random numbers between 10 and 100 inclusive. The program prompts the user for the value of n, generates the random numbers, and writes them to the file three on each line. You must close the file output stream when you are done writing to the file. Otherwise, the output file may not have all the n numbers in it. File I/O are usually buffered by the operating systems, and these buffers are flushed when you close the streams. Seed the random number generator with the system’s clock so that it generates a different sequence of numbers each time you run the program. Compile and run the program and make sure it works (check the output file to verify that it contains n numbers between 10 and 100 and that there are three numbers on each line).

*********************** switch partner’s role at this point ********************

Now you are going to read the numbers from the file you just created “proj7.txt” and display them on the screen 3 on each line. In addition you are going to find the largest, the smallest, and the average of all the numbers in the file and display them neatly on the screen (see below). You will do all these tasks by reading the input file “proj7.txt” just once.

Include a loop in the program that lets the user repeat the above process for as many times as she/he wishes to do so.

Sample Input and Output:

Programmer:   hhhhhhh   

Course: 2011

Lab#: 11111111

Due Date: 1-1-11

How many numbers you want in “proj7.txt”: 10

File “proj7.txt” contains:

21        90        12

30        20        50

62        71        99

86

The largest number in “proj7.txt” is: 99

The smallest number in “proj7.txt” is:                         12

The average of all the numbers in “proj7.txt” is:         54.1

Do it again? Y or y (for yes), and N or n (for no): N

Explanation / Answer

Please find the code below:

#include <iostream>
#include <fstream>
#include <time.h>
#include <sstream>
using namespace std;

int getRandom(){
int iSecret, iGuess;
srand (time(NULL));
iSecret = rand() % 100 + 10;
return iSecret;
}

int main () {
  
int n;
cout << "Enter n:" << endl;
cin >> n;
  
while(true) {
  
ofstream myfile ("proj7.txt");
if (myfile.is_open())
{
for(int i=1; i<=n; i++){
myfile << getRandom();
myfile << " ";
if(i%3==0)
myfile << " ";
}
myfile.close();
}
else cout << "Unable to open file";
  
int small=0,large=0,sum=0;
string line;
ifstream myfile1 ("proj7.txt");
if (myfile1.is_open())
{
while ( getline (myfile1,line) )
{
cout << line << ' ';
string arr[3];
int i = 0;
stringstream ssin(line);
while (ssin.good() && i < 3){
ssin >> arr[i];
++i;
}
  
for(i=0;i<3;i++){
int num = stoi( arr[i]);
sum = sum + num;
if(num>large)
large = num;
if(num<small)
small = num;
}
}
  
double avg = sum/n;
cout << "The largest number in proj7.txt is: " << large << endl;
cout << "The smallest number in proj7.txt is:" << small << endl;
cout << "The average of all the numbers in proj7.txt is: " << avg << endl;
myfile.close();
}
  
else cout << "Unable to open file";
  
  
char c;
cout << "Do it again? Y or y (for yes), and N or n (for no): N" <<endl;
cin >> c;

if(c=='Y' || c=='y'){
break;   
}
  
}
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote