Exercise 1
Write a function that reads a file "numbers. dat". This file contains a list...
Computers and Technology, 16.04.2020 04:52 vctorsurfs327
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;
}
Answers: 3
Computers and Technology, 22.06.2019 12:40
How do i get the most points, without any effort?
Answers: 2
Computers and Technology, 23.06.2019 02:30
Research data that is presented using descriptive language is said to be
Answers: 2
Computers and Technology, 23.06.2019 09:30
Name the range function that would generate the following list of integers values: 0,1,2,3,4,5.
Answers: 1
Computers and Technology, 23.06.2019 21:30
Which of the following includes the three primary network access technologies? dsl, cable modem, broadband lan, wan, man voip, uc, iptv tcp/ip, ftp, dhcp
Answers: 2
Chemistry, 24.03.2021 21:00
Mathematics, 24.03.2021 21:00
Business, 24.03.2021 21:00
Biology, 24.03.2021 21:00
Biology, 24.03.2021 21:00
Mathematics, 24.03.2021 21:00
Mathematics, 24.03.2021 21:00
Mathematics, 24.03.2021 21:00