•

Check out our Community Blogs

Register and join over 40,000 other developers!

### Recent Blog Entries

• phi

I love this community !

• JackJames

hi i am jack i am seo expert jack james would love you to read new post

# Picking Random Numbers From An Array By Using Pointers

pointers arrays functions rand random number array

4 replies to this topic

### #1 toyganozy

toyganozy

CC Lurker

• New Member
• 4 posts

Posted 05 April 2012 - 04:23 PM

Copy this code into your C program.

```//  Then complete the subroutine deal() below so that 5 cards are randomly assigned to cards[][]
//  Each time the subroutine is called, a different hand should be dealt.
//   There are 52 cards, 13 denominations, 1 in each of 4 suits.
//   The cards, Ace-through-King, A,2,3,4,5,6,7,8,9,10,J,Q,K and four card suits Spades (S), Hearts (H),
//	Diamonds (D), and Clubs © are used to denote, e.g. a Jack of hearts as "JH",
//	and a 10 of diamonds as "10D". Use the array cards[][] in deal()

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
void deal(char *ptr);
main(){
int m;
char hand[5][4],nd;
int nodeal;
printf("deal a hand? (enter y or n) ");
scanf("%s",&nd);
while(nd=='y'){
deal(hand[0]); // hand is a pntr-to-a-pntr. hand[k] is a pntr.
for(m=0; m<5; m++){printf("%s  ",(hand[m]));}
printf("\n");
printf("deal a hand? (enter y or n) ");
scanf("%s",&nd);
}

system("pause");
}

void deal(char *ptr){
char cards[52][4]={"AS","2S","3S","4S","5S","6S","7S","8S","9S","10S","JS","QS","KS","AH","2H","3H",
"9D", "10D","JD","QD","KD","AC","2C","3C","4C","5C","6C","7C","8C","9C","10C","JC","QC","KC"};
int i,j,k,c[5];

// seed the random numer generator using the time  srand((unsigned int)time(NULL));

// generate 5 random values 0-to-51 with no duplications.

// check for duplications, correct any duplications

// load the chosen cards using the pointer. You’ll need to load 4 bytes/card based on the character pointer

}```

Can anyone help me to complete this function. I am having a hard time doing it.

Edited by Roger, 07 April 2012 - 07:39 AM.

• 0

### #2 WingedPanther73

WingedPanther73

A spammer's worst nightmare

• Moderator
• 17757 posts
• Location:Upstate, South Carolina
• Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
• Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 06 April 2012 - 07:26 PM

well, the "seed the random number generator" part should be pretty straight-forward...

How can you generate 5 random numbers and ensure no duplicates?
• 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/

### #3 toyganozy

toyganozy

CC Lurker

• New Member
• 4 posts

Posted 06 April 2012 - 08:30 PM

Like you said, seeding was easy.

Maybe use an "if" statement to ensure it ?.?

I'm having hard time with picking the values, assigning them to c[5] array, and returning the new c[5] array....
• 0

### #4 kernelcoder

kernelcoder

CC Devotee

• Expert Member
• 990 posts
• Location:Dhaka
• Programming Language:C, Java, C++, C#, Visual Basic .NET
• Learning:Objective-C, PHP, Python, Delphi/Object Pascal

Posted 06 April 2012 - 11:23 PM

Hi,

Returning c[5] will not work as it is local variable to method 'deal' which created on stack. If you do this, you will die at runtime. Following is the code I made working. Please have a look at it..

```
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include <string.h>

void deal(char ptr[][4] ){
char cards[52][4]={"AS","2S","3S","4S","5S","6S","7S","8S","9S","10S","JS","QS","KS","AH","2H","3H",
"9D", "10D","JD","QD","KD","AC","2C","3C","4C","5C","6C","7C","8C","9C","10C","JC","QC","KC"};
int i,j,rnd;
srand((unsigned int)time(NULL));

for(i = 0; i < 5; i++) {
rnd = rand() % 52;
j = 0;
while(j < i) {
rnd = rand() % 52;
j = 0;
}
else {
j++;
}
}
strcpy(ptr[i], cards[rnd]);
}
}

int main(){
int m;
char hand[5][4],nd;
int nodeal;
printf("deal a hand? (enter y or n) ");
scanf("%s",&nd);
while(nd=='y'){
deal(hand); // hand is a pntr-to-a-pntr. hand[k] is a pntr.
for(m=0; m<5; m++){printf("%s ",(hand[m]));}
printf("\n");
printf("deal a hand? (enter y or n) ");
scanf("%s",&nd);
}

system("pause");
return 0;
}
```

Please let me know if it works for you.
• 0

### #5 WingedPanther73

WingedPanther73

A spammer's worst nightmare

• Moderator
• 17757 posts
• Location:Upstate, South Carolina
• Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
• Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 07 April 2012 - 10:09 AM

Close, but what if you find the match on the 3rd item, and you generate a new match to the 1st item (that you've already passed)?

I'd use a while loop that contains a sentinel value (bMatch or something), and an inner loop to check if a match is found.
• 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/

### Also tagged with one or more of these keywords: pointers, arrays, functions, rand, random number, array

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download