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

need help on c++ program please You will create one source code program, Prog14.

ID: 3833891 • Letter: N

Question

need help on c++ program please

You will create one source code program, Prog14.cpp. This program will 1. Use a function, which you will complete, named PromptUser0, to ask the user for a filename. It will return the filename as a string object. 2. pen the filename for reading. If you cannot open the filename the user supplied prompt the user for another. Repeat this until you can open the user's file. I found that the file should be placed in the directory where Visual Studio puts the ".vcxproj" file and the ".cpp" file. Otherwise you have to give the FULL path to the file. If you put the file on the Desktop, the filename will probably be something like C:usersyour user nameDesktoplyour-real-file-name 3. Use a function, which you will complete named Read Data0, that will take a reference to an integer vector as the first parameter and a reference to a file stream as the second parameter. It will test to make sure the stream is open, then read all the data from the stream and place that data into the vector. If the file is not open at the beginning of the function, the function will return a 1, otherwise it will return 0. The main program should check the return value of this function. If there is an error, give the user an error message and then end the program.

Explanation / Answer

// This is a skeleton for you start from.
// Get a filename from the user.
// Open the file and
// read a list of integer data from the file.
// Display the number of values, arithmetic mean and median of
// the list on the standard output. Input data are assumed to
// always be valid (that means all integers).
//
// NOTE: the file you are reading from should in the directory
// that contains the ".sln" file for your project. At least
// that is where I got it to work.
#include<iostream>
using std::cin;
using std::cout;
using std::endl;

#include<vector>
using std::vector;

#include <algorithm>
using std::sort;

#include<string>
using std::string;

#include<fstream>
using std::ifstream;

using std::getline;

/*
* You have two choices here. Either turn these into prototypes and
* complete the function definitions after main() or just put in the CORRECT
* code here. What you see is a set of stubs that will compile and run. It just
* does not do anything. But you can fill these in one at a time,
* AND TEST THEM, to complete the program.
*/

/*
* Prints a message and reads in the string the user supplies
*/
string PromptUser()
{
string filename;
cout << "Please enter the name of a file containing integers." << endl << "We'll return the number of integers, mean and median." << endl;
cin >> filename;
return filename;
}

/*
* Reads in the data from the stream "in" and puts the values in the vector.
* returns 0 if everything went well.
* returns 1 if the file was not open
*/

int ReadData(vector<int>& theData, ifstream& in)
{
in.open(PromptUser());
if (in.is_open())
{
int a;
while (!in.eof())
{
in >> a;
theData.push_back(a);
}
return 0;
}
else
{
return 1;
}
}

/*
* Computes the mean and median of the values in the vector.
*/

void ComputeStats(vector<int>& theData, double& mean, double& median)
{
int sum = 0;
for (size_t i = 0; i < theData.size(); i++)
{
sum += theData[i];
}

mean = sum / theData.size();

size_t size = theData.size();
sort(theData.begin(), theData.end());

if (size % 2 == 0)
{
median = (theData[size / 2 - 1] + theData[size / 2]) / 2;
}
else{
median = theData[size / 2];
}

return;
}

/*
* Simple function that displays the results. See instructions for
* format of output.
*/

void DisplayStats(int num, double mean, double median)
{
cout << "********** Data Summary **********" << endl;
cout << "Number of Values: " << num << endl;
cout << "Mean Value: " << mean << endl;
cout << "Median Value: " << median << endl;
cout << "**********************************" << endl;
return;
}

int main()
{
vector<int> theData;
string filename;
ifstream infile;

/*
* Get the filename
*/
filename = PromptUser();

infile.open(filename);
do
{
infile.clear();
filename = PromptUser();
infile.open(filename);
} while (!infile);
/*
* Now you have to open the file.
* If the filename is bad (file will not open)
* reset the ifstream with ifstream.clear().
* Prompt the user (with that function) again and repeat until
* the input file is opened.
*
*/

/*
* Now that the file is open, read the data
*/

int rtn = ReadData(theData, infile);

/*
* ReadData() needs to make sure the file is actually open
* before it tries to read from it. Strongly suggest you
* use the is_open() for this, not good() or bad().
*
* Check the return value from ReadData(). This is a deduction
* if you do not check the return value.
* If it is 1, the file was not open so
* give the user a message and end the program.
* Do not use the exit() function, just return from main()
*/
if (rtn == 1)
{
cout << "Could not open the file." << endl;
return 0;
}

/*
* Now close the input file. Done with it. This is a deduction
* if you do not close the file.
*/
infile.close();
int numValues = theData.size();
double meanValue, medianValue;

ComputeStats(theData, meanValue, medianValue);

DisplayStats(numValues, meanValue, medianValue);

return 0;
}