Computers and Technology

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;

}

answer
Answers: 3

Other questions on the subject: Computers and Technology

image
Computers and Technology, 22.06.2019 06:30, messyquestions
Plz 40 points what are raster vectors? a bitmap image a vector file a type of printing press a small projector
Answers: 1
image
Computers and Technology, 22.06.2019 23:30, lexirandall19
Define a function printfeetinchshort, with int parameters numfeet and numinches, that prints using ' and " shorthand. ex: printfeetinchshort(5, 8) prints: 5' 8"
Answers: 1
image
Computers and Technology, 23.06.2019 04:00, coolconnor1234p0sv4p
Another name for addicting games. com
Answers: 1
image
Computers and Technology, 23.06.2019 10:30, badpotterchris
How would you categorize the software that runs on mobile devices? break down these apps into at least three basic categories and give an example of each.
Answers: 1
Do you know the correct answer?
Exercise 1

Write a function that reads a file "numbers. dat". This file contains a list...

Questions in other subjects:

Konu
Mathematics, 08.06.2021 17:20