Thread: Picking random numbers from an array

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    1

    Picking random numbers from an array

    Code:
    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",
    "4H","5H","6H","7H","8H","9H","10H","JH","QH","KH","AD","2D","3D","4D","5D","6D","7D","8D",
    "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. 

  2. #2
    Registered User
    Join Date
    Sep 2011
    Location
    Athens , Greece
    Posts
    357
    First of all.

    You should do something with the readability
    of this code.

    i.e ->

    Code:
     for(m=0; m<5; m++){printf("%s ",(hand[m]));}
    You can write it as ->

    Code:
     for(m=0; m<5; m++)
         printf("%s" , (hand[m]));
    if the body of the loop contains only one command there is no need to add some braces.

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Can anyone help me to complete this function. I am having a hard time doing it.
    Don't just dump your assignment. Show what you've tried so far. And use the code tags properly, like this:

    [code]
    Your code here, with no extra formatting
    [/code]
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Picking numbers from text file
    By Scythed in forum C Programming
    Replies: 13
    Last Post: 11-09-2010, 01:50 AM
  2. picking out specific values from an array
    By jak9 in forum C Programming
    Replies: 7
    Last Post: 10-13-2010, 01:05 PM
  3. Filling an array with random numbers
    By euclid in forum C Programming
    Replies: 21
    Last Post: 11-28-2007, 06:53 PM
  4. Picking out random words
    By beanroaster in forum C++ Programming
    Replies: 16
    Last Post: 09-04-2005, 04:02 AM
  5. Unique Random Numbers in 2D Array
    By kssjbr in forum C++ Programming
    Replies: 2
    Last Post: 08-06-2003, 02:45 AM