fork download
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <ctime>
  4.  
  5. using namespace std;
  6.  
  7. void moveForward() {
  8. cout << "Moving forward" << endl;
  9. }
  10.  
  11. void turnRight() {
  12. cout << "Turning right" << endl;
  13. }
  14.  
  15. void stopMovement() {
  16. cout << "Stopping movement" << endl;
  17. }
  18.  
  19. int measureDistance() {
  20. return rand() % 50 + 1; // Simulate distance measurement (1-50 cm)
  21. }
  22.  
  23. void dropPackage() {
  24. cout << "Dropping package" << endl;
  25. }
  26.  
  27. int main() {
  28. srand(time(0)); // Seed for random values
  29.  
  30. cout << "Starting simulation..." << endl;
  31.  
  32. for (int i = 0; i < 10; i++) { // Simulating 10 sensor readings
  33. int distance = measureDistance();
  34. cout << "Measured Distance: " << distance << " cm" << endl;
  35.  
  36. if (distance < 10) {
  37. stopMovement();
  38. dropPackage();
  39. break;
  40. } else if (distance < 20) {
  41. stopMovement();
  42. turnRight();
  43. } else {
  44. moveForward();
  45. }
  46. }
  47.  
  48. cout << "Simulation complete!" << endl;
  49. return 0;
  50. }
  51.  
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout
Starting simulation...
Measured Distance: 9 cm
Stopping movement
Dropping package
Simulation complete!