Question

In: Computer Science

C++ Objective To wrap up this week, we will be looking at the Call by Value...

C++
Objective
To wrap up this week, we will be looking at the Call by Value mechanism, and how it differs from Call by Reference; whether it is how they work in memory or how they help you accomplish a practical task, knowing the differences between these two mechanisms is important foundational knowledge.

**You should have one main function + 4 other functions for your submission for this lab. you have no need for extra functions beyond that, and you should not have less. You may use any pre existing functions already defined in class or a previous lab.

-Write a function storeTotal(,) that takes two arguments of type double, and has a return type of type Boolean. This function will take the number 256 and divide it by the second parameter, and add the result to the first parameter. It will return true afterwards.

-You should be making mindful decisions of which parameters should be call by value and which should be call by reference.

-If dividing by the second parameter would result in a run time error, the program does not do the calculation, and instead returns false.

-Ask the user to input two numbers, one at a time, discarding excess input each time.

-The program should keep looping until the user enters valid input.

-Once the user enters input, call function storeTotal appropriately.

-Whether storeTotal runs successfully (returns true) or not (returns false), display an appropriate message.

-Output the results of the variable that is cumulating value. This number is ALWAYS displayed in scientific notation, accurate to 3 decimal places

Repeat this 2 times.

Sample Output

How much do you already have? A

Invalid Input!

How much do you already have? Bck

Invalid Input!

How much do you already have? 42.4

What is the split factor? ,!

Invalid Input!

What is the split factor? 3.5

You now have 1.155e+002

How much do you already have? 35.6

What is the split factor? 0

That didn't go well, you still have 3.560e+001

*Explanation* 256/3.5 and then added to 42.4 gives 115.54, which, in scientific notation, gives the output above.

*Note* How scientific notation is displayed can vary from compiler to compiler, as long as you are getting it done through proper knowledge of C++ then the output does not need to look exactly the same.

Solutions

Expert Solution

#include<iostream>
using namespace std;

//Function storeTotal in which the 1st argument is passed by reference andd 2nd is called by value
bool storeTotal(double &n,double s)
{
    double r;
    if(s==0)
        return false;
    else
    {
        r=256/s;
        n+=r;
        return true;
    }
}

//Function to take input of number
void input_num(double &num)
{
    cout<<"How much do you already have? ";
    //Loop to check for valid inputs
    while(!(cin>>num))
    {
        cout<<"Invalid Input!"<<endl;
        cin.clear();
        cin.ignore(132,'\n');       //Clears all the 132 previous inputs given by the user
        cout<<"How much do you already have? ";
    }
}

//Function to take input of split
void input_split(double &split)
{
    cout<<"What is the split factor? ";
    //Loop to check for valid inputs
    while(!(cin>>split))
    {
        cout<<"Invalid Input!"<<endl;
        cin.clear();
        cin.ignore(132,'\n');    //Clears all the 132 previous inputs given by the user
        cout<<"What is the split factor? ";
    }
}

int main()
{
    double num,split;
    bool flag;
    input_num(num);
    input_split(split);
    flag=storeTotal(num,split);
    cout.precision(3);  //To set the precision to 3 decimal places
    if(flag==false)
    {
        cout<<"That didn't go well, you still have ";
        cout<<scientific;       //To show scientific notation of the number
        cout<<num<<endl;
    }
    else
    {
        cout<<"You now have ";
        cout<<scientific;
        cout<<num<<endl;
    }
    return 0;
}

The output :

PLEASE LIKE THE ANSWER IF YOU FIND IT HELPFUL OR YOU CAN COMMENT IF YOU NEED CLARITY / EXPLANATION ON ANY POINT.


Related Solutions

Lab 8 Individual Wrap Up Wrap up question 1: Is the tennis ball catch more similar...
Lab 8 Individual Wrap Up Wrap up question 1: Is the tennis ball catch more similar to the happy ball or the sad ball? Explain your answer using the energies in each scenario. Wrap up question 2: Describe the resulting behavior of a tennis ball if a force momentarily did positive work on the ball, then a smaller force consistently did negative work on the ball, as the ball moved downwards through the air. Be specific about changes to the...
As we wrap up our course in principle of managerial accounting, I would like you to...
As we wrap up our course in principle of managerial accounting, I would like you to discuss how this class could be relevant to your future professional careers. Many of you won't become professional accountants, but there are many concepts that you could see again in your careers. Please take a minimum of 2 paragraphs and discuss at least 2 significant concepts that you have learned that not only caught your attention, but also could be very valuable to take...
This week we are looking at the ACA. There are many strong political views of this...
This week we are looking at the ACA. There are many strong political views of this legislation, but we need to look at what worked and what did not. Can the Law be amended to work better than its current form? Or does it need to be scrapped and completely re-written? Which option is more cost-effective? These are just a couple of the difficult questions. Analyze how cost-benefit analysis affects legislative efforts Analyze legislative intent of bills Identify proponents and...
We are looking at the market for gasoline in the United States, this week. Begin with...
We are looking at the market for gasoline in the United States, this week. Begin with the market in equilibrium. A report is released that we should expect the price of gasoline to rise in the near future, with up to a 50-cent increase sometime next week. Assume that we all have access to this information. Graph the changes and answer the following questions: How do the unhappy participants react? Question options: They run home crying They compete with other...
In finance we often assume that the objective of the firm is to maximise the value...
In finance we often assume that the objective of the firm is to maximise the value of the firm. Why might this be a desirable objective? Why might managers not pursue such an objective?
So, this week we are looking at how to govern the fragmented metropolis. The metropolitan battleground...
So, this week we are looking at how to govern the fragmented metropolis. The metropolitan battleground is fragmented along the space, race and ethnicity lines in both the cities and suburbs. How relevant is the State's input in metropolitan governance? Along the same line, walling off the suburbs by creating gated communities leads to spatial inequity in development. What are some ways central cities can gain some back some of this urban economic power?
we are looking at the "changeSpecificIndex" method. i was able to replace the value of that...
we are looking at the "changeSpecificIndex" method. i was able to replace the value of that specific index, but im stumped at adding a value to a specific index AND shifting the values after it to the next index. Here is my code _____________________________________________ import java.util.Scanner; import java.util.Arrays; public class ArrayMenu{ static int count; static Scanner kb = new Scanner(System.in);             public static void main(){ int item=0; int[] numArray=new int[100]; count=0;       while (item !=8){ menu();...
While looking at budgeting this week, we encountered numerous forms of variances (the difference between a...
While looking at budgeting this week, we encountered numerous forms of variances (the difference between a standard and the actual cost). We read how variances can be favorable (the company makes money) or unfavorable. Sometimes, a favorable variance leads to an unintended negative consequence (such as when a burger joint uses less meat in its patties, which causes dissatisfied customers who fail to return to the restaurant). Now it’s your turn to come up with an example of variances. Think...
In this course wrap-up discussion, please reflect on your experience in the course and address the...
In this course wrap-up discussion, please reflect on your experience in the course and address the following items: Identify one of your major takeaways from this course. What aspect of the course helped you achieve this? How will you apply this knowledge to your practice and career?
"Course Wrap-Up" Please respond to the following: • Discuss the way in which the concepts of...
"Course Wrap-Up" Please respond to the following: • Discuss the way in which the concepts of this course can be applied to your current or future work position. Course name: Auditing and Attestation. Business environment and concept
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT