Thread: Deck of cards - Easy to understand program

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

    Deck of cards - Easy to understand program

    I'm trying to make a deck of cards using c programming. I want it so the computer will ask me for a number from 1-52 and each of the numbers will correspond to a card. I've got as far as the program printing every card but I have no idea how to make a card "whole". I have all the numbers and all the suits but they are in no way attached, they just print up at together. Can anyone help please.

    Code:
    #include <stdio.h>
    
    
    int main(void) {
    
    
      char value[14][6]={"Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Jack","Queen","King","Ace"};
      char suit[4][9]= {"Hearts", "Diamonds", "Clubs", "Spades"};
      
      int i=0;
      int j=0;
      
      for (i=0; i<13; i++){
          for (j=0; j<4; j++){
               printf("The %s of %s\n\n", value[i], suit[j]);
          }
      }
      system("pause");
    }
    All this does is print each number 4 times and cycles through the suits.

    Also I'm relatively new to c programming so if possible could the programming be kept at a level easy to understand.
    Thanks!

  2. #2
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    A simple solution is to calculate the indices for your "value" and "suit" arrays for any number between 1 and 52.
    Hint: imagine your cards are ordered by suits (first 13 cards are hearts, next 13 cards are diamonds, ...) and then use division and modulo/remainder.

    Code:
    char value[14][6]={"Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Jack","Queen","King","Ace"};
    There are only 13 cards for each suit.

    Bye, Andreas

  3. #3
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Shouldn't it be 1-56?
    Use scnaf in order to make the computer get a number from the keyboard.
    Then you could use Andreas idea

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by std10093 View Post
    Shouldn't it be 1-56?
    Use scnaf in order to make the computer get a number from the keyboard.
    Then you could use Andreas idea
    No, the 14 was a mistake, there are 13 cards per suit, 13x4 = 52

    Unless you have a 500 pack (having 11's, 12's, 13's) in which case there are 16 cards per suit for a total of 64 cards.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Oh ok iMalc.I do not know about card and decks,i just read the code

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 04-30-2012, 09:52 PM
  2. Trying to Model a Deck of Cards.
    By _lisa in forum C Programming
    Replies: 3
    Last Post: 11-16-2011, 12:05 AM
  3. Trying to make a deck of cards.
    By Jesse20ghet in forum C++ Programming
    Replies: 1
    Last Post: 11-07-2011, 03:44 PM
  4. Easy but do not understand XD.
    By GamerProduction in forum C++ Programming
    Replies: 4
    Last Post: 06-20-2007, 10:58 AM
  5. filling a deck of cards
    By lakai02 in forum C Programming
    Replies: 6
    Last Post: 12-07-2002, 10:13 AM

Tags for this Thread