fork download
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5. #define RACES 10
  6.  
  7. // race results
  8. struct raceResult
  9. {
  10. int raceNumber; // The unique Race Number
  11. int winningHorseNumber; // 1st place horse number for the race
  12. int placeHorseNumber; // 2nd place horse number for the race
  13. int showHorseNumber; // 3rd place horse number for the race
  14. };
  15.  
  16.  
  17. // assume you can access this array that contains the results of horse races for a given day
  18. struct raceResult theDailyResults[RACES];
  19.  
  20. // your specific race wager
  21. struct raceWager
  22. {
  23. int raceNumber; // the race number where you bet your horse
  24. int myHorseNumber; // the horse you bet on
  25. float myBetAmount; // how much money you are betting
  26. char betType; // W for "Win", P for "Place", or S for "Show"
  27. };
  28.  
  29.  
  30.  
  31. // call this function and pass in your bet to see how much you won ... if you lost or there
  32. // is an error  in your wager info, it just returns zero
  33. float myHorseBet (struct raceWager myBet, struct raceResult theDailyResults[])
  34. {
  35. int validRaceNumber = (myBet.raceNumber >= 1 && myBet.raceNumber <= 10);
  36. int validHorseNumber = (myBet.myHorseNumber >= 1 && myBet.myHorseNumber <= 20);
  37. int validBetAmount = (myBet.myBetAmount >= 5 && myBet.myBetAmount <= 1000);
  38. if (!validRaceNumber || !validHorseNumber || !validBetAmount) // Invalid bet
  39. {
  40. return 0;
  41. }
  42. for (int i = 0; i < RACES; i++)
  43. {
  44. if (theDailyResults[i].raceNumber == myBet.raceNumber) // race number needs to match
  45. {
  46. struct raceResult race = theDailyResults[i];
  47. float winnings = 0;
  48. printf("My bet %d %d %f %c\n", myBet.raceNumber, myBet.myHorseNumber, myBet.myBetAmount, myBet.betType);
  49. printf("Race result %d %d %d %d\n", race.raceNumber, race.showHorseNumber, race.placeHorseNumber, race.winningHorseNumber);
  50. if (myBet.betType == 'S') // Show
  51. {
  52. if (myBet.myHorseNumber == race.showHorseNumber || myBet.myHorseNumber == race.placeHorseNumber || myBet.myHorseNumber == race.winningHorseNumber)
  53. {
  54. winnings = (myBet.myBetAmount * 3);
  55. return winnings;
  56. }
  57. }
  58. else if (myBet.betType == 'P') // Place
  59. {
  60. if (myBet.myHorseNumber == race.winningHorseNumber || myBet.myHorseNumber == race.placeHorseNumber)
  61. {
  62. winnings = (myBet.myBetAmount * 5);
  63. return winnings;
  64. }
  65. }
  66. else if (myBet.betType == 'W') // Win
  67. {
  68. if (myBet.myHorseNumber == race.winningHorseNumber)
  69. {
  70. winnings = (myBet.myBetAmount * 10);
  71. return winnings;
  72. }
  73. }
  74. else return 0;
  75. }
  76. }
  77. return 0; // Invalid bet type
  78. }
  79.  
  80. int main(void) {
  81. struct raceWager myBet = {1, 1700, 5, 'S'};
  82. struct raceResult dailyResults[] = {{2, 1, 2, 3}};
  83. float winnings = myHorseBet(myBet, dailyResults);
  84.  
  85. printf("%f\n", winnings);
  86. }
Success #stdin #stdout 0s 5292KB
stdin
Standard input is empty
stdout
0.000000