Challenge 64

Description

The purpose of this challenge is to use various arrays. This challenge simulates a lottery game.

The Game

This lottery game asks the player to pick random numbers between 1 and 20. The player must also pick 3 different letters.

The winner will win a payout according to the following:

  1. 0 matching numbers : $0.00
  2. 1 matching number: $5.00
  3. 2 matching numbers: $75.00
  4. 3 matching numbers: $500.00

The amount won above will be multiplied as below based on how many different letters are matched:

  1. Multiply by 1 if no matching letters
  2. Multiply by 2 if 1 matching letter
  3. Multiply by 5 if 2 matching letters
  4. Multiply by 10 if 3 matching letters. If the letters match in exactly the right order, multiply by 100 instead. Example, If the winning letters are abc, the payout is multiplied by 100 if the user guesses abc, but only by 10 if the user guesses cab

Requirements

  1. Start by copying the sample main below. The comments in main() will guide you in what functions will be called and in what order
  2. Write a function bool in_array(int numbers[], int size, int num). This function will return true if num is found in the numbers array. Otherwise, it will return false. The size parameters represents how many elements are in the numbers array.
  3. Write a function bool in_array(char letters[], char c). This function will return true if c is found in the letters array. Otherwise, it will return false. Notice that this is an overload of the other in_array() function above.
  4. Write a function void generate_combination(int num_max, int numbers[], char letters[]). This function will generate random values for the numbers array and the letters array. The num_max variable represents the largest number that can be randomly created.
    void generate_combination(int num_max, int numbers[], char letters[])
    {
        // write a loop to fill up the numbers array with random 
        // numbers. Make sure that each number that is added is unique
        // and is not already in the numbers array (Use the in_array
        // function to check if a number exists in the array)
    
        int num;
    
        // this generates a random number. Use it in a loop. 
        num = rand() % num_max;
    
    
        // Similarly, fill the letters array with random letters. 
        // Each letter must be unique and not already in the array
        // Call the overloaded in_array() function to check if a 
        // letter already exists in the letter array
    
        char c; 
    
        // this generates a random letter. Use it in a loop
        c = char((rand() % 26) + 97);
    }
    
    
  5. Write a function void clear(int numbers[], int size). This function will set all elements of the numbers array to 0. size represents how many elements are in the array.
  6. Write a function void make_lower(char letters[]). This will convert each element of the letters array to lowercase. use the tolower() function to convert any letter to its lower case equivalent.
  7. Write a function void show(int numbers[], char letters[]). This function will display each element of numbers on the same line, separated by spaces. It will then be followed by the letters. See Sample Interaction.
  8. Write a function int count_matches(int numbers[], int userpicks[]). This will check all elements of userpicks against the numbers array. The function returns how many matching numbers were found. HINT: Nested loops.
  9. Write a function int count_matches(char letters[], char userpicks[]). This will check all elements of userpicks against the letters array. The function returns how many matching letters were found. HINT: Nested loops.

Sample Interaction / Output

NOTE: The samples below use a NUM_MAX value of 55 (larger numbers generated randomly)

Run (no matches)

42 31 10 zts
Enter character guess: abc
Enter 3 numbers: 1 2 3
Matching numbers: 0
Matching letters: 0
Payout: $0

Run it again (one correct number)

38 7 9 wvs
Enter character guess: mon
Enter 3 numbers: 9 12 15
Matching numbers: 1
Matching letters: 0
Payout: $5

Run it again (3 numbers, no multiplier)

44 14 27 ajy
Enter character guess: prs
Enter 3 numbers: 44 27 14
Matching numbers: 3
Matching letters: 0
Payout: $500

Run it again (3 correct numbers, 1 multiplier)

14 34 10 rec
Enter character guess: xvr
Enter 3 numbers: 10 34 14
Matching numbers: 3
Matching letters: 1
Payout: $1000

Run it again (3 correct numbers, 3 correct letters in the wrong order)

4 50 13 khn
Enter character guess: knh
Enter 3 numbers: 4 13 50
Matching numbers: 3
Matching letters: 3
Payout: $5000

Run it again (3 correct number, letters in exactly same order, maximum payout!)

13 46 25 crv
Enter character guess: crv
Enter 3 numbers: 25 46 13
Matching numbers: 3
Matching letters: 3
Payout: $50000

Sample main()

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cstring>

using namespace std;

int main()
{
    srand(time(NULL));

    const int NUM_MAX = 20;
    int winning_numbers[3];
    char winning_letters[4] = "";

    double payout[4] = { 0, 5.0, 75.0, 500 };
    double multiplier[4] = { 1, 2, 5, 10 };

    // the user's guesses will use these
    int usernumbers[3];
    char userletters[4];

    // call clear function to clear winning_numbers
    // TODO

    // call generate_combination to create winning combo
    // 20 represents the maximum number used for generating
    // random numbers. 
    generate_combination(NUM_MAX, winning_numbers, winning_letters); 

    // uncomment the following line below during testing
    // this will display the winning numbers before you
    // enter your picks. If you were to actually guess the 
    // winning numbers and letters, you may never actually hit them
    // show(winning_numbers, winning_letters);

    // Ask the user to enter their letter guesses. THe player's letters
    // will fill up the userletters array
    // TODO
    // call make_lower immediately afterwards to lowercase their entry
    // TODO

    // Ask the user to enter their number guesses. The player's numbers
    // will fill up the usernumbers array
    // TODO

    // call count_matches for the winning_numbers
    // and for winning_letters. Remember that count_matches 
    // function is overloaded
    // TODO

    // display the payout information
    // remember to account for the multipliers
    // TODO
    return 0;
}

LEGEND
PROGRAM OUTPUT
USER INPUT
FROM INPUT

CATALOG ID: CPP-CHAL0064

Print Requirements