subject

Exercise 1

Write a function that reads a file "numbers. dat". This file contains a list of integers. The function should return the largest number, the smallest number, and the average of the numbers in the file.

Make sure you include a function prototype.

Write the statements to call your function and print the largest and smallest numbers.

Note there should be no input/output (cin/cout) statements inside your function.

Exercise 2

Write another version of the function from exercise 1. In this version, the function will prompt the user for a file name, then the function should check whether the input file is opened successfully before reading from the file. If the file is not opened successfully, the function should return false. If the file is opened successfully, the function should return the largest, smallest, and the average of the numbers in the file as before, and return true. The parameter list for the function should be the same as the original version. To return true or false, use a return value for the function.

Write a prototype for this new version of the function.

Write the statements to call the new version of the function. If the input file could not be opened, print an error message. Otherwise, print the largest and smallest numbers.

Note there should be no input/output (cin/cout) statements inside your function.

Sample output:

Which exercise? 1

The min is: -15357

The max is: 184494

The average is: 85225.6

Which exercise? 2

Enter the name of the file: numbers. dat

The max number is: -15357

The min number is: 184494

The average is: 85225.6

Which exercise? 2

Enter the name of the file: slkmv

Cannot open the file!

Requirements for all programs

No global variables, other than constants

The required tasks must be performed with the functions specified (not just with a single main() routine)

Note that each exercise requires the writing of a function, and a main routine to test that function.

Each function should do exactly the task specified in item 1 of each exercise

Item 2 of each exercise specifies what to do in main() -- this will always involve CALLING the function, sometimes more than once

Note that there is NO keyboard-input/screen-output specified in the functions themselves (i. e. "return" does not mean "print")

My lab partners and I are having a bit trouble with this so far we have a code, but the numbers keep coming up as error:

#include

#include

#include

#include

using namespace std;

void numbers1(){

ifstream inFile;

ofstream outFile;

inFile. open("numbers. dat");

outFile. open("numbers. dat");

string file;

double max;

double min;

double average;

inFile >> max;

inFile >> min;

inFile >> average;

outFile << fixed << setprecision(2);

outFile << "The min number is: " << min << endl;

outFile << "The max number is: " << max << endl;

outFile << "The average number is: " << average << endl;

inFile. close();

outFile. close ();

}

bool numbers2(string inFile){

if (inFile == "numbers. dat"){

ifstream inFile;

ofstream outFile;

inFile. open("numbers. dat");

outFile. open("numbers. dat");

double max = 0;

double min = -0;

double average;

inFile >> max;

inFile >> min;

inFile >> average;

outFile << "The largest number is: " << max << endl;

outFile<<"The smallest number is: " << min << endl;

outFile. close();

outFile. close();

outFile << "The max number is: " << max << endl;

outFile << "The min number is: " << min << endl;

outFile << "The average number is: " << average << endl;

inFile. close();

outFile. close ();

return true;

}

else {

return false;

}

return numbers2(inFile);

}

int main()

{

int exercise;

double max;

double min;

double average;

cout << "Which exercise? \n";

cin >> exercise;

if (exercise == 1){

numbers1();

cout << "The min is: " << min << endl;

cout << "The max is: " << max << endl;

cout << "The average is: " << average << endl;

}

else if (exercise == 2){

string inFile;

cout << "Enter the name of the file: \n";

cin >> inFile;

if (numbers2(inFile) == true){

cout << "The max number is: " << max << endl;

cout << "The min number is: " << min << endl;

cout << "The average number is: " << average << endl;

}

else {

cout << "Cannot open the file!\n";

}

}

return 0;

}

ansver
Answers: 3

Another question on Computers and Technology

question
Computers and Technology, 22.06.2019 16:30
What type of file does a cookie place on web site visitors' computers so that web site managers can customize their sites to their visitors' preferences?
Answers: 1
question
Computers and Technology, 23.06.2019 06:30
Martha is designing a single-player game. her manager suggests that she plan the design to incorporate future modifications. which principle of game design relates to planning for future modifications?
Answers: 1
question
Computers and Technology, 23.06.2019 21:00
Which set of steps will organize the data to only show foods with more than 100 calories and rank their sugar content from greatest to least?
Answers: 1
question
Computers and Technology, 23.06.2019 22:20
If i uninstall nba 2k 19 from my ps4 will my career be gone forever?
Answers: 2
You know the right answer?
Exercise 1

Write a function that reads a file "numbers. dat". This file contains a list...
Questions
question
Social Studies, 26.09.2019 15:00
Questions on the website: 13722367