In: Computer Science
Problem: Charity Gala (gala.c)
Many charities support good causes, but one of the difficulties each of them has is organizing their fundraising events. You've decided that you'd like to donate your skills to create a program that organizes the typical activities at a fund-raising gala. In particular, your program will help manage the following:
1) Ticket sales
2) Silent Auction
3) Raffle
Your program will log the number of tickets sold both in advance and at the event, a silent auction for donated items, and a raffle for other donated items.
Program Details
Ticket Sales Details
You will sell tickets in advance and at the door. Prices for buying in advance and at the door will be given. Also, the total number of tickets sold in advance will be given. Each guest will have a unique number. If there are n tickets sold in advance, then these guests will be numbered 0 through n-1. As the event starts, requests to buy tickets at the door may be made and these guests will be numbered sequentially, starting at the lowest unassigned number. The maximum number of guests will be 1000.
Silent Auction Details
The silent auction will have up to 1000 items. At any time, users can bid on any item, so long as the new bid exceeds the previous bid. At the point in time when the auction is closed, the items with at least one bid are given to the users who have placed the highest bid on that item. Your program must always keep track of all of the best bids on items so that no matter when the auction closes, you'll have all the data for who has won all of the items. Any item without a bid goes to no one.
Raffle Details
There will be a given number for the total number of raffle tickets sold, that will be 1000 or fewer. Guests can buy raffle tickets for a set price of $2.00. There will be a number of prizes awarded from the raffle (not to exceed 100) after the raffle has finished. For each raffle ticket, you'll have to keep track of which guest has it. The raffle tickets will be numbered starting at 0 through the number of tickets minus one. After the raffle finishes, you will be given the raffle numbers pulled for the winners of each prize. You will be guaranteed that these numbers correspond to numbers that were previously handed out during the raffle. You must determine who wins each prize.
Information for each event is provided from a file. You will take in input from the file and produce output for each event that occurs, in order.
A skeleton of the solution for this assignment is posted on the webcourse. You must fill in the functions that are currently empty. After you write each function, you should test it before moving on. The main function should not be modified for the final submission (you may modify it during testing, as long as you return it to its initial form).
Descriptions of each function are given in the skeleton along with the function Pre- and Post-conditions. The output sample on the webcourse shows the wording you should use and how the program should run when completed. Points are allotted for following the precise wording shown.
Input Specification
The first line of the file contains the following three values, separated by spaces:
Cost of the presales tickets (in dollars), Cost of the tickets at the door (in dollars), and the number of presale tickets. The first two values will be positive real numbers to two decimal places and the last will be a positive integer.
The second line of the file will contain one positive integer representing the number of auction items.
The third line of the file will contain the two following positive integers pertaining to the raffle: the number of raffle tickets available and the number of raffle prizes.
The fourth line of the file will contain a single positive integer, numEvents, representing the number of events that occur at the fundraising event. These events are split into two groups: actions by guests at the ball and awards given (raffle, auction, person, totalrevenue). All of the actions precede all of the awards.
The following numEvents lines will contain information about each event that occurs, with one event or award described for each line.
You will produce exactly one line of output for each event described. Here are the formats of each event that could occur:
If a patron buys a ticket at the door, a command will be on a line by itself:
BUY TICKET k
where k is a positive integer indicating the number of tickets bought at the door. These guests will be numbered as previously mentioned. You are guaranteed that the total number of tickets bought, including presales, will not exceed 1000.
If a patron makes a bid in the silent auction, a command of the following form will be issued:
BIDITEM k p d
where k represents the item number for which the bid is being placed, p represents the number of the person placing the bid, and d represents the dollar amount the person has bid. This last value is a positive real number to two decimal places while the others are non-negative integers within the appropriate ranges.
The following command closes the auction. Any bids made after this command is executed are ignored.
CLOSEAUCTION
If a guest desires to buy raffle tickets, a command with the following format will be used:
BUY RAFFLE k p
This command lets person p buy exactly k raffle tickets. The raffle tickets are numbered as previously described, so long as the tickets are still available. If they aren't available, the person gets whatever tickets remain.
The last several commands in the file will deal with awarding prizes. These commands will take the following forms.
A query about a raffle item will be of the following form:
AWARD RAFFLE i t
where i is the raffle item being given out and t is the number of the winning ticket of that raffle item. Your program will have to respond with the winner of this item. You will be guaranteed that this query will appear EXACTLY once for each raffle prize and that all queries of this form will appear AFTER the last raffle ticket is sold. Furthermore, you are guaranteed that the raffle ticket number was purchased by an individual and that the raffle prize number is valid. Finally, a single raffle ticket will correspond to at most one raffle prize.
A status query about a silent auction item will be of the following form:
AWARD AUCTION i
where i represents the silent auction item being queried. Your program will have to respond with the winner of this item and the amount they paid for it OR that there were no bids for that item.
The final status query in the file will be the following line:
TOTAL REVENUE
Output Specification
For the input command of the form:
BUY TICKET k
output a single line of the form:
SOLD TICKETS a - b
where a is the starting number of the tickets sold and b is the ending number (inclusive) of the tickets sold. If k is 1, then a and b will be equal. (Note: For the very first command of this type, the value of a will equal the number of presold tickets, since the presale tickets are numbered from 0 to the total number of presale tickets minus 1.)
For the input command of the form:
BIDITEM k p d
output a single line with one of the following forms:
BIDITEM k ACCEPTED for PERSON p at d DOLLARS
BIDITEM k REJECTED for PERSON p at d DOLLARS
The two reasons to reject a bid are if the new bid is not higher than the previous bid OR if the auction has been closed already.
For the CLOSEAUCTION command, simply print a single line with the exact same output:
CLOSEAUCTION
For the input command
BUY RAFFLE k p
output a single line of one of the two following forms:
RAFFLE TICKETS a - b given to PERSON p
NO RAFFLE TICKETS given to PERSON p
where a represents the first raffle ticket number issued and b represents the last raffle ticket number issued to person p. If there are no more raffle tickets left with the command is issued, the latter format is used. Note that the former format may be used if the person asks for a certain number of raffle tickets, but gets fewer since she bought all that were left.
For the input command
AWARD RAFFLE i t
output a single line of the following format:
RAFFLE i WON BY PERSON p
where i is the raffle item in question (in the query) and p is the number of the guest who won the item.
For the input command
AWARD AUCTION i
output a single line of the following format:
AUCTION ITEM i WON BY PERSON p for $d.dd
where i is the number of the auction item, p is the person who won the item and d.dd is the number of dollars he paid for it, to two decimal places. Or, if there are no bids for the item, output a single line of the following format:
NO BIDS FOR AUCTION ITEM %d
For the input command
TOTALREVENUE
output a single line with the following format:
TOTALREVENUE is $d.dd
where d.dd is the total amount gained by the event via ticket sales, auction sales, and raffle ticket sales. (We are assuming that all items were donated so that there was no cost associated with the event.
/*Header Comment
*/
#include <stdio.h>
#include <string.h>
//Constants to be used.
//MAXSIZE is the maximum size of all strings
//MAXAUCTIONITEMS is the maximum number of items in the
auction
//MAXRAFFLE is the maximum number of available raffle tickets
#define MAXSIZE 100
#define MAXAUCTIONITEMS 1000
#define MAXRAFFLE 1000
//Function prototypes - do not change these
void initRaffle(int raffles[MAXRAFFLE]);
void initAuction(float auction[2][MAXAUCTIONITEMS]);
void buyTickets(float * totalRevenue, int * ticketsSold, int
numTickets, float price);
void buyRaffle(float * totalRevenue, int raffles[MAXRAFFLE], int
availTickets, int numTickets, int person);
void bid(float auction[2][MAXAUCTIONITEMS], float bid, int
auctionNumber, int person, int flag);
float awardAuction(float auction[2][MAXAUCTIONITEMS], int
auctionNumber);
void awardRaffle(int raffles[MAXRAFFLE], int raffleNumber, int
winner);
//Main function
int main() {
FILE * ifp;
char filename[MAXSIZE], event[MAXSIZE], item[MAXSIZE];
float presale, dayOf, totalRevenue = 0;
float auctions[2][MAXAUCTIONITEMS];
int raffles[MAXRAFFLE];
int numPresale, numAuctions, numRaffle, numPrizes, numEvents;
int i, ticketsSold = 0, auctionFlag = 1;
printf("Please enter the input file name.\n");
scanf("%s", filename);
ifp = fopen(filename, "r");
fscanf(ifp, "%f%f%d", &presale, &dayOf, &numPresale);
totalRevenue += numPresale * presale;
ticketsSold = numPresale;
fscanf(ifp, "%d", &numAuctions);
fscanf(ifp, "%d%d", &numRaffle, &numPrizes);
fscanf(ifp, "%d", &numEvents);
initRaffle(raffles);
initAuction(auctions);
for (i=0; i<numEvents; i++) {
fscanf(ifp, "%s", event);
if (strcmp(event, "BUY") == 0) {
fscanf(ifp, "%s", item);
if (strcmp(item, "TICKET") == 0) {
int numTickets;
fscanf(ifp, "%d", &numTickets);
buyTickets(&totalRevenue, &ticketsSold, numTickets,
dayOf);
}
else if (strcmp(item, "RAFFLE") == 0){
int numTickets, person;
fscanf(ifp, "%d%d", &numTickets, &person);
buyRaffle(&totalRevenue, raffles, numRaffle, numTickets,
person);
}
}
else if (strcmp(event, "BIDITEM") == 0) {
int itemNumber, person;
float amount;
fscanf(ifp, "%d%d%f", &itemNumber, &person,
&amount);
bid(auctions, amount, itemNumber, person, auctionFlag);
}
else if (strcmp(event, "CLOSEAUCTION") == 0) {
printf("CLOSE AUCTION.\n");
auctionFlag = 0;
}
else if (strcmp(event, "AWARD") == 0) {
fscanf(ifp, "%s", item);
if (strcmp(item, "AUCTION") == 0) {
int auctionNumber;
fscanf(ifp, "%d", &auctionNumber);
totalRevenue += awardAuction(auctions, auctionNumber);
}
else if (strcmp(item, "RAFFLE") == 0){
int raffleNumber, winner;
fscanf(ifp, "%d%d", &raffleNumber, &winner);
awardRaffle(raffles, raffleNumber, winner);
}
}
else {
printf("TOTALREVENUE is $%.2lf.\n", totalRevenue);
}
}
fclose(ifp);
return 0;
}
void initRaffle(int raffles[MAXRAFFLE]) {
}
void initAuction(float auction[2][MAXAUCTIONITEMS]) {
}
void buyTickets(float * totalRevenue, int * ticketsSold, int
numTickets, float price) {
}
void buyRaffle(float * totalRevenue, int raffles[MAXRAFFLE], int
availTickets, int numTickets, int person) {}// Pre-conditions:
auction is an 2D array that holds the current highest bid and the
person with
void bid(float auction[2][MAXAUCTIONITEMS], float bid, int
auctionNumber, int person, int flag) {
}
float awardAuction(float auction[2][MAXAUCTIONITEMS], int
auctionNumber) {}
void awardRaffle(int raffles[MAXRAFFLE], int raffleNumber, int
winner) {}
Summary :
Solution provided with Notes , Code and Sample output .
Notes :
Implemented given functions with comments and outline below brief description.
initRaffle() initAuction() initializes the arrays with -1 for persons and 0 for bid prices so that its easier to check if the slot is already taken or not.
Buy Ticket simply increments the given pointer and revenues by the amount of ticket price multiplied by num of tickets.
BuyRaffle() allocates numTickets to the person by assigning the raffles[] array slots from next avaialble slot which is obtainted by checking -1 .
bid() this checks if the auction[] array is already bid with amount less than current bid and if yes replaces the value with current bid person and amount.
awardAuction() checks if the auctionNumber slot in auction[] has any one bid ( non 0 bid & -1 in person ) and prints the details if not , it prints no bid is made.
awardRaffle() checks for winnner consecutive slots belong to the same person and anounces the awardee .
############################## Code ####################
#include <stdio.h>
#include <string.h>
// set debug to 1 to print debug messages
#define debug 0
//Constants to be used.
//MAXSIZE is the maximum size of all strings
//MAXAUCTIONITEMS is the maximum number of items in the
auction
//MAXRAFFLE is the maximum number of available raffle tickets
#define MAXSIZE 100
#define MAXAUCTIONITEMS 1000
#define MAXRAFFLE 1000
//Function prototypes - do not change these
void initRaffle(int raffles[MAXRAFFLE]);
void initAuction(float auction[2][MAXAUCTIONITEMS]);
void buyTickets(float * totalRevenue, int * ticketsSold, int
numTickets, float price);
void buyRaffle(float * totalRevenue, int raffles[MAXRAFFLE], int
availTickets, int numTickets, int person);
void bid(float auction[2][MAXAUCTIONITEMS], float bid, int
auctionNumber, int person, int flag);
float awardAuction(float auction[2][MAXAUCTIONITEMS], int
auctionNumber);
void awardRaffle(int raffles[MAXRAFFLE], int raffleNumber, int
winner);
//Main function
int main() {
FILE * ifp;
char filename[MAXSIZE], event[MAXSIZE],
item[MAXSIZE];
float presale, dayOf, totalRevenue = 0;
float auctions[2][MAXAUCTIONITEMS];
int raffles[MAXRAFFLE];
int numPresale, numAuctions, numRaffle, numPrizes,
numEvents;
int i, ticketsSold = 0, auctionFlag = 1;
printf("Please enter the input file
name.\n");
scanf("%s", filename);
ifp = fopen(filename, "r");
fscanf(ifp, "%f%f%d", &presale, &dayOf, &numPresale);
totalRevenue += numPresale * presale;
ticketsSold = numPresale;
fscanf(ifp, "%d", &numAuctions);
fscanf(ifp, "%d%d", &numRaffle,
&numPrizes);
fscanf(ifp, "%d", &numEvents);
initRaffle(raffles);
initAuction(auctions);
for (i=0; i<numEvents; i++) {
fscanf(ifp, "%s", event);
if (strcmp(event, "BUY") == 0) {
fscanf(ifp, "%s", item);
if (strcmp(item, "TICKET") == 0)
{
int
numTickets;
fscanf(ifp,
"%d", &numTickets);
buyTickets(&totalRevenue, &ticketsSold, numTickets,
dayOf);
}
else if (strcmp(item, "RAFFLE") ==
0){
int numTickets,
person;
fscanf(ifp,
"%d%d", &numTickets, &person);
buyRaffle(&totalRevenue, raffles, numRaffle, numTickets,
person);
}
}
else if (strcmp(event, "BIDITEM") == 0) {
int itemNumber, person;
float amount;
fscanf(ifp, "%d%d%f",
&itemNumber, &person, &amount);
bid(auctions, amount, itemNumber,
person, auctionFlag);
}
else if (strcmp(event, "CLOSEAUCTION") == 0) {
printf("CLOSE AUCTION.\n");
auctionFlag = 0;
}
else if (strcmp(event, "AWARD") == 0) {
fscanf(ifp, "%s", item);
if (strcmp(item, "AUCTION") == 0)
{
int auctionNumber;
fscanf(ifp, "%d",
&auctionNumber);
totalRevenue +=
awardAuction(auctions, auctionNumber);
}
else if (strcmp(item, "RAFFLE") ==
0){
int raffleNumber, winner;
fscanf(ifp, "%d%d",
&raffleNumber, &winner);
awardRaffle(raffles, raffleNumber,
winner);
}
}
else {
printf("TOTALREVENUE is $%.2lf.\n",
totalRevenue);
}
}
fclose(ifp);
return 0;
}
/* Initialize all raffle entries with -1 */
void initRaffle(int raffles[MAXRAFFLE])
{
for(int i=0; i < MAXRAFFLE ; i++)
raffles[i] = -1;
}
/* Initialize auction[0] ( person numbe ) with -1 and auction[1][]
( bid amount ) with 0 */
void initAuction(float auction[2][MAXAUCTIONITEMS]) {
for( int i=0 ; i < MAXAUCTIONITEMS ; i++ )
{
auction[0][i] = -1 ;
auction[1][i] = 0 ;
}
}
/* Increment total tickets sold by numTickets and add the
related revenues to total Revenues */
void buyTickets(float * totalRevenue, int * ticketsSold, int
numTickets, float price)
{
printf(" SOLD TICKETS %d %d\n", *ticketsSold ,
(*ticketsSold + numTickets));
*ticketsSold += numTickets ;
*totalRevenue += numTickets * price;
}
void buyRaffle(float * totalRevenue, int raffles[MAXRAFFLE], int
availTickets, int numTickets, int person)
{
// Check if the available tickets
int i=0;
for(i =0 ; (i < availTickets) ; i++ )
if ( raffles[i] == -1)
break;
if ( debug ) printf(" I: %d ,AvailableTickets %d ,
numTickets %d , person %d
\n",i,availTickets,numTickets,person);
// check if numTickets requested is < avaialble
tickets
if ( ( i + numTickets ) < availTickets )
{
// assign the person to the raffles
slot
for( int j = i ; ( j < i +
numTickets ) ; j++ )
raffles[j] = person ;
*totalRevenue += numTickets *
2;
printf(" RAFFLE TICKETS %d - %d
given to PERSON %d \n",i, i + numTickets -1, person);
} else {
printf(" NO RAFFLE TICKETS given to
PERSON %d \n",person);
}
}
// Pre-conditions: auction is an 2D array that holds the current
highest bid and the person with
void bid(float auction[2][MAXAUCTIONITEMS], float bid, int
auctionNumber, int person, int flag)
{
// check action not closed
if ( flag )
{
// Check action number is within
MAXAUCTIONITEMS
if ( auctionNumber <
MAXAUCTIONITEMS )
{
// check if the
bid price at auctionNumber is < current bid then update the
person
// else updation
is not required.
if (
auction[1][auctionNumber] < bid )
{
auction[0][auctionNumber] = person;
auction[1][auctionNumber] = bid;
printf(" BIDITEM %d ACCEPTED for PERSON %d at
%.2f DOLLARS \n",auctionNumber,person, bid);
return;
}
}
}
printf(" BIDITEM k REJECTED for PERSON %d at %.2f
DOLLARS \n",person, bid);
}
float awardAuction(float auction[2][MAXAUCTIONITEMS], int
auctionNumber)
{
float price = 0.0;
if(debug) printf(" AWD auction %d person - %.0f %.2f
\n",auctionNumber, auction[0][auctionNumber],
auction[1][auctionNumber]);
// Check if the auctionNumber is within limits
if (( auctionNumber >= 0 ) & ( auctionNumber
< MAXAUCTIONITEMS ))
{
// Check if the actionNumber slot
is contested by bids
if (( auction[1][auctionNumber]
> 0.0 ) & ( auction[0][auctionNumber] > -1 ))
{
price =
auction[1][auctionNumber];
printf(" AUCTION
ITEM %d WON BY PERSON %d for $%.2f \n", auctionNumber,
(int)auction[0][auctionNumber],auction[1][auctionNumber]);
}
else
printf(" NO BIDS
FOR AUCTION ITEM %d \n",auctionNumber);
}
return price;
}
void awardRaffle(int raffles[MAXRAFFLE], int raffleNumber, int
winner) {
int person = -1;
if (debug ) printf(" AWRD RAFFELE %d %d
\n",raffleNumber, winner);
// Check if raffleNumber is within limits
if(( raffleNumber >=0 ) & ( raffleNumber <
MAXRAFFLE) )
{
person =
raffles[raffleNumber];
// Check if the winner consecutive
bids are for same person
for( int i= raffleNumber ; i <
raffleNumber + winner ; i++ )
{
if(debug)
printf(" RAFFLE %d %d " , raffleNumber, raffles[i] );
if ( raffles[i]
!= person )
{
printf(" No RAFFLE winner \n");
return;
}
}
printf(" RAFFLE %d WON BY PERSON %d \n", raffleNumber,
person);
}
return ;
}
#################### Sample Output ################