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

Other questions on the subject: Computers and Technology

image
Computers and Technology, 22.06.2019 03:00, TamB01
How can i clip a picture to a question on the computer?
Answers: 1
image
Computers and Technology, 23.06.2019 00:40, QueenKy6050
Consider the following statements: struct nametype{string first; string last; }; struct coursetype{string name; int callnum; int credits; char grade; }; struct studenttype{nametype name; double gpa; coursetype course; }; studenttype student; studenttype classlist[100]; coursetype course; nametype name; mark the following statements as valid or invalid. if a statement is invalid, explain why. a.) student. course. callnum = "csc230"; b.) cin > > student. name; c.) classlist[0] = name; d.) classlist[1].gpa = 3.45; e.) name = classlist[15].name; f.) student. name = name; g.) cout < < classlist[10] < < endl; h.) for (int j = 0; j < 100; j++)classlist[j].name = name; i.) classlist. course. credits = 3; j.) course = studenttype. course;
Answers: 1
image
Computers and Technology, 23.06.2019 01:20, shiann2002
Me with this program in c++ ! computers represent color by combining sub-colors red, green, and blue (rgb). each sub-color's value can range from 0 to 255. thus (255, 0, 0) is bright red. (130, 0, 130) is a medium purple. (0, 0, 0) is black, (255, 255, 255) is white, and (40, 40, 40) is a dark gray. (130, 50, 130) is a faded purple, due to the (50, 50, 50) gray part. (in other word, equal amounts of red, green, blue yield gray).given values for red, green, and blue, remove the gray part. ex: if the input is 130 50 130, the output is: 80 0 80. thus, find the smallest value, and then subtract it from all three values, thus removing the gray.
Answers: 3
image
Computers and Technology, 23.06.2019 04:00, china236
In a word processing program, such as microsoft word, which feature to you choose the desired picture enhancement?
Answers: 2
You know the right answer?
Exercise 1

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

Questions in other subjects:

Konu
Mathematics, 08.04.2021 18:50
Konu
Mathematics, 08.04.2021 18:50
Konu
Mathematics, 08.04.2021 18:50