double future_value(double p)
double future_value(double p)
{
. . .
}
double future_value(double p)
{ double b = 1000 * pow(1 + p / 100, 10);
return b;
}
Syntax 5.1 : Function Definition return_type function_name(parameter1, ..., parametern)
|
double future_value(double initial_balance, double p, int n)
{ double b = initial_balance * pow(1 + p / 100, n);
return b;
}
double balance = future_value(1000, rate, 10);
/**
Computes the value of an investment with compound interest.
@param initial_balance - the initial value of the investment
@param p the interest rate pre period in percent
@param n the number of periods the investment is held
@return the balance after n periods
*/
double future_value(double initial_balance, double p, int n)
{ double b = initial_balance * pow(1 + p / 100, n);
return b;
}
Syntax 5.3 : return statement return expression;
|
double future_value(double initial_balance, double p, int n)
{ if (n < 0) return 0;
if (p < 0) return 0;
double b = initial_balance * pow(1 + p / 100, n);
return b;
}
double future_value(double initial_balance, double p, int n)
{ if (p >= 0)
return initial_balance * pow(1 + p / 100, n);
/* Error */
}
bool approx_equal(double x, double y)
{ const double EPSILON = 1E-14;
if (x == 0) return fabs(y) <= EPSILON;
if (y == 0) return fabs(x) <= EPSILON;
return fabs(x - y) / max(fabs(x), fabs(y)) <= EPSILON;
}
b = future_value(total / 2, rate, year2 - year1);
double future_value(double initial_balance, double p, int n)
{ p = 1 + p / 100;
double b = initial_balance * pow(p, n);
return b;
}
print_time(now);
void print_time(Time t)
{ cout << t.get_hours() << ":";
if (t.get_minutes() < 10) cout << "0";
cout << t.get_minutes() << ":";
if (t.get_seconds() < 10) cout << "0";
cout << t.get_seconds();
}
void raise_salary(Employee& e, double by)
{ double new_salary = e.get_salary() * ( 1 + by / 100);
e.set_salary(new_salary);
}
Syntax 5.4 : Reference Parameters type_name& parameter_name
|
Syntax 5.5 : Constant Reference Parameters const type_name& parameter_name
|
double future_value(double initial_balance, double p, int n)
{ double r = initial_balance * pow(1 + p / 100, n);
return r;
}
int main()
{ cout << "Please enter the interest rate in percent: ";
double r;
cin >> r;
double balance = future_value(10000, r, 10);
cout << "After 10 years the balance is" << balance
<< "\n";
return 0;
}
/**
Turns a number into its English name.
@param n a positive integer < 1,000,000
@return the name of n (e.g. "two hundred seventy four"
*/
string int_name(int n);
/**
Turns a digit into its English name
@param n an integer between 1 and 9
@return the name of n ("one" ... "nine")
*/
string digit_name(int n);
/**
Turns a number between 10 and 19 into its English name.
@param n an integer between 10 and 19
@return the name of n ("ten"..."nineteen")
*/
string teen_name(int n);
/**
Gives the English name of a multiple of 10
@param n an integer between 2 and 9
@return the name of 10 * n ("twenty"..."ninety")
*/
string tens_name(int n);
string int_name(int n)
{ int c = n; /* the part that needs to be converted */
string r; /* the return value */
if (c >= 1000)
{ r = name of thousands in c + "thousand"
remove thousands from c
}
if (c >= 100)
{ r = r + name of hundreds in c + "hundreds"
remove hundreds from c
}
if (c >= 20)
{ r = r + name of tens in c
remove tens from c
}
if (c >= 10)
{ r = r + name of c
c = 0
}
if (c > 0)
r = r + name of c;
return r;
}
if (c >= 1000)
{ r = int_name(c / 1000) + " thousand";
c = c % 1000;
}
int_name(n = 416) c r 416 ""
int_name(n = 416) c r 416 ""
digit_name(n = 4) Returns "four"
int_name(n = 416) c r416"" 16 "four hundred"
int_name(n = 416) c r416""16"four hundred" 0 "four hundred sixteen"
Syntax 5.6 : Assertions assert(expression);
|
double future_value(double initial_balance, double p, int n)
{ assert(p >= 0);
assert(n >= 0);
return initial_balance * pow(1 + p / 100, n);
}
/**
Computes the value of an investment with compound interest.
@param initial_balance the initial value of the investment
@param p the interest rate in percent; must be >= 0
@param n the number of periods the investment is held; must be >= 0
@return the balance after n periods
*/