Question

Write a C PROGRAM that will simulate rolling a die. When rolling a die, one will...

Write a C PROGRAM that will simulate rolling a die.
When rolling a die, one will get a die face value of 1-6. We can use rand function and math ceiling
function to get a random number of 1-6 generated. The program will simulating rolling 5 dice
simultaneously and store their face values into an array of size 5. The program will keep rolling the dice
and print the following:
1. All face values in one line
2. If any three of the dice have the same value, then add one more print line:
****** THREE OF A KIND *******
3. Print the total count need to get “three of a kind” result.

The program then will reset the count to 0 and start over to simulate the following:
1. All face values in one line
2. If the outcome is three and two, then add one more print line:
****** FULL HOUSE *******
3. Print the total count need to get “full house” result.

The program then will reset the count to 0 and start over to simulate the following:
1. All face values in one line
2. If the outcome is a straight line, then add one more print line:
****** STRAIGHT LINE *******
3. Print the total count need to get “straight line” result.

Requirements:
You will need the following functions:
1. Roll5Die function which will generate 5 random die face value and store them into an array of
size 5
2. getThreeKind function which will roll the 5-die continuously until the result is a “Three of a
Kind”. The function then will return the count to main function.
3. getFullHouse function which will roll the 5-die continuously until the result is a “full house”. The
function then will return the count to main function.
4. getStraightLine function which will roll the 5-die continuously until the result is a “straight line”.
The function then will return the count to main function.
5. All print statement should be done in the main() function. The array of 5 die is declared in main()
function. Then the function just make 3 calls and print the result after each call.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

C Code: commented

#include <stdio.h>
#include <stdlib.h>

void Roll5Die(int *arr)
{
    for (int i = 0; i < 5; i++)
    {
        arr[i] = rand() % 6 + 1; // giving random numbers to the dice
    }
}
int getThreeKind(int *arr)
{
    int count = 0;
    int p = 0;
    while (1)
    {
        Roll5Die(arr);
        for (int i = 0; i < 5; i++) // checking triplets in the array
        {
            for (int j = i + 1; j < 5; j++)
            {
                for (int k = j + 1; k < 5; k++)
                {
                    for (int l = 0; l < 5; l++)
                    {
                        printf("%d ", arr[l]);
                    }
                    printf("\n");
                    if (arr[i] == arr[j] && arr[j] == arr[k]) // if three are the same
                    {
                        count++;
                        p = 1;
                        printf("****** THREE OF A KIND ******");
                        break;
                    }
                    count++;
                }
                if (p) // to break out from the multiple loops
                    break;
            }
            if (p)
                break;
        }
        if (p)
            break;
    }
    return count;
}

int getFullHouse(int *arr)
{
    int count = 0;
    int p = 0;
    while (1)
    {
        Roll5Die(arr);
        for (int i = 0; i < 5; i++) // checking pairs of the array
        {
            for (int j = i + 1; j < 5; j++)
            {
                for (int k = 0; k < 5; k++)
                {
                    printf("%d ", arr[k]);
                }
                printf("\n");
                if (arr[i] == 3 && arr[j] == 2) // if condition satisfied
                {
                    count++;
                    p = 1;
                    printf("****** FULL HOUSE ******");
                    break;
                }
                count++;
            }
            if (p) // to break out of the multiple loops
                break;
        }
        if (p)
            break;
    }
    return count;
}

int getStraightLine(int * arr)
{
    int count = 0;
    int p = 0;
    while(1)
    {
        Roll5Die(arr);
        for(int i = 0; i < 5; i++)
        {
            printf("%d ", arr[i]);
        }
        printf("\n");
        for(int i = 0; i < 5 - 1; i++)
        {
            if(arr[i + 1] - arr[i]) // to check if all data is contiguous
            {
                continue;
            }
            else
            {
                p = 1;
                break;
            }
        }
        count++;
        if(p == 0)
        {
            printf("****** STRAIGHT LINE ******");
            return count;
        }
    }
}
int main()
{
    int arr[5];
    int count;
    //calling the three functions and printing after each
    count = getThreeKind(arr);
    printf("Number of counts for a Three Kind -- %d\n", count);
    count = getFullHouse(arr);
    printf("Number of counts for a Full House -- %d\n", count);
    count = getStraightLine(arr);
    printf("Number of counts for a Straight Line -- %d\n", count);
    return 0;
}

