### Call By Value/Call by Reference assignment help. closed account (SNRzwA7f)
I'm working on a homework assignment for class and am absolutely stumped by what I need to do. I'm asked to call an overloaded function that will calculate sum and mean for two separate menu options by using the same function. I am supposed to be using a boolean expression to help determine the output for the corresponding menu option but I'm getting held up by the bool. Any help or explanation would be greatly appreciated. I'll post the fragments of code that are stumping me as I believe it's just within the function body and switch statements.
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445`` `````` double sum_mean(double& num1, bool decision); //function prototype switch (selection) //switch statement for each menu option { case 1: //statement for first menu option sum_mean(num1, decision); break; case 2: //statement for second menu option sum_mean(num1, decision); break; //function body double sum_mean(double& num1, bool decision) { double sum; int choice; double mean; decision(true); cout << "How many positive numbers would you like to enter?" <> choice; cout << "Please enter " << choice << " numbers separated by a space and press when completed." < 0) { cin >> num1; choice--; sum = sum += num1; } cout << "The sum of your numbers is :" << sum << endl; if (decision == TRUE) { mean = sum / choice; cout << "The mean of these numbers is: " << mean << endl; } return sum; }``````
Last edited on Why does one function need to do two different things? That's bad design.

Have you read the tutorial on functions?
http://www.cplusplus.com/doc/tutorial/functions/

What specifically is confusing to you? You can't just dump several lines of code and say "this is confusing" - say specifically what parts are confusing. By the time you get to line 40, choice is 0. Perhaps you should avoid that happening (and maybe give it a better name.)

I don't understand why you're passing in a number by reference to sum_mean. closed account (SNRzwA7f)
The boolean expression is confusing me. I don't understand how I can declare it true or false and yield mean in the "mean" menu option, or false and just calculate the sum and stop there.

The wording of the instructions honestly may have me confused:

1. Instead of using a constant to input 5 numbers, you will prompt the user to enter
the positive quantity of numbers to read in when the user selects menu options 1
through 4 (i.e., sum, mean, min, or max). This means that the user could request
to enter any positive integer for the quantity of numbers to read in. In the case of
a zero or negative integer, the user will be re-prompted for the quantity until a
positive integer is entered.
2. You will write a function to display your personal information (i.e., department
this function’s sole purpose is simply to display your information as specified (i.e.,
no other input or output or calculations of any sort).
3. You will write a function to print out the menu. Note that this function’s sole
purpose is simply to display your information as specified (i.e., no other input or
output or calculations of any sort).
4. You will write one function that, given the quantity of numbers, will return the sum
of the numbers entered. In addition, you shall also use a loop to prompt for and
read in all of the numbers, however many the user requested. You shall then
use, or rather, call this function appropriately when processing or calculating the
sum (menu option #1) or mean (menu option #2). This requirement also implies
that you shall print the sum and mean, for menu options #1 and #2, respectively,
from the main function.
5. You shall write one function that, given the quantity of numbers and a Boolean
indicating whether the minimum or maximum is to be determined, will return the
minimum or maximum of the numbers entered, depending on the value of the
Boolean. In addition, you shall also use a loop to prompt for and read in all of the
numbers, however many the user requested. You shall then use, or rather, call
this function appropriately when determining the minimum (menu option #3) or
maximum (menu option #4). This requirement also implies that you shall print the
minimum and maximum, respectively, for menu options #3 and #4, respectively,
from the main function.
6. Note that the functions written in requirements 4 and 5 are to be written as
overloaded functions (i.e., same function name, but with different function
signatures). This is my interpretation of your instructions:

 ``12345678910111213141516171819202122`` ``````//prototypes void displayMenu(); void displayInfo(); double calcStuff(int&); double calcStuff(int&, bool); //this is different than the first one because it takes 2 parameters int main() { //stuff } //actual functions double calcStuff(int &X) { //return the sum of X numbers } double calcStuff(int &X, bool min) { //if min === true, return the minimum of X numbers //else return the maximum of X numbers }`````` closed account (SNRzwA7f)
Thank you! That definitely added some insight and helped! But I'm still not sure about find mean. Also my sum expression is yield a decimal for some reason when I'm using integers:

How many positive numbers would you like to enter?
2
Please enter 2 numbers separated by a space and press <Enter> when completed.
5
5
The sum of your numbers is :8.76173 If your code for your summing function is the same as in the opening post, the reason is because you didn't initialize the variable sum to 0. (line 20)

However, your entire function needs to be re-written anyway because it doesn't fit the assignment specs.
Topic archived. No new replies allowed.