Question
In c++ programming, can you please edit this program to meet these requirements:


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.
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;
}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

#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;

}

Add a comment
Know the answer?
Add Answer to:
In c++ programming, can you please edit this program to meet these requirements: The program that...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please Help! Write a C++ program to read integers until 9999 is entered (called sentinel –...

    Please Help! Write a C++ program to read integers until 9999 is entered (called sentinel – sentinel is used to indicate the end of input and must not to be considered as the valid data for the computation). Perform the following operations for the integers entered by the user. Display the sum of the numbers less than 100 Display the smallest of the positive integers Display the largest of the negative integers Display how many integers are between 100 and...

  • c++ program that prints joke and its punchline

    nothing happens when I replace the skeleton code. the program runs but it does not give me the desired results. All the question marks must be removed and filled in with the appropriate code.// Function prototypesvoid displayAllLines(ifstream &infile);void displayLastLine(ifstream &infile);int main(){// Arrays for file nameschar fileName1[SIZE];char fileName2[SIZE];// File stream objectsifstream jokeFile;ifstream punchlineFile;// Explain the program to the user.cout << "This program will print a joke "<< "and its punch line.\n\n";// Get the joke file name.cout << "Enter the name of...

  • Create a new file (in Dev C++) Modify program above to use a vector instead of...

    Create a new file (in Dev C++) Modify program above to use a vector instead of an array. Header comments must be present Prototypes must be present if functions are used Hello and goodbye messages must be shown Vector(s) must be used for implementation Use comments and good style practices ====================================================================================================== #include <iostream> using namespace std; int main() { cout<<"Welcome! This program will find the minimum and maximum numbers in an array."<<endl; cout<<"You will be asked to enter a number...

  • //This program is your final practice exam. //Please fill in the functions at the bottom of...

    //This program is your final practice exam. //Please fill in the functions at the bottom of the file. (sum and removeItem) //DO NOT CHANGE ANYTHING ELSE. //main has all the code needed to test your functions. Once your functions are written, please build and make sure it works fine //Note that in this case, the list is not sorted and does not need to be. Your goal is to insert the number in the given position. #include <iostream> #include <fstream>...

  • //This program is your final exam. //Please fill in the functions at the bottom of the...

    //This program is your final exam. //Please fill in the functions at the bottom of the file. (evenCount and insertItem) //DO NOT CHANGE ANYTHING ELSE. //main has all the code needed to test your functions. Once your functions are written, please build and make sure it works fine //Note that in this case, the list is not sorted and does not need to be. Your goal is to insert the number in the given position. #include <iostream> #include <fstream> using...

  • //This program is your final exam. //Please fill in the functions at the bottom of the...

    //This program is your final exam. //Please fill in the functions at the bottom of the file. (evenCount and insertItem) //DO NOT CHANGE ANYTHING ELSE. //main has all the code needed to test your functions. Once your functions are written, please build and make sure it works fine //Note that in this case, the list is not sorted and does not need to be. Your goal is to insert the number in the given position. #include <iostream> #include <fstream> using...

  • Question 19 4 pts Using the program below, please complete the program by adding 2 functions...

    Question 19 4 pts Using the program below, please complete the program by adding 2 functions as follow: 1. A function named getAverage that calculates and returns the Average. 2. A function named getMaximum that returns the maximum of the three numbers. Make sure to use the proper data types for all variables and functions. #include <iostream> using namespace std; //function getAverage //function getMaximum int main(int argc, char** argv) { int x, y, z; cout << "Enter three whole numbers:...

  • This is for a C++ program: I'm almost done with this program, I just need to...

    This is for a C++ program: I'm almost done with this program, I just need to implement a bool function that will ask the user if they want to repeat the read_course function. I can't seem to get it right, the instructions suggest a do.. while loop in the main function. here is my code #include <iostream> #include <cstring> #include <cctype> using namespace std; const int SIZE = 100; void read_name(char first[], char last[]); void read_course(int & crn, char des[],...

  • // This program will read in a group of test scores (positive integers from 1 to...

    // This program will read in a group of test scores (positive integers from 1 to 100) // from the keyboard and then calculate and output the average score // as well as the highest and lowest score. There will be a maximum of 100 scores. // #include <iostream> using namespace std; float findAverage (const int[], int); // finds average of all grades int findHighest (const int[], int); // finds highest of all grades int findLowest (const int[], int); //...

  • #include <iostream> #include <fstream> using namespace std; //constants const int CAP = 100; //function prototypes bool...

    #include <iostream> #include <fstream> using namespace std; //constants const int CAP = 100; //function prototypes bool openFile(ifstream &); void readData(ifstream &, int [], int &); void printData(const int [], int); void sum(const int[], int); void removeItem(int[], int &, int); int main() { ifstream inFile; int list[CAP], size = 0; if (!openFile(inFile)) { cout << "Program terminating!! File not found!" << endl; return -1; } //read the data from the file readData(inFile, list, size); inFile.close(); cout << "Data in file:" <<...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT