i need help with creating a deck of cards with a max size of 13. need help on ho
ID: 3847726 • Letter: I
Question
i need help with creating a deck of cards with a max size of 13.
need help on how to create the deck with 4 different suits.
void shuffle(int[]); /*
Argument – int Array signifying deck of cards, which was created in main
Return Nothing
Takes in the deck of cards array, loads with values from 1-13 and shuffles the order. Do not create a new array. Hint: Pass by reference!
Remember: Index of an array starts from location 0, your deck must have card values from 1-13. To shuffle the order use rand() function to get a random index and then swap the values. Example: After loading the deck with values from 1-13 in the locations 0-12, your deck is loaded. Now to shuffle, as of now at location 0, value is 1 and at location 1 value is 2 and so on. Imagine rand() returns a random index 3(which has the card value 4 in it now), you have to swap this with whatever is in location 0. This was just a one-time swap; you have to do it for all the 13 locations. How do you do it??
Hint: Bubble sort logic (but you are not sorting anything here).
Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
void shuffle(int card[])
{
int i ;
srand( time ( NULL ) );
for ( i = 0 ; i < 13 ; i++ )
{
int cardNo = rand( ) % ( 13 - i ) ;
int temp = card [ cardNo ] ;
card [ cardNo ] = card [ 13 - i - 1 ] ;
card [ 13 - i - 1 ] = temp ;
}
}
int main()
{
int deck[13] , i;
for(i=0;i<13;++i)
deck[i] = i+1;
shuffle(deck);
return 0;
}
==========================================================
Thanks, let me know if there is any concern.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.