Complete and full answer in order to give credit Do not copy

Complete and full answer in order to give credit. Do not copy-paste a similar problem.Program is to be written in C (just plain C) Thank you.

Write algorithms and programs to simulate a deck of cards and deal hands of cards. Conventional deck of cards has 52 cards: 13 ranks and 4 suits. Your simulation should be able to create a deck of cards, display the original deck, shuffle the deck, display the shuffled deck, deal the specified number of cards to the specified number of players and display each of the hands of cards.

Output: Display the original deck, display the shuffled deck, and display each of the hands of cards. Decks, hands and cards should be clean, clear and appropriately labeled - do not simply list as a simple column or row of values.

Input: Accept input via the command-line arguments. Validate command-line input. Input will specify the # of cards/hand and the # of hands (players), in that order.

Solution

// C Program to shuffle a given array

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

// A utility function to swap to integers

void swap (int *a, int *b)

{

    int temp = *a;

    *a = *b;

    *b = temp;

}

// A utility function to print an array

void printArray (int arr[], int n)

{

    for (int i = 0; i < n; i++)

        printf(\"%d \", arr[i]);

    printf(\"\ \");

}

// A function to generate a random permutation of arr[]

void randomize ( int arr[], int n )

{

    // Use a different seed value so that we don\'t get same

    // result each time we run this program

    srand ( time(NULL) );

    // Start from the last element and swap one by one. We don\'t

    // need to run for the first element that\'s why i > 0

    for (int i = n-1; i > 0; i--)

    {

        // Pick a random index from 0 to i

        int j = rand() % (i+1);

        // Swap arr[i] with the element at random index

        swap(&arr[i], &arr[j]);

    }

}

// Driver program to test above function.

int main()

{

    int arr[] = {1, 2, 3, 4, 5, 6, 7, 8};

    int n = sizeof(arr)/ sizeof(arr[0]);

    randomize (arr, n);

    printArray(arr, n);

    return 0;

}

// C Program to shuffle a given array

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

// A utility function to swap to integers

void swap (int *a, int *b)

{

    int temp = *a;

    *a = *b;

    *b = temp;

}

// A utility function to print an array

void printArray (int arr[], int n)

{

    for (int i = 0; i < n; i++)

        printf(\"%d \", arr[i]);

    printf(\"\ \");

}

// A function to generate a random permutation of arr[]

void randomize ( int arr[], int n )

{

    // Use a different seed value so that we don\'t get same

    // result each time we run this program

    srand ( time(NULL) );

    // Start from the last element and swap one by one. We don\'t

    // need to run for the first element that\'s why i > 0

    for (int i = n-1; i > 0; i--)

    {

        // Pick a random index from 0 to i

        int j = rand() % (i+1);

        // Swap arr[i] with the element at random index

        swap(&arr[i], &arr[j]);

    }

}

// Driver program to test above function.

int main()

{

    int arr[] = {1, 2, 3, 4, 5, 6, 7, 8};

    int n = sizeof(arr)/ sizeof(arr[0]);

    randomize (arr, n);

    printArray(arr, n);

    return 0;

}

Complete and full answer in order to give credit. Do not copy-paste a similar problem.Program is to be written in C (just plain C) Thank you. Write algorithms a
Complete and full answer in order to give credit. Do not copy-paste a similar problem.Program is to be written in C (just plain C) Thank you. Write algorithms a
Complete and full answer in order to give credit. Do not copy-paste a similar problem.Program is to be written in C (just plain C) Thank you. Write algorithms a

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site