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...

  • //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[],...

  • #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:" <<...

  • C++ Programming - Design Process I need to create a flow chart based on the program...

    C++ Programming - Design Process I need to create a flow chart based on the program I have created. I am very inexperienced with creating this design and your help will be much appreciated. My program takes a string of random letters and puts them them order. Our teacher gave us specific functions to use. Here is the code: //list of all the header files #include <iomanip> #include <iostream> #include <algorithm> #include <string> using namespace std; //Here are the Function...

  • c++ programming : everything is done, except when you enter ("a" ) in "F" option ,...

    c++ programming : everything is done, except when you enter ("a" ) in "F" option , it does not work. here is the program. #include <iostream> #include <string> #include <bits/stdc++.h> #include <iomanip> #include <fstream> using namespace std; #define MAX 1000 class Inventory { private: long itemId; string itemName; int numberOfItems; double buyingPrice; double sellingPrice; double storageFees; public: void setItemId(long id) { itemId = id; } long getItemId() { return itemId; } void setItemName(string name) { itemName = name; } string...

  • ***Please complete the code in C*** Write a program testArray.c to initialize an array by getting...

    ***Please complete the code in C*** Write a program testArray.c to initialize an array by getting user's input. Then it prints out the minimum, maximum and average of this array. The framework of testArrav.c has been given like below Sample output: Enter 6 numbers: 11 12 4 90 1-1 Min:-1 Max:90 Average:19.50 #include<stdio.h> // Write the declaration of function processArray int main) I int arrI6]i int min-0,max-0 double avg=0; * Write the statements to get user's input and initialize the...

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
ADVERTISEMENT