Question

(c++) Using reference variables, calculate the min, max, and average values of a vector of doubles...

(c++)

Using reference variables, calculate the min, max, and average values of a vector of doubles in a function called MinMax. This function will not return any data through the return type. Instead, all values must be returned via reference parameters. Make sure that you pass the parameters in the correct order: vector, min, max, and average.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <iostream>
#include <vector>

using namespace std;

void MinMax(vector<double> v, double &min, double &max, double &average) {
    min = v[0];
    max = v[0];
    average = 0;
    for (int i = 0; i < v.size(); ++i) {
        if (v[i] > max) max = v[i];
        if (v[i] < min) min = v[i];
        average += v[i];
    }
    average /= v.size();
}

int main() {
    int size;
    double n, min, max, average;
    cout << "Enter size of the vector: ";
    cin >> size;
    vector<double> v;
    cout << "Enter " << size << " numbers: ";
    for (int i = 0; i < size; ++i) {
        cin >> n;
        v.push_back(n);
    }
    MinMax(v, min, max, average);
    cout << "Min: " << min << endl;
    cout << "Max: " << max << endl;
    cout << "Average: " << average << endl;
    return 0;
}

Add a comment
Know the answer?
Add Answer to:
(c++) Using reference variables, calculate the min, max, and average values of a vector of doubles...
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
  • #include <stdio.h> // Define other functions here to process the filled array: average, max, min, sort,...

    #include <stdio.h> // Define other functions here to process the filled array: average, max, min, sort, search // Define computeAvg here // Define findMax here // Define findMin here // Define selectionSort here ( copy from zyBooks 11.6.1 ) // Define binarySearch here int main(void) { // Declare variables FILE* inFile = NULL; // File pointer int singleNum; // Data value read from file int valuesRead; // Number of data values read in by fscanf int counter=0; // Counter of...

  • Use C Create a function that will take in a vector (three double variables) representing a...

    Use C Create a function that will take in a vector (three double variables) representing a position in meters. Calculate the magnitude of the vector in meters: sqrt(x * x + y * y + z * z) Calculate the magnitude of the vector in feet: magnitudeInMeters * 3.28084 Using passing by reference, return both outputs from the same function. Input: Three unique doubles, each one representing a component of the vector. Output: Magnitude of the vector in meters, magnitude...

  • C Programming write two functions, similar to what you see in the sample program. The first will ask the user to enter some information (I have included the type in parentheses) First Name (char[]) L...

    C Programming write two functions, similar to what you see in the sample program. The first will ask the user to enter some information (I have included the type in parentheses) First Name (char[]) Last Name (char[]) Age (int) Height in Inches (double) Weight in Pounds (double) You will use pass-by-reference to modify the values of the arguments passed in from the main(). Remember that arrays require no special notation, as they are passed by reference automatically, but the other...

  • Create a C program that: Within main, declares a linear array consisting of 10 double values....

    Create a C program that: Within main, declares a linear array consisting of 10 double values. You can assign values to the array when it is declared or fill them items manually using scanf () - your choice. Also, declare three doubles: min, max, and average. Then from within main, a function is called. The function should take a pointer to the array declared above and then finds the maximum value, the minimum value, and calculates the average of the...

  • using C language Create an array of doubles with 5 elements. In the array prompt the...

    using C language Create an array of doubles with 5 elements. In the array prompt the user to enter 5 temperature values (in degree Fahrenheit). Do this within main. Create a user defined function called convert2Cels. This function will not return any values to main with a return statement. It should have parameters that include the temperature array and the number of elements. The function should convert the values for Fahrenheit to Celsius and save them back into the same...

  • code in C++ Create a program that uses the pass by reference operator instead of the...

    code in C++ Create a program that uses the pass by reference operator instead of the return command. Your main program will need to: create empty variables call void function 1 with no input to display your name to the screen call function 2 to collect the square feet that a gallon of paint covers from user call function 2 to collect the square feet of wall that needs to be painted call function 3 to calculate the number of...

  • Using the following parallel array and array of vectors: // may be declared outside the main...

    Using the following parallel array and array of vectors: // may be declared outside the main function const int NUM_STUDENTS =3; // may only be declared within the main function string Students[NUM_STUDENTS] = {"Tom","Jane","Jo"}; vector <int> grades[NUM_STUDENTS] {{78,98,88,99,77},{62,99,94,85,93}, {73,82,88,85,78}}; Write a C++ program to run a menu-driven program with the following choices: 1) Display the grades 2) Add grade 3) Remove grade for all students for a selected assignment 4) Display Average for each student 5) Display the name of...

  • CODE WRITTEN IN C++ ## Problem Overview One of the many ways that technology has helped...

    CODE WRITTEN IN C++ ## Problem Overview One of the many ways that technology has helped make people's lives easier is in making change. It used to be that store clerks had to know how to make correct change in their heads. Now, however, we have computers to do all of our ~thinking~ math for us! In this homework assignment, you will develop a function that is called with three arguments: * the amount of change that needs to be...

  • MUST BE WRITTEN IN C++ All user input values will be entered by the user from...

    MUST BE WRITTEN IN C++ All user input values will be entered by the user from prompts in the main program. YOU MAY NOT USE GLOBAL VARIABLES in place of sending and returning data to and from the functions. Create a main program to call these 3 functions (5) first function will be a void function it will called from main and will display your name and the assignment, declare constants and use them inside the function; no variables will...

  • **C++** Given three function parameters p1, p2, p3, rotate the values to the right. Rotate means...

    **C++** Given three function parameters p1, p2, p3, rotate the values to the right. Rotate means to shift each item to the item on the right, with the rightmost item rotating around to become the leftmost item. If initial values are 2 4 6, final values are 6 2 4. HINTS Declare the function's three parameters as reference type. Function return type is void. Use a tmp variable in the function (similar to when swapping two variables). Your first assignment...

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