#include <iostream>
#include <cstdlib>
using namespace std;
void somefunc() {
int tot_mov = 0, tot_pub = 0, tot_hom = 0;
double avg_mov = 0., avg_pub = 0., avg_hom = 0.;
int i = 0, x = 0;
cout << "enter the starting position.\n";
cin >> i;
cout << "At starting block " << i << endl;
for(int j = 0; j < 1000000; j++)
{
while(i < 8 && i > 1)
{
x = rand() % 3;
if(x == 0)
{
i++;
tot_mov++;
}
else
{
i--;
tot_mov++;
}
}
if(i == 1)
{
tot_pub++;
}
if(i == 8)
{
tot_hom++;
}
}
avg_mov = tot_mov / 1000000;
avg_pub = tot_pub / 1000000;
avg_hom = tot_hom / 1000000;
cout << "Total moves " << tot_mov << endl;
cout << "Average moves " << avg_mov << endl;
cout << "Total Home " << tot_hom << endl;
cout << "Average home " << avg_hom << endl;
cout << "Total pub " << tot_pub << endl;
cout << "Average pub " << avg_pub << endl;
return;
}
void somefunc_modded() {
int tot_mov = 0, tot_pub = 0, tot_hom = 0;
double avg_mov = 0., avg_pub = 0., avg_hom = 0.;
int i = 0;
cout << "enter the starting position.\n";
cin >> i;
cout << "At starting block " << i << endl;
for(int j = 0; j < 1000000; j++)
{
int i_temp = i;
while(i_temp < 8 && i_temp > 1)
{
int x = rand() % 3;
if(x == 0)
{
i_temp++;
tot_mov++;
}
else
{
i_temp--;
tot_mov++;
}
}
if(i_temp == 1)
{
tot_pub++;
}
if(i_temp == 8)
{
tot_hom++;
}
}
// Casts are optional, they're there to prevent
// any integer truncation before value is saved to
// the double.
avg_mov = static_cast<double>(tot_mov) / 1000000;
avg_pub = static_cast<double>(tot_pub) / 1000000;
avg_hom = static_cast<double>(tot_hom) / 1000000;
cout << "Total moves " << tot_mov << endl;
cout << "Average moves " << avg_mov << endl;
cout << "Total Home " << tot_hom << endl;
cout << "Average home " << avg_hom << endl;
cout << "Total pub " << tot_pub << endl;
cout << "Average pub " << avg_pub << endl;
return;
}
int main() {
somefunc();
std::cout << std::endl << "Once more, with feeling!" << std::endl << std::endl;
somefunc_modded();
}