fork download
  1. #include <stdio.h>
  2.  
  3. #define RACES 10
  4. // race results
  5. struct raceResult
  6. {
  7. int raceNumber; // The unique Race Number
  8. int winningHorseNumber; // 1st place horse number for the race
  9. int placeHorseNumber; // 2nd place horse number for the race
  10. int showHorseNumber; // 3rd place horse number for the race
  11. };
  12. // assume you can access this array that contains the results of horse races for a given day
  13. struct raceResult theDailyResults[RACES];
  14. // your specific race wager
  15. struct raceWager
  16. {
  17. int raceNumber; // the race number where you bet your horse
  18. int myHorseNumber; // the horse you bet on
  19. float myBetAmount; // how much money you are betting
  20. char betType; // W for "Win", P for "Place", or S for "Show"
  21. };
  22. // call this function and pass in your bet to see how much you won ... if you lost or there
  23. // is an error in your wager info, it just returns zero
  24. float myHorseBet (struct raceWager myBet, struct raceResult theDailyResults[])
  25. {
  26. //input validations
  27. if (myBet.myBetAmount < 5 || myBet.myBetAmount > 1000)
  28. {
  29. return 0;
  30. }
  31. if (myBet.raceNumber < 1 || myBet.raceNumber > RACES)
  32. {
  33. return 0;
  34. }
  35. if (myBet.myHorseNumber < 1 || myBet.myHorseNumber > 20)
  36. {
  37. return 0;
  38. }
  39. //get the result for given race number
  40. struct raceResult result = {0};
  41. int raceFound = 0; //caveman boolean for compatability
  42. for (int i = 0; i < RACES; i++)
  43. {
  44. if (theDailyResults[i].raceNumber == myBet.raceNumber)
  45. {
  46. result = theDailyResults[i];
  47. raceFound = 1;
  48. break;
  49. }
  50. }
  51.  
  52. if (!raceFound)
  53. {
  54. return 0;
  55. }
  56.  
  57. //switch case for easy calculations
  58. switch (myBet.betType)
  59. {
  60. case 'W': // Win
  61. if (myBet.myHorseNumber == result.winningHorseNumber)
  62. {
  63. return myBet.myBetAmount * 10;
  64. }
  65. break;
  66.  
  67. case 'P': // Place
  68. if (myBet.myHorseNumber == result.winningHorseNumber ||
  69. myBet.myHorseNumber == result.placeHorseNumber)
  70. {
  71. return myBet.myBetAmount * 5;
  72. }
  73. break;
  74.  
  75. case 'S': // Show
  76. if (myBet.myHorseNumber == result.winningHorseNumber ||
  77. myBet.myHorseNumber == result.placeHorseNumber ||
  78. myBet.myHorseNumber == result.showHorseNumber)
  79. {
  80. return myBet.myBetAmount * 3;
  81. }
  82. break;
  83.  
  84. default:
  85. return 0;
  86. }
  87. }
  88.  
  89. int main()
  90. {
  91. // Sample daily race results
  92. struct raceResult theDailyResults[RACES] = {
  93. {1, 5, 3, 8},
  94. {2, 12, 7, 10},
  95. {3, 15, 20, 6},
  96. {4, 2, 1, 3},
  97. {5, 9, 8, 4},
  98. {6, 11, 13, 5},
  99. {7, 19, 14, 17},
  100. {8, 6, 7, 8},
  101. {9, 3, 1, 2},
  102. {10, 20, 18, 16}
  103. };
  104.  
  105. // Sample bet
  106. struct raceWager myBet = {3, 20, 50, 'W'};
  107.  
  108. // Calculate winnings
  109. float winnings = myHorseBet(myBet, theDailyResults);
  110. if (winnings > 0) {
  111. printf("You won $%.2f!\n", winnings);
  112. } else {
  113. printf("Sorry, you lost your bet.\n");
  114. }
  115.  
  116. return 0;
  117. }
Success #stdin #stdout 0s 5284KB
stdin
Standard input is empty
stdout
You won $50.00!