In: Computer Science
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.
#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.