The program that needs editing: #include <iostream> #include <fstream> #include
ID: 3732816 • Letter: T
Question
The program that needs editing:
#include <iostream> #include <fstream> #include <iomanip> using namespace std;
int cal_avg(char* filenumbers, int n){
int a = 0; int number[100];
ifstream filein(filenumbers);
if (!filein) {
cout << "Please enter a a correct file to read in the numbers from";
}
while (!filein.eof()) {
filein >> number[a]; a++;
}
int total = number[0];
for (a = 0; a < n; a++) {
total = total + number[a]; }
return total/n; }
int cal_min(char* filenumbers, int n){
int a = 0; int number[100];
ifstream filein(filenumbers);
if (!filein) {
cout << "Please enter a a correct file to read in the numbers from"; }
while (!filein.eof()) {
filein >> number[a];
a++; }
int minimum = number[0];
for (a = 0; a < n; a++) {
if (minimum > number[a]) {
minimum = number[a]; } }
return minimum; }
int cal_max(char* filenumbers, int n){
int number[100]; int a = 0;
ifstream filein(filenumbers);
if (!filein) {
cout << "Please enter a a correct file to read in the numbers from";
} while (!filein.eof()) {
filein >> number[a];
a++; }
int maximum = number[0];
//loop to test each number to see if it is lower/higher than the previous, therefore calculating the maximum
for (a = 0; a < n; a++) {
if (maximum < number[a]) {
maximum = number[a];
}
}
return maximum;
}
int main()
{
int maximum; int minimum; int total = 0; char filenumbers[100]; float average = 0;
cout << "Enter the name of the file that you will read the numbers from:" << endl;
cin >> filenumbers;
cout << "The average of the 100 numbers is " << cal_avg(filenumbers,100) << endl; cout << "The minimum of the 100 numbers is " << cal_min(filenumbers,100) << endl; cout << "The maximum of the 100 numbers is " << cal_max(filenumbers,100) << endl;
return 0; } #include <iostream> #include <fstream> #include <iomanip> using namespace std;
int cal_avg(char* filenumbers, int n){
int a = 0; int number[100];
ifstream filein(filenumbers);
if (!filein) {
cout << "Please enter a a correct file to read in the numbers from";
}
while (!filein.eof()) {
filein >> number[a]; a++;
}
int total = number[0];
for (a = 0; a < n; a++) {
total = total + number[a]; }
return total/n; }
int cal_min(char* filenumbers, int n){
int a = 0; int number[100];
ifstream filein(filenumbers);
if (!filein) {
cout << "Please enter a a correct file to read in the numbers from"; }
while (!filein.eof()) {
filein >> number[a];
a++; }
int minimum = number[0];
for (a = 0; a < n; a++) {
if (minimum > number[a]) {
minimum = number[a]; } }
return minimum; }
int cal_max(char* filenumbers, int n){
int number[100]; int a = 0;
ifstream filein(filenumbers);
if (!filein) {
cout << "Please enter a a correct file to read in the numbers from";
} while (!filein.eof()) {
filein >> number[a];
a++; }
int maximum = number[0];
//loop to test each number to see if it is lower/higher than the previous, therefore calculating the maximum
for (a = 0; a < n; a++) {
if (maximum < number[a]) {
maximum = number[a];
}
}
return maximum;
}
int main()
{
int maximum; int minimum; int total = 0; char filenumbers[100]; float average = 0;
cout << "Enter the name of the file that you will read the numbers from:" << endl;
cin >> filenumbers;
cout << "The average of the 100 numbers is " << cal_avg(filenumbers,100) << endl; cout << "The minimum of the 100 numbers is " << cal_min(filenumbers,100) << endl; cout << "The maximum of the 100 numbers is " << cal_max(filenumbers,100) << endl;
return 0; } Program #1 This assignment is similar to the assignment #7 with some modifications. Instead of three functions to calculate average, minimum, and maximum, you need to write just one function. As any function can return only one value, you need use call by reference to get the values for min, max, and average. The header of the function is given below. Il uses call by reference to return min, max and average to the calling,function void calc stats(string file_name, int total_num, int& min, int& max, double& avg ); The parameter file _name is the name of the file that contains the integers. The total num contains the total number of integers in the file. In the main) function, prompt the user to input both the file name containing the integers, and the total number of integers in the file. Then call the function calc_stats (with appropriate arguments) to find average, min, and max and output the value returned by the function.
Explanation / Answer
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
void cal_avg(string filenumbers, int total_num, int &min, int &max, double &avg) {
int number[100];
int a = 0;
ifstream filein(filenumbers);
if (!filein) {
cout << "Please enter a a correct file to read in the numbers from";
}
while (!filein.eof()) {
filein >> number[a];
a++;
}
int minimum = number[0];
int maximum = number[0];
int total = 0;
for (a = 0; a < total_num; a++) {
if (maximum < number[a]) {
maximum = number[a];
}
if (minimum > number[a]) {
minimum = number[a];
}
total = total + number[a];
}
double average = total / total_num;
min = minimum;
max = maximum;
avg = average;
}
int main()
{
int maximum = 0;
int minimum = 0;
char filenumbers[100];
double average = 0;
cout << "Enter the name of the file that you will read the numbers from:"
<< endl;
cin >> filenumbers;
cal_avg(filenumbers, 15, minimum, maximum, average);
cout << "The average of the 100 numbers is " << average<< endl;
cout << "The minimum of the 100 numbers is " << minimum << endl;
cout << "The maximum of the 100 numbers is " << maximum << endl;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.