# Help with Program Pass by reference and call by value

Page 1 of 1

## 0 Replies - 1547 Views - Last Post: 13 February 2008 - 01:27 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=43227&amp;s=a53d6140db69db2ce475b2094bb89796&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Rstewart79

Reputation: 0
• Posts: 9
• Joined: 08-February 08

# Help with Program Pass by reference and call by value

Posted 13 February 2008 - 01:27 PM

Hello I need help with getting my program to read the values using call by reference and call by value. Below I have pasted my instructions, and I have set the functions up to do in each case .. however my instructor wants me be able to write these functions in a manner where they can be referenced throughout the program and not just in one if statement.

instructions :

FOR EACH YEAR, you must do the following steps 2-6 to make your prediction:
2) Calculate the simple gain as: I = P*R, where I is the amount of interest, P is the
principle, and R is the rate, determined randomly for each year from the range [-
0.15,0.25].
3) From the gain, calculate the tax as: Tax = I*r, where I is the amount of interest from
step 2 and r is the tax rate given by the user. Note: If there is no gain or a loss, the user
pays no tax (in this example, there are no tax write-offs).
4) Adjust the total for the gain (or loss).
5) Now, calculate the penalty caused by inflation as Penalty = total*inflation rate, where
total is the result of step 4 and the inflation rate was given by the user.
6) Subtract the loss by inflation in 5 from the total in 4.
7) As long as there are years remaining, go back to step 2.

In order to solve this problem, here are the (minimum) functions you should use.
Feel free to develop more to make things simpler for you:
- Calculate simple gain (pass-by-value). Given a principle and a rate, it calculates
the gain for one year. This will be used by the several of the calculations.
- Adjust gains for taxes (pass-by-reference). Given a gain and a tax rate, it reduces
the gain by the loss due to taxes.
- Adjust total for inflation (pass-by-reference). Given a total and an inflation rate,
reduce the amount by inflation.
- Driver (pass-by-value). Unit test each of your functions with all of your test cases
automatically and report the results. Makes testing easy!

```#include <iostream> //input and output library
#include <cstdlib>
#include <string>

using namespace std;

int main ()

{
double years;
double yearly_gain;
double tax;
double inflation;
double interest;
double principle;
double rate;
int sel;

while(sel !=4)
{
cout << "Welcome to Price Moneybags Financial Wizard!!" << endl;
cout << " " <<endl;
cout << "1)"<<" "<< "Price Moneybags Mutual Fund" << endl;
cout << "2)"<<" "<< "Price Moneybags Tax-Free Bonds" << endl;
cout << "3)"<<" "<< "Price Moneybags Bury in Ground Option" << endl;
cout << "4)"<<" "<< "Exit the program" << endl;
// This is just the begining of the program that shows the user the investment choices they have to choose
// from.
cout << " " << endl;

// Read in the value into sel as part of the loop. Validate it to also make sure it is not less than 1 or greater than 4.
// If invalid, clear the error flag of cin, ignore what was typed into the buffer (up to 100 chars or until newline is met)
// Put out the prompt again and wait for their next choice.
while (!(std::cin >> sel) || (sel < 1) || (sel > 4))
{
cin.clear();
cin.ignore(100, '\n');
cout << "Please input a selection between (1-4): " << endl;
}

/* These are switch statements, I use these to have the compilier take in the user input
and depending on what selection the customer uses, it will compute their investment based on their
choice.*/

if(sel == 1)//if statement, telling the program to compute the following based on the user input "sel"
{
cout << "How Much Money will you invest in our Mutual Fund?" << endl;
cin>> principle;
cout << "How many years will you be investing with us?" <<endl;
cin>> years;
cout << "What is the average tax rate (in decimal):" << endl;
cin>> tax;
cout << "What is the average rate of inflation(in decimal):" << endl;
cin>> inflation;
rate = ((double)rand() * 0.4 /(double)RAND_MAX);
interest = ((rate) * principle);
double gain = (double(interest)*(tax));

yearly_gain = (gain / years);

cout << "We Predict that your investment will be worth: " << gain << " in Todays dollars." << endl;
cout << "That is an average annual gain of: " << yearly_gain << ". " << endl;

}
if(sel == 2)//if statement, telling the program to compute the following based on the user input "sel"
{
cout << "How Much Money will you invest in our Tax Free Bonds?" << endl;
cin>> principle;
cout << "How many years will you be investing with us?" <<endl;
cin>> years;
cout << "This Investment option is free of Tazes" << endl;
cout << " " << endl;
cout << "What is the average rate of inflation:" << endl;
cin>> inflation;

}
if(sel ==3)//if statement, telling the program to compute the following based on the user input "sel"
{
cout << "How much money will you bury under ground?" << endl;
cin>> principle;
cout << "How many years will it remain buried?" <<endl;
cin>> years;
cout << "This Option is Tax Free" << endl;
cout <<" " << endl;
cout << "What is the average rate of inflation:" << endl;
cin>> inflation;

}

if(sel == 4)//if statement, telling the program to compute the following based on the user input "sel"
// exit statement, if the user inputs 4 program will exit.
{
cout<< "Thank you for using Price Money Bags investment Software!" << endl;
}
}
return 0;
}
```

Is This A Good Question/Topic? 0

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }