cwin.coord(0, a, MAX_ITERATIONS,
-a);
do
{ xold = xnew;
xnew =
(xold + a / xold) / 2;
cwin <<
Point(iteration, xnew);
iteration++;
} while (iteration < MAX_ITERATIONS);
return 0;
}
Генериране на случайни събития и симулации
Вложени цикли
* Таблица за сумата по влог за 5, 10, 15, 20, 25 и 30 години при годишна
лихва от 5 до 10% (през 0.5%).
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
double future_value(double init_balance,
double p, int n)
{ double b = init_balance*pow(1
+ p/12/100, 12*n);
return b; }
int main()
{
const double RATE_MIN = 5;
const double RATE_MAX = 10;
const double RATE_INCR = 0.5;
const int YEAR_MIN = 5;
const int YEAR_MAX = 30;
const int YEAR_INCR = 5;
/* print table header */
cout << " Rate ";
int year;
for (year=YEAR_MIN; year<=YEAR_MAX;
year=year+YEAR_INCR)
{ cout << setw(2) <<
year << " years "; }
cout << "\n";
cout << fixed << setprecision(2);
double rate;
double init_bal = 10000;
for (rate=RATE_MIN; rate<=RATE_MAX;
rate=rate+RATE_INCR)
/* print table row */
{ int year;
cout << setw(5) <<
rate;
for (year=YEAR_MIN; year<=YEAR_MAX;
year=year+YEAR_INCR)
{ double balance = future_value(init_bal,
rate, year);
cout <<
setw(10) << balance; }
cout << "\n";
}
return 0;
}
Rate 5 years 10
years 15 years 20 years
5.0 12833.59 16470.09 21137.04 27126.40 5.5 13157.04 17310.76 22775.84 29966.26 6.0 13488.50 18193.97 24540.94 33102.04 6.5 13828.17 19121.84 26442.01 36564.47 7.0 14176.25 20096.61 28489.47 40387.39 7.5 14532.94 21120.65 30694.52 44608.17 8.0 14898.46 22196.40 33069.21 49268.03 8.5 15273.01 23326.47 35626.53 54412.43 9.0 15656.81 24513.57 38380.43 60091.52 9.5 16050.09 25760.55 41345.93 66360.61 10.0 16453.09 27070.41 44539.20 73280.74 |
Област на действие на променлива