Powerball Program

I'm not sure how to use pointers and arrays!
I need help with all the functions as well... please help a.s.a.p!


You will implement a program that simulates “Powerball”.

The program must match this specification.

Please read this specification carefully and thoroughly. Please ask questions if you are not sure what it means.

Use only pointer notation to access array elements throughout your code – i.e. you may not use [ ] brackets anywhere except to declare the two arrays. You may use a global constant for the number of items in the arrays. You may not use any global variables. (25 points)

main( ) will: (25 points)
□ Declare two arrays, one will hold the player’s number picks, the other will hold the winning Powerball numbers.
□ Call each of the functions specified below to do the following 5 steps.
o Display instructions.
o Get the player’s number selections.
o Generate the winning Powerball numbers.
o Determine if the player has won, and, if so, which prize.
o Display the result (whether or not the player has won, the prize, and the odds of that win.)
□ Display a running total of the player’s winnings.
The player spends $2 to play each time, so you must subtract that from any winnings.
□ Keep running in a loop until the player decides to quit.

Declare, implement and call the following functions exactly as specified below.

void DisplayInstructions ( ); (10 points)
DisplayInstructions will display a description of the game, how to play, and how to win.

void GetPlayerSelections (int *); (20 points)
GetPlayerSelections is passed an array. It will ask the player for input.
Player selects 5 numbers from 1-59, and 1 number from 1-35 for the last number.
Do not allow the player to enter duplicate numbers for the first 5 numbers. (Keep looping until they have selected 6 acceptable numbers.)
Store the player’s selections in the array.

void GeneratePowerball (int *); (25 points)
GeneratePowerball is passed an array.
It randomly generates the 6 numbers according to the rules above and fills the array with these numbers.
Make sure there are no duplicates in the first 5 numbers.

int CheckForWin (int *, int *); (40 points)
CheckForWin is passed the two arrays.
It displays the Player’s Selections and the Powerball Numbers.
It returns an integer 1-9 that represents one of the winning combinations or returns 0 for no win.
Remember that the numbers do not have to be in order to win.

int DisplayPrize (int ); (15 points)
DisplayPrize takes an integer from 0-9 and displays whether it’s a win or a loss, what the prize is and what the odds are of winning that prize.
It returns the amount won (0 if no win).
Pointers are declared like this:

int *name;//it can be int/char/long int/uint...anything, but the amount it can store depends on the type of pointer it is

Once you declare a pointer in this manner, it is not pointing at anything useful. It is pointing to some random place in memory. So for you to make use of a pointer especially with an array, there are a couple ways I know how. First is the way I had started with but you have to give it something to point to.

int array[3] = {'A', 'B', 'C'};//An array with 0,1,2 spaces to store things in and the 3rd spot is to store the '\0' (NULL) value.

Now to make use of our pointer:
*name = array;//Now the pointer is pointing to the first value in our array array[0] (A)

Pointers declared this way are always pointing to the first value in the array unless otherwise specified i.e.
*name = array[2];//Now it will be pointing to the value 'C'

We can do useful things with this pointer now that it is pointing at something.
for (int y = 0; y < 3; y++, name++)
cout>> *name >> " "
//Now this will go ahead and print all the values in our array (A B C)
//You will notice that I did something like this: name++
//This is another beauty of pointers is that you can move them around to point
// to different things including other arrays or values! So specifying name++
// means to move the pointer to the next value in the array 

A second way to use pointers will be to have a dynamically allocated array which is I think what you will most likely need. So instead of creating a pointer and an array, we can have both in one. Observe:

int *arr = new int[Enter_a_size_here];

And to access it, you can access it like a normal array or like a pointer, whichever you choose.
So we can do:
arr[0] = 5;


*arr = 5;//This means the same thing as the above

The first function you have to declare is quite easy. You are just required to output some instructions to the user at the start of the game. cout, printf, puts, etc will work for that.

void GetPlayerSelections (int *);
This function is being passed a pointer to an array. Declaring a dynamically allocated array in your main() can be useful because you can easily access it's elements in another function.

void GeneratePowerball (int *);
Same thing applies from above. So you need another array to be passed to the function where it will place randomly generated numbers into the array.

int CheckForWin (int *, int *);
This will go through both arrays and check that array[n] = array2[n]. If it finds no such match, it will return 0

int DisplayPrize (int );
This is being passed a number, I persume from the function before it and it is to display a prize depending on what value it was passed. The odds can be done with simple math.

This will all be run in one while-loop until the player has spent all of his tokens
Hope that helps, gl
Last edited on
Topic archived. No new replies allowed.