fork download
  1. #include<iostream>
  2. #include<iomanip>
  3.  
  4. int main()
  5. {
  6. std::srand(static_cast<unsigned>(std::time(nullptr)));
  7.  
  8. int iterations;
  9. std::cout << "How many iterations would you like(<0 = quit): ";
  10. std::cin >> iterations;
  11.  
  12. const int initialPosition = 5;
  13. int currentPosition = initialPosition;
  14.  
  15. for(int i = 0; i < iterations; ++i)
  16. {
  17. int random = std::rand();
  18.  
  19. if(random % 2 == 0) //random & 1 == 1 -- even
  20. {
  21. ++currentPosition;
  22. }
  23. else
  24. {
  25. --currentPosition;
  26. }
  27.  
  28. std::cout << "Iteration: " << std::setw(4) << i+1 << ':' << std::setw(currentPosition) << '*' << std::endl;
  29. }
  30.  
  31. }
Success #stdin #stdout 0s 3344KB
stdin
10
stdout
How many iterations would you like(<0 = quit): Iteration:    1:   *
Iteration:    2:    *
Iteration:    3:   *
Iteration:    4:  *
Iteration:    5: *
Iteration:    6:  *
Iteration:    7: *
Iteration:    8:  *
Iteration:    9: *
Iteration:   10:*