fork download
  1. #include <iostream>
  2. #include <cstdlib>
  3.  
  4. using namespace std;
  5.  
  6. void somefunc() {
  7. int tot_mov = 0, tot_pub = 0, tot_hom = 0;
  8. double avg_mov = 0., avg_pub = 0., avg_hom = 0.;
  9. int i = 0, x = 0;
  10.  
  11. cout << "enter the starting position.\n";
  12. cin >> i;
  13. cout << "At starting block " << i << endl;
  14.  
  15. for(int j = 0; j < 1000000; j++)
  16. {
  17.  
  18. while(i < 8 && i > 1)
  19. {
  20. x = rand() % 3;
  21. if(x == 0)
  22. {
  23. i++;
  24. tot_mov++;
  25. }
  26. else
  27. {
  28. i--;
  29. tot_mov++;
  30. }
  31. }
  32.  
  33. if(i == 1)
  34. {
  35. tot_pub++;
  36. }
  37.  
  38. if(i == 8)
  39. {
  40. tot_hom++;
  41. }
  42.  
  43. }
  44.  
  45. avg_mov = tot_mov / 1000000;
  46. avg_pub = tot_pub / 1000000;
  47. avg_hom = tot_hom / 1000000;
  48.  
  49. cout << "Total moves " << tot_mov << endl;
  50. cout << "Average moves " << avg_mov << endl;
  51. cout << "Total Home " << tot_hom << endl;
  52. cout << "Average home " << avg_hom << endl;
  53. cout << "Total pub " << tot_pub << endl;
  54. cout << "Average pub " << avg_pub << endl;
  55.  
  56. return;
  57. }
  58.  
  59. void somefunc_modded() {
  60. int tot_mov = 0, tot_pub = 0, tot_hom = 0;
  61. double avg_mov = 0., avg_pub = 0., avg_hom = 0.;
  62. int i = 0;
  63.  
  64. cout << "enter the starting position.\n";
  65. cin >> i;
  66. cout << "At starting block " << i << endl;
  67.  
  68. for(int j = 0; j < 1000000; j++)
  69. {
  70. int i_temp = i;
  71.  
  72. while(i_temp < 8 && i_temp > 1)
  73. {
  74. int x = rand() % 3;
  75. if(x == 0)
  76. {
  77. i_temp++;
  78. tot_mov++;
  79. }
  80. else
  81. {
  82. i_temp--;
  83. tot_mov++;
  84. }
  85. }
  86.  
  87. if(i_temp == 1)
  88. {
  89. tot_pub++;
  90. }
  91.  
  92. if(i_temp == 8)
  93. {
  94. tot_hom++;
  95. }
  96.  
  97. }
  98.  
  99. // Casts are optional, they're there to prevent
  100. // any integer truncation before value is saved to
  101. // the double.
  102. avg_mov = static_cast<double>(tot_mov) / 1000000;
  103. avg_pub = static_cast<double>(tot_pub) / 1000000;
  104. avg_hom = static_cast<double>(tot_hom) / 1000000;
  105.  
  106. cout << "Total moves " << tot_mov << endl;
  107. cout << "Average moves " << avg_mov << endl;
  108. cout << "Total Home " << tot_hom << endl;
  109. cout << "Average home " << avg_hom << endl;
  110. cout << "Total pub " << tot_pub << endl;
  111. cout << "Average pub " << avg_pub << endl;
  112.  
  113. return;
  114. }
  115.  
  116. int main() {
  117. somefunc();
  118. std::cout << std::endl << "Once more, with feeling!" << std::endl << std::endl;
  119. somefunc_modded();
  120. }
Success #stdin #stdout 0.17s 3420KB
stdin
3
3
stdout
enter the starting position.
At starting block 3
Total moves 2
Average moves 0
Total Home 0
Average home 0
Total pub 1000000
Average pub 1

Once more, with feeling!

enter the starting position.
At starting block 3
Total moves 5502595
Average moves 5.5026
Total Home 23391
Average home 0.023391
Total pub 976609
Average pub 0.976609