Assignment Title: Fun with arrays.
Note: Submit as one file, HW6.c
You're about to play Briefcase or No Briefcase! It's a little bit like a popular game show that will remain nameless. Here's how to play:
You are given ten briefcases (0,1,2 ...9), each with a dollar amount from $1 to $1,000,000,000 in multiples of ten. They are randomly placed in an array of size ten by a function we provided called popCases() (see appendix). After they are randomly assigned to these multiples of ten, the user is shown all the values remaining and the case numbers remaining (10 cases with 10 values). Next the user will enter a number and that case number is to be emptied and the value it used to hold should be shown to the user. This will continue until the user is down to one case, which is the one they win.
The user will be notified after each turn, the case numbers of unopened cases, and the values that remain in play (that haven’t been “emptied”). This is also provided to you in the printCases() function (see appendix), but you need to create the function that displays the case numbers still available. You may play our version of it using the sample file on ICON. There should be a Mac, Windows and Linux version so everyone will be able to unzip the file and play with it to get a feel for how the program should work. It may even be useful to run yours and the demo side-by-side to make sure it works in a similar way. Just remember that because the briefcases are randomly populated, you probably won't get the same outcome as the program if you run the two side-by-side.
A few notes about the requirements of the game:
1) After a briefcase is emptied, the program should set that value in the case to zero
2) After a briefcase is emptied, the user should not be able to access that value and the program should go on to the next turn after displaying a friendly error message. The same goes for a suitcase number that doesn't exist (for example -4 or 99, since there are only 10 suitcases)
3) After each round, right after the current values in the cases are shown using the printCases() function, the program should use a function to print the case numbers available. Hint: since you want to set the emptied cases to zero, just don't print off the cases that have a value of zero.
4) Functions are to be used extensively, and you should only need a single array of size ten. (Use #define SIZE 10 after your #include). Also, when iterating through the array, use SIZE instead of the number 10 as the upper bounds. See the way the functions provided go through the array for clarification.
5) Be careful about the indexes of the case. When the user enters a case number or case index of four, you want to access the array index of three. This should be pretty simple and should just involve passing a number, say casenum, to a function and when accessing array[index], use array[casenum-1].
6) Your main program should have no more than ten lines of code.
7) Do NOT change the code inside the functions provided.
These are the functions that you must create:
1) void playGame():
This should be used in the beginning to start the game
2) void printCaseIndex():
This should all of the available (nonzero) case indexes (Note: 1 to 10, not 0 to 9).
3) void printWinner():
This should be called when there is only one case left (Note: use numCasesLeft function) and should print the only remaining (nonzero) case's value
4) int numCasesLeft():
This should be called with every turn, and should return the number of cases left in play (I.e. nonzero values)
5) void checkCase():
This should check the case of the index it is passed (Note if passed an index of 1, that really means an array index of 0) and should do nothing but print an error to the user that they either entered a wrong choice or a case that they already tried, and the game should move on to the next round. If passed a valid index the function should print the value of the case chosen and “clear” (set to zero) that case.
Grading Guide:
Changing the functions given at all -- -100 (not a typo)
Not creating one of the functions required -- -20 per function
Functions not returning stated values -- -25
Functions not doing what was asked -- -20
Not including the necessary prototypes -- -20
Not printing out the case numbers in play after each turn -- -20
Not printing out the values left in the cases (we gave you a function that will do this) -- -20
Not adhering to one of the rules of the game -- -20
Having more than ten lines of code in the main -- -10
Setting "cleared" cases to a number other than zero -- -15
Not checking for invalid entries, allowing for invalid access of an array -- -20
#include<stdio.h>
#define SIZE 10
/*Here is the clearCases function: it is to be used once at the beginning of the program to initialize all of the briefcases to zero.*/
void clearCases(int cases[SIZE]) {
int index;
for(index=0;index<SIZE;index++){
cases[index]=0;
}
}
/*Here is the popCases function: If it is passed a cleared array, it will randomly populate it with dollar values from $1 to $1,000,000,000
Note: Be sure to #include stdlib.h and time.h*/
void popCases(int cases[SIZE]) {
int index,dollars;
srand( time(NULL) );
dollars=1;
while(!isFull(cases) ) {
index=( rand() % 10 );
if (!isTaken(cases,index) ) {
cases[index]=dollars;
dollars*=10;
}
}
}
/*Here is the printCases function: If you pass it an array it will show the values of the array in order from smallest to greatest.*/
void printCases(int cases[SIZE]) {
int index1,index2;
int print[SIZE]={1,10,100,1000,10000,100000,1000000,10000000,100000000,1000000000};
printf("-------------------------\nThese values are left!\n");
for(index1=0;index1<SIZE;index1++){
for(index2=0;index2<SIZE;index2++){
if (print[index1]==cases[index2]) {
printf("[$%d]\n",print[index1]);
}
}
}
printf("-------------------------\n");
}
/*This is the isTaken function, it checks to see if the given index of the array is already taken. It is used when populating the briefcases. If the suitcase of the index passed into the function is taken, it returns 1. If it is not taken it returns 0.*/
int isTaken(int cases[SIZE], int index) {
int var;
if (cases[index] != 0 ) {
var = 1;
}
else {
var = 0;
}
return var;
}
/*This is the isFull function: it checks to see if all of the briefcases are full. If they are, the integer 1 is returned, if they are not the integer 0 is returned.*/
int isFull(int cases[SIZE]) {
int index;
int var = 1;
for(index=0;index<SIZE;index++) {
if (cases[index]==0) {
var = cases[index];
}
}
return var;
}