#include <stdio.h>
#define RACES 10
// race results
struct raceResult
{
int raceNumber; // The unique Race Number
int winningHorseNumber; // 1st place horse number for the race
int placeHorseNumber; // 2nd place horse number for the race
int showHorseNumber; // 3rd place horse number for the race
};
// assume you can access this array that contains the results of horse races for a given day
struct raceResult theDailyResults[RACES];
// your specific race wager
struct raceWager
{
int raceNumber; // the race number where you bet your horse
int myHorseNumber; // the horse you bet on
float myBetAmount; // how much money you are betting
char betType; // W for "Win", P for "Place", or S for "Show"
};
// call this function and pass in your bet to see how much you won ... if you lost or there
// is an error in your wager info, it just returns zero
float myHorseBet (struct raceWager myBet, struct raceResult theDailyResults[])
{
//input validations
if (myBet.myBetAmount < 5 || myBet.myBetAmount > 1000)
{
return 0;
}
if (myBet.raceNumber < 1 || myBet.raceNumber > RACES)
{
return 0;
}
if (myBet.myHorseNumber < 1 || myBet.myHorseNumber > 20)
{
return 0;
}
//get the result for given race number
struct raceResult result = {0};
int raceFound = 0; //caveman boolean for compatability
for (int i = 0; i < RACES; i++)
{
if (theDailyResults[i].raceNumber == myBet.raceNumber)
{
result = theDailyResults[i];
raceFound = 1;
break;
}
}
if (!raceFound)
{
return 0;
}
//switch case for easy calculations
switch (myBet.betType)
{
case 'W': // Win
if (myBet.myHorseNumber == result.winningHorseNumber)
{
return myBet.myBetAmount * 10;
}
break;
case 'P': // Place
if (myBet.myHorseNumber == result.winningHorseNumber ||
myBet.myHorseNumber == result.placeHorseNumber)
{
return myBet.myBetAmount * 5;
}
break;
case 'S': // Show
if (myBet.myHorseNumber == result.winningHorseNumber ||
myBet.myHorseNumber == result.placeHorseNumber ||
myBet.myHorseNumber == result.showHorseNumber)
{
return myBet.myBetAmount * 3;
}
break;
default:
return 0;
}
}
int main()
{
// Sample daily race results
struct raceResult theDailyResults[RACES] = {
{1, 5, 3, 8},
{2, 12, 7, 10},
{3, 15, 20, 6},
{4, 2, 1, 3},
{5, 9, 8, 4},
{6, 11, 13, 5},
{7, 19, 14, 17},
{8, 6, 7, 8},
{9, 3, 1, 2},
{10, 20, 18, 16}
};
// Sample bet
struct raceWager myBet = {3, 20, 50, 'W'};
// Calculate winnings
float winnings = myHorseBet(myBet, theDailyResults);
if (winnings > 0) {
printf("You won $%.2f!\n", winnings
); } else {
printf("Sorry, you lost your bet.\n"); }
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CgojZGVmaW5lIFJBQ0VTIDEwCi8vIHJhY2UgcmVzdWx0cwpzdHJ1Y3QgcmFjZVJlc3VsdAp7CiAgICBpbnQgcmFjZU51bWJlcjsgICAgICAgICAgICAgICAgICAgIC8vIFRoZSB1bmlxdWUgUmFjZSBOdW1iZXIKCWludCB3aW5uaW5nSG9yc2VOdW1iZXI7ICAgLy8gMXN0IHBsYWNlIGhvcnNlIG51bWJlciBmb3IgdGhlIHJhY2UKICAgIGludCBwbGFjZUhvcnNlTnVtYmVyOyAgICAgICAvLyAybmQgcGxhY2UgaG9yc2UgbnVtYmVyIGZvciB0aGUgcmFjZQogICAgaW50IHNob3dIb3JzZU51bWJlcjsgICAgICAgLy8gM3JkIHBsYWNlIGhvcnNlIG51bWJlciBmb3IgdGhlIHJhY2UKfTsKLy8gYXNzdW1lIHlvdSBjYW4gYWNjZXNzIHRoaXMgYXJyYXkgdGhhdCBjb250YWlucyB0aGUgcmVzdWx0cyBvZiBob3JzZSByYWNlcyBmb3IgYSBnaXZlbiBkYXkKc3RydWN0IHJhY2VSZXN1bHQgdGhlRGFpbHlSZXN1bHRzW1JBQ0VTXTsKLy8geW91ciBzcGVjaWZpYyByYWNlIHdhZ2VyCnN0cnVjdCByYWNlV2FnZXIKewogICAgaW50IHJhY2VOdW1iZXI7ICAgICAgICAgICAgICAgIC8vIHRoZSByYWNlIG51bWJlciB3aGVyZSB5b3UgYmV0IHlvdXIgaG9yc2UKICAgIGludCBteUhvcnNlTnVtYmVyOyAgICAgICAvLyB0aGUgaG9yc2UgeW91IGJldCBvbgogICAgZmxvYXQgbXlCZXRBbW91bnQ7ICAgICAgICAgLy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aWYgKHRoZURhaWx5UmVzdWx0c1tpXS5yYWNlTnVtYmVyID09IG15QmV0LnJhY2VOdW1iZXIpIAogICAgICAgIHsKICAgICAgICAgICAgcmVzdWx0ID0gdGhlRGFpbHlSZXN1bHRzW2ldOwogICAgICAgICAgICByYWNlRm91bmQgPSAxOwogICAgICAgICAgICBicmVhazsKICAgICAgICB9CiAgICB9CiAgICAKICAgIGlmICghcmFjZUZvdW5kKSAKICAgIHsKICAgICAgICByZXR1cm4gMDsKICAgIH0KICAgIAogICAgLy9zd2l0Y2ggY2FzZSBmb3IgZWFzeSBjYWxjdWxhdGlvbnMKICAgIHN3aXRjaCAobXlCZXQuYmV0VHlwZSkgCiAgICB7CiAgICAgICAgY2FzZSAnVyc6IC8vIFdpbgogICAgICAgICAgICBpZiAobXlCZXQubXlIb3JzZU51bWJlciA9PSByZXN1bHQud2lubmluZ0hvcnNlTnVtYmVyKSAKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgcmV0dXJuIG15QmV0Lm15QmV0QW1vdW50ICogMTA7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgYnJlYWs7CgogICAgICAgIGNhc2UgJ1AnOiAvLyBQbGFjZQogICAgICAgICAgICBpZiAobXlCZXQubXlIb3JzZU51bWJlciA9PSByZXN1bHQud2lubmluZ0hvcnNlTnVtYmVyIHx8CiAgICAgICAgICAgICAgICBteUJldC5teUhvcnNlTnVtYmVyID09IHJlc3VsdC5wbGFjZUhvcnNlTnVtYmVyKSAKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgcmV0dXJuIG15QmV0Lm15QmV0QW1vdW50ICogNTsKICAgICAgICAgICAgfQogICAgICAgICAgICBicmVhazsKCiAgICAgICAgY2FzZSAnUyc6IC8vIFNob3cKICAgICAgICAgICAgaWYgKG15QmV0Lm15SG9yc2VOdW1iZXIgPT0gcmVzdWx0Lndpbm5pbmdIb3JzZU51bWJlciB8fAogICAgICAgICAgICAgICAgbXlCZXQubXlIb3JzZU51bWJlciA9PSByZXN1bHQucGxhY2VIb3JzZU51bWJlciB8fAogICAgICAgICAgICAgICAgbXlCZXQubXlIb3JzZU51bWJlciA9PSByZXN1bHQuc2hvd0hvcnNlTnVtYmVyKSAKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgcmV0dXJuIG15QmV0Lm15QmV0QW1vdW50ICogMzsKICAgICAgICAgICAgfQogICAgICAgICAgICBicmVhazsKCiAgICAgICAgZGVmYXVsdDoKICAgICAgICAgICAgcmV0dXJuIDA7CiAgICB9Cn0KCmludCBtYWluKCkKewoJLy8gU2FtcGxlIGRhaWx5IHJhY2UgcmVzdWx0cwogICAgc3RydWN0IHJhY2VSZXN1bHQgdGhlRGFpbHlSZXN1bHRzW1JBQ0VTXSA9IHsKICAgICAgICB7MSwgNSwgMywgOH0sCiAgICAgICAgezIsIDEyLCA3LCAxMH0sCiAgICAgICAgezMsIDE1LCAyMCwgNn0sCiAgICAgICAgezQsIDIsIDEsIDN9LAogICAgICAgIHs1LCA5LCA4LCA0fSwKICAgICAgICB7NiwgMTEsIDEzLCA1fSwKICAgICAgICB7NywgMTksIDE0LCAxN30sCiAgICAgICAgezgsIDYsIDcsIDh9LAogICAgICAgIHs5LCAzLCAxLCAyfSwKICAgICAgICB7MTAsIDIwLCAxOCwgMTZ9CiAgICB9OwoKICAgIC8vIFNhbXBsZSBiZXQKICAgIHN0cnVjdCByYWNlV2FnZXIgbXlCZXQgPSB7MywgMjAsIDUwLCAnVyd9OwoKICAgIC8vIENhbGN1bGF0ZSB3aW5uaW5ncwogICAgZmxvYXQgd2lubmluZ3MgPSBteUhvcnNlQmV0KG15QmV0LCB0aGVEYWlseVJlc3VsdHMpOwogICAgaWYgKHdpbm5pbmdzID4gMCkgewogICAgICAgIHByaW50ZigiWW91IHdvbiAkJS4yZiFcbiIsIHdpbm5pbmdzKTsKICAgIH0gZWxzZSB7CiAgICAgICAgcHJpbnRmKCJTb3JyeSwgeW91IGxvc3QgeW91ciBiZXQuXG4iKTsKICAgIH0KCiAgICByZXR1cm4gMDsKfQ==