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

Lab Exercises – 8/29/2018 Lab 1: Write a C++ program to store exactly 1000 rando

ID: 3747548 • Letter: L

Question

Lab Exercises – 8/29/2018

Lab 1: Write a C++ program to store exactly 1000 random integers, each followed by a space, in a file. The random integers are to be in the range of 100 to 999, inclusive.



Lab 2: Write a second C++ program which uses the file produced in Lab 1 as its input file. This program finds the maximum value in the input file, and outputs it, with a label, in an output file.



Lab 3: Write a third C++ program which uses the file produced in Lab 1 as its input file. This program gets user input of a value from 100 to 999, and counts how many times the user’s value appears in the input file. It reports the result to the user using screen output.

Explanation / Answer

If you have any doutbs, please give me comment...

#include<iostream>

#include<fstream>

#include<cstdlib>

#include<ctime>

using namespace std;

int main(){

srand(time(NULL));

char filename[100];

cout<<"Enter output filename: ";

cin>>filename;

ofstream out;

out.open(filename);

for(int i=0; i<1000; i++){

int num = ((rand()%900)+100);

out<<num<<" ";

}

out.close();

cout<<"Successfully saved into file"<<endl;

return 0;

}

lab2)

#include<iostream>

#include<climits>

#include<fstream>

using namespace std;

int main(){

char filename[100];

cout<<"Enter input filename: ";

cin>>filename;

ifstream in;

in.open(filename);

if(in.fail()){

cout<<"Unable to open file"<<endl;

return 0;

}

int num, max = INT_MIN;

while(!in.eof()){

in>>num;

if(max<num)

max = num;

}

in.close();

cout<<"Enter output filename: ";

cin>>filename;

ofstream out;

out.open(filename);

out<<"Maximum value in input file is: "<<max<<endl;

out.close();

return 0;

}

lab3)

#include<iostream>

#include<climits>

#include<fstream>

using namespace std;

int main(){

char filename[100];

cout<<"Enter input filename: ";

cin>>filename;

ifstream in;

in.open(filename);

if(in.fail()){

cout<<"Unable to open file"<<endl;

return 0;

}

int inp_num, num, count = 0;

cout<<"Enter value to count in a file(100-999):";

cin>>inp_num;

while(!in.eof()){

in>>num;

if(num==inp_num)

count++;

}

cout<<count<<" times the user's value "<<inp_num<<" appears in the input file."<<endl;

in.close();

return 0;

}

Usage:

step 1: g++ lab1.cpp or g++ lab2.cpp or g++ lab3.cpp

step 2: ./a.out