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 06:00
Write a function that draws a pool ball. this function should take as parameters, the color, the number that should go on the pool ball, and the location of the center of the pool ball. the radius of the pool balls should be pool_ball_radius, and the font of the number should be pool_ball_font. the text of the pool ball font should be white. drawpoolball(color.orange, 5, 100, 100); drawpoolball(color.green, 6, 50, 200); drawpoolball(color.red, 3, 150, 350); drawpoolball(color.blue, 2, 250, 140); to center the numbers on the pool ball, you should use the getwidth() and getheight() methods. you are allowed to call these methods on your text object, such as txt.
Answers: 3
question
Computers and Technology, 22.06.2019 23:20
How can you tell if someone sent you a text message to your email instead of a email
Answers: 1
question
Computers and Technology, 23.06.2019 12:30
Animations and transitions are added from the
Answers: 1
question
Computers and Technology, 23.06.2019 15:00
Barbara is interested in pursuing a career in the science and math pathway. which qualifications will her reach that goal? a.an advanced knowledge of physics and math b.an advanced knowledge of engineering and math c. an advanced knowledge of physics and robotics an d. advanced knowledge of machinery and math
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
English, 13.01.2020 06:31
question
Mathematics, 13.01.2020 06:31
question
Physics, 13.01.2020 06:31
Questions on the website: 13722361