help/questions-and-answer8 20- points wite c progn 8 (20 points) Writea C+ progr
ID: 3714058 • Letter: H
Question
help/questions-and-answer8 20- points wite c progn 8 (20 points) Writea C+ program which prompts the user for ap output file name and then reads a list of ten or fewer positive intesers into a The input is terminated by the seatinel value-100. The program then finds the maximum integer in the list. Finally, the program outputs to both the screen and the specified output file the maximum integer followed by all list elements whose values vector called halfmax ese are stricty greater than half the maximum. In the output file, you will write the maximum imteger to the first line and the list of values greater than half the maximum to the second line. Here is an example of running the program (user input is shown in bold): >halfmax exe Enter name of output file: out.txt Enter a list of ten or fewer positive integers ending with 479376 6-100 -100: Maximum:9 Integers strictly greater than 4.5:79766 > cat out.txt 79766 Important. To receive full credit you must 1) use a vector to hold the input list, 2) write your solution in the main function below, and 3) implement the algorithm described in the comments. Assume the user will only enter positive integers, except for the sentinel value-100 of course. #includeExplanation / Answer
Below is your code: -
#include<iostream>
#include<fstream>
#include<string>
#include<cstdlib>
#include<vector>
using namespace std;
int main() {
/*insert your solution here*/
//declaring variable
vector<int> halfmax_exe;
ofstream outfile;
string fileName;
//prompt the user for the output file name
cout<<"Enter name of output file: ";
cin>>fileName;
//open the output file
outfile.open(fileName);
//check for error opening file
if (!outfile) {
cout << "Unable to open file "<<fileName;
exit(1); // call system to stop
}
//prompt user for input positive integers.
cout<<"Enter a list of 10 or fewer positive integer ending with -100: "<<endl;
//Read upto 10 positive integers
int count = 0;
int num;
int maxNum = -1;
do {
cin>>num;
count++;
halfmax_exe.push_back(num);
if(num > maxNum) {
maxNum = num;
}
} while(count < 10 && num != -100);
if(halfmax_exe.size() > 0) {
//output maximum to screen and file
cout<<"Maximum: "<<maxNum<<endl;;
outfile<<maxNum<<endl;
//output the values greater than half the max.
double halfMaxNum = maxNum / 2.0;
cout<<"Integers strictly greater than "<<halfMaxNum<<": ";
for(vector<int>::size_type i = 0; i != halfmax_exe.size(); i++) {
if(halfmax_exe[i] > halfMaxNum) {
cout<<halfmax_exe[i]<<" ";
outfile<<halfmax_exe[i]<<" ";
}
}
}
//close the output file
outfile.close();
return 0;
}
Output
Enter name of output file: out.txt
Enter a list of 10 or fewer positive integer ending with -100:
4 7 9 3 7 6 6 -100
Maximum: 9
Integers strictly greater than 4.5: 7 9 7 6 6
out.txt
9
7 9 7 6 6
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.