#include <stdio.h>
#include <string.h>
#include <stdlib.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[])
{
int validRaceNumber = (myBet.raceNumber >= 1 && myBet.raceNumber <= 10);
int validHorseNumber = (myBet.myHorseNumber >= 1 && myBet.myHorseNumber <= 20);
int validBetAmount = (myBet.myBetAmount >= 5 && myBet.myBetAmount <= 1000);
if (!validRaceNumber || !validHorseNumber || !validBetAmount) // Invalid bet
{
return 0;
}
for (int i = 0; i < RACES; i++)
{
if (theDailyResults[i].raceNumber == myBet.raceNumber) // race number needs to match
{
struct raceResult race = theDailyResults[i];
float winnings = 0;
printf("My bet %d %d %f %c\n", myBet.
raceNumber, myBet.
myHorseNumber, myBet.
myBetAmount, myBet.
betType); printf("Race result %d %d %d %d\n", race.
raceNumber, race.
showHorseNumber, race.
placeHorseNumber, race.
winningHorseNumber); if (myBet.betType == 'S') // Show
{
if (myBet.myHorseNumber == race.showHorseNumber || myBet.myHorseNumber == race.placeHorseNumber || myBet.myHorseNumber == race.winningHorseNumber)
{
winnings = (myBet.myBetAmount * 3);
return winnings;
}
}
else if (myBet.betType == 'P') // Place
{
if (myBet.myHorseNumber == race.winningHorseNumber || myBet.myHorseNumber == race.placeHorseNumber)
{
winnings = (myBet.myBetAmount * 5);
return winnings;
}
}
else if (myBet.betType == 'W') // Win
{
if (myBet.myHorseNumber == race.winningHorseNumber)
{
winnings = (myBet.myBetAmount * 10);
return winnings;
}
}
else return 0;
}
}
return 0; // Invalid bet type
}
int main(void) {
struct raceWager myBet = {1, 3, 5};
struct raceResult dailyResults[RACES] = {{2, 1, 2, 3}};
float winnings = myHorseBet(myBet, dailyResults);
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdHJpbmcuaD4KI2luY2x1ZGUgPHN0ZGxpYi5oPgoKI2RlZmluZSBSQUNFUyAxMAoKLy8gcmFjZSByZXN1bHRzCnN0cnVjdCByYWNlUmVzdWx0CnsKICAgIGludCByYWNlTnVtYmVyOyAgICAgICAgICAgLy8gVGhlIHVuaXF1ZSBSYWNlIE51bWJlcgogICAgaW50IHdpbm5pbmdIb3JzZU51bWJlcjsgICAvLyAxc3QgcGxhY2UgaG9yc2UgbnVtYmVyIGZvciB0aGUgcmFjZQogICAgaW50IHBsYWNlSG9yc2VOdW1iZXI7ICAgICAvLyAybmQgcGxhY2UgaG9yc2UgbnVtYmVyIGZvciB0aGUgcmFjZQogICAgaW50IHNob3dIb3JzZU51bWJlcjsgICAgICAvLyAzcmQgcGxhY2UgaG9yc2UgbnVtYmVyIGZvciB0aGUgcmFjZQp9OwoKCi8vIGFzc3VtZSB5b3UgY2FuIGFjY2VzcyB0aGlzIGFycmF5IHRoYXQgY29udGFpbnMgdGhlIHJlc3VsdHMgb2YgaG9yc2UgcmFjZXMgZm9yIGEgZ2l2ZW4gZGF5CnN0cnVjdCByYWNlUmVzdWx0IHRoZURhaWx5UmVzdWx0c1tSQUNFU107CgovLyB5b3VyIHNwZWNpZmljIHJhY2Ugd2FnZXIgIApzdHJ1Y3QgcmFjZVdhZ2VyCnsKICAgIGludCByYWNlTnVtYmVyOyAgICAgIC8vIHRoZSByYWNlIG51bWJlciB3aGVyZSB5b3UgYmV0IHlvdXIgaG9yc2UKICAgIGludCBteUhvcnNlTnVtYmVyOyAgIC8vIHRoZSBob3JzZSB5b3UgYmV0IG9uCiAgICBmbG9hdCBteUJldEFtb3VudDsgICAvLyBob3cgbXVj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gKICAgICAgICB7CiAgICAgICAgICAgIHN0cnVjdCByYWNlUmVzdWx0IHJhY2UgPSB0aGVEYWlseVJlc3VsdHNbaV07CiAgICAgICAgICAgIGZsb2F0IHdpbm5pbmdzID0gMDsKICAgICAgICAgICAgcHJpbnRmKCJNeSBiZXQgJWQgJWQgJWYgJWNcbiIsIG15QmV0LnJhY2VOdW1iZXIsIG15QmV0Lm15SG9yc2VOdW1iZXIsIG15QmV0Lm15QmV0QW1vdW50LCBteUJldC5iZXRUeXBlKTsKICAgICAgICAgICAgcHJpbnRmKCJSYWNlIHJlc3VsdCAlZCAlZCAlZCAlZFxuIiwgcmFjZS5yYWNlTnVtYmVyLCByYWNlLnNob3dIb3JzZU51bWJlciwgcmFjZS5wbGFjZUhvcnNlTnVtYmVyLCByYWNlLndpbm5pbmdIb3JzZU51bWJlcik7CiAgICAgICAgICAgIGlmIChteUJldC5iZXRUeXBlID09ICdTJykgLy8gU2hvdwogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICBpZiAobXlCZXQubXlIb3JzZU51bWJlciA9PSByYWNlLnNob3dIb3JzZU51bWJlciB8fCBteUJldC5teUhvcnNlTnVtYmVyID09IHJhY2UucGxhY2VIb3JzZU51bWJlciB8fCBteUJldC5teUhvcnNlTnVtYmVyID09IHJhY2Uud2lubmluZ0hvcnNlTnVtYmVyKQogICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgIHdpbm5pbmdzID0gKG15QmV0Lm15QmV0QW1vdW50ICogMyk7CiAgICAgICAgICAgICAgICAgICAgcmV0dXJuICB3aW5uaW5nczsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQogICAgICAgICAgICBlbHNlIGlmIChteUJldC5iZXRUeXBlID09ICdQJykgLy8gUGxhY2UKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgaWYgKG15QmV0Lm15SG9yc2VOdW1iZXIgPT0gcmFjZS53aW5uaW5nSG9yc2VOdW1iZXIgfHwgbXlCZXQubXlIb3JzZU51bWJlciA9PSByYWNlLnBsYWNlSG9yc2VOdW1iZXIpCiAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgd2lubmluZ3MgPSAobXlCZXQubXlCZXRBbW91bnQgKiA1KTsKICAgICAgICAgICAgICAgICAgICByZXR1cm4gd2lubmluZ3M7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KICAgICAgICAgICAgZWxzZSBpZiAobXlCZXQuYmV0VHlwZSA9PSAnVycpIC8vIFdpbgogICAgICAgICAgICB7IAogICAgICAgICAgICAgICAgaWYgKG15QmV0Lm15SG9yc2VOdW1iZXIgPT0gcmFjZS53aW5uaW5nSG9yc2VOdW1iZXIpCiAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgd2lubmluZ3MgPSAobXlCZXQubXlCZXRBbW91bnQgKiAxMCk7CiAgICAgICAgICAgICAgICAgICAgcmV0dXJuIHdpbm5pbmdzOwogICAgICAgICAgICAgICAgfSAgCiAgICAgICAgICAgIH0KICAgICAgICAgICAgZWxzZSByZXR1cm4gMDsKICAgICAgICB9CiAgICB9CiAgICByZXR1cm4gMDsgLy8gSW52YWxpZCBiZXQgdHlwZQp9CgppbnQgbWFpbih2b2lkKSB7CglzdHJ1Y3QgcmFjZVdhZ2VyIG15QmV0ID0gezEsIDMsIDV9OwoJc3RydWN0IHJhY2VSZXN1bHQgZGFpbHlSZXN1bHRzW1JBQ0VTXSA9IHt7MiwgMSwgMiwgM319OwoJZmxvYXQgd2lubmluZ3MgPSBteUhvcnNlQmV0KG15QmV0LCBkYWlseVJlc3VsdHMpOwoKCXByaW50ZigiJWZcbiIsIHdpbm5pbmdzKTsKfQ==