Output:

Run it on your PC, as the output tends to get very large, I can't post it here

Add a comment
Know the answer?
Add Answer to:
Write a C PROGRAM that will simulate rolling a die. When rolling a die, one will...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • in C please, Write the code to simulate rolling three dice and find the sum of...

    in C please, Write the code to simulate rolling three dice and find the sum of the top three faces. Repeat rolling the three dice 10,000 times. Use an array called frequency to have the count of each sum. Pass this array to a function that calculates the maximum number that appears in this array. Print this maximum in main.

  • C+++ Program Part A Create a class called die (the singular of dice). The die class...

    C+++ Program Part A Create a class called die (the singular of dice). The die class should have a single data member called value. The die constructor should initialize the die’s value to 1. Create a member function called roll() that gives the die a random value between 1 and 6. Create a member function called show() that displays value. In main, demonstrate the creation of a die object to validate it is working. Part B Revise main to count...

  • PYTHON: Dice Rolling Write a script that takes input for the number of dice to roll...

    PYTHON: Dice Rolling Write a script that takes input for the number of dice to roll and for the number of sides for the dice. Roll that many n-sided dice and store the values an array. Take the number of sides of the dice and the number of dice as typed inputs from the user. Output should be a comma separated list of the die numbers with no spaces like this: 6,4,1,3,1 Write a script that will simulate the roll...

  • please do it in C++ Write a program that simulates the rolling of two dice. The...

    please do it in C++ Write a program that simulates the rolling of two dice. The program should use rand to roll the first die and should use rand again to roll the second die. The sum of the two values should then be calculated. [Note: Each die can show an integer value from 1 to 6, so the sum of the two values will vary from 2 to 12, with 7 being the most frequent sum and 2and 12...

  • ( Java Programming ) Write an application to simulate the rolling of two dice. The application...

    ( Java Programming ) Write an application to simulate the rolling of two dice. The application should use an object of class Random once to roll the first die and again to roll the second die. The sum of the two values should then be calculated. Each die can show an integer value from 1 to 6, so the sum of the values will vary from 2 to 12, with 7 being the most frequent sum, and 2 and 12...

  • Write a java program that simulates the rolling of four dice. The program should use random...

    Write a java program that simulates the rolling of four dice. The program should use random number generator to roll dice. The sum of the four values should then be calculated. Note that each die can show an integer value from 1 to 6, so the sum of the four values will vary from 4 to 24, with 14 being the most frequent sum and 4 and 24 being the least frequent sums. Your program should roll the dice 12,960...

  • Write a program to simulate rolling three dices simultaneously. Keep rolling these three dices until the...

    Write a program to simulate rolling three dices simultaneously. Keep rolling these three dices until the three dices show the values of “1”, “2”, and “3” (we call this condition 1. The dices are in no particular orders to show these values in this case). Then the program shows the values of the three dices and the number of rolling so far. After that the rolling continues (and counting of the number of rolling continues too) until the first dice...

  • Linear Congruence Generator Below is a generator desigred to simulate rolling a six sided de (d6)....

    Linear Congruence Generator Below is a generator desigred to simulate rolling a six sided de (d6). Note: the +1 is to adjust the outcome so you can get a roll of 6 and and do not get o roll of roll, = [(13. rollm-L 4)mod 6 11 5. Using the formula above, determine the outcome of the first 10 rolls in the sequence if the first roll is 3 Roll Number Die Result 6. Come up with a recurrence relation...

  • Write a Python (version 3.5.2) program that simulates how often certain hands appear in Yahtzee. In...

    Write a Python (version 3.5.2) program that simulates how often certain hands appear in Yahtzee. In Yahtzee, you roll five dice and then use those dice to make certain combinations. For example, the Yahtzee combination is formed by all five dice having the same value. A large straight occurs when all of the dice are in consecutive order (e.g., 1,2,3,4,5 or 2,3,4,5,6). If you haven’t played Yahtzee, you can find out more from various online sources such as Wikipedia. Once...

  • need help with dice excercise For example, if we were writing a calendar program of some...

    need help with dice excercise For example, if we were writing a calendar program of some sort, we might very well use an array with 366 spots to hold the days, not worrying about adding more days to the year. Exercise 23 (C level] For another example, suppose we want to simulate rolling two six-sided dice repeatedly, tallying how many times each total is rolled. We need a memory cell to count the number of times each of the possible...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT