Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The Lab Part 1 A deck of cards can be represented by an array of integers from 1

ID: 3699336 • Letter: T

Question

The Lab

Part 1

A deck of cards can be represented by an array of integers from 1 to 52. One way to shuffle the cards is to generate a permutation of the integers. A permutation is a way in which a set number of things can be ordered or arranged. The easiest way to generate a permutation is to randomly select an integer from 1 to 52, the problem with this method is we have to have a way to eliminate duplicates. There are numerous ways to accomplish this but for this lab follow the steps below.

Create a header file

Create a file called Permutation.h that will hold your function prototypes. To accomplish our lab you will need to create three functions - but only two need to be in the header (Why?)

int* initPermuation (int n);    // Creates a dynamic array of size n, initializes it with the correct values and returns the array to the user

int nextPermutation (int* cards, int& deckSize);    // Receives the currect card array and the size of the array, returns the next "card". Deck size is passed as a reference variable so it can be modified

void removePermutation (int* deck, int deckSize, int card);   //Removes the index at card from the array. This function is called by nextPermutation and is never called by the user.

Code to implement these should be in your Permutations.cpp file.Steps for the Program

This code should appear in a file called Driver.cpp.

Ask the user for the size of the deck, call it n

Generate a deck using the initPermutation function

Loop through the size of the deck (hint: do not use your deckSize variable for this)Call nextPermutation

Randomly generate a number between 0 and deckSize - 1

Save the card value currently stored at that index

Call removePermutation

Shift remaining elements down to fill the gap created by "removing" the card you just drew

Reduce the deckSize by 1

Return the card value saved earlier

Print out the value returned

Release the dynamically allocated memory

Sample Output

A possible outcome of running this program with n = 10 is below. Remember yours should be different.

Deck Permutations
5
2
9
1
8
4
7
3
10
6

Helpful Files

Random.cpp
Random.h
These files should help with generating your random numbers. The header file contains Pre- and Post- conditions so you can understand how to use the files.

Explanation / Answer

//main.cpp

#include <iostream>

#include "permutations.h"

using namespace std;

int* initPermutation(int n)

{

int* a=new int [n];

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

{

*(a+i)=i+1;

}

return a;

}

int main() {

// your code goes here

int n,deck_size;

cout<<"Enter deck size:";

cin>>n;

deck_size=n;

int* cards;

cards=initPermutation(n);

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

{

int a;

a=nextPermutation(cards,deck_size);

cout<<" "<<a;

}

delete[] cards;

return 0;

}

///////////////////////////////////////

// permutations.h

#ifndef PERMUTATIONS_H
#define PERMUTATIONS_H


void removePermutation(int* cards,int ds,int r);

int nextPermutation(int *cards,int& ds);

#endif // PERMUTATIONS_H

////////////////////////////////////////////////////

//permutations.cpp

#include<time.h>

#include<stdlib.h>

#include"permutations.h"

#define ll long long

using namespace std;

void removePermutation(int* cards,int ds,int r)

{

for(int i=r;i<ds-1;i++)

{

*(cards+i)=*(cards+i+1);

}

return;

}

int nextPermutation(int *cards,int& ds)

{

srand((int)time(0));

int r=(rand()%ds);

int p=*(cards+r);

removePermutation(cards,ds,r);

ds=ds-1;

return p;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote