Optimization of code

This is a discussion on Optimization of code within the C++ Programming forums, part of the General Programming Boards category; Hey I got a question about optimizing a chunk of code, I already tried to make it smaller, but now ...

  1. #1
    I am me, who else?
    Join Date
    Oct 2002
    Posts
    250

    Post Optimization of code

    Hey I got a question about optimizing a chunk of code, I already tried to make it smaller, but now I am out of ideas for a moment and I thought I'd see how others might do this. So all I want to do is when I pass a function a number I want it to give it a suit and the number as a word, (e.g. 4 => four).


    Here's the code:

    Code:
    void show_card(int num)
    {
    
        char suit[100];
            char number[100];
            //char *display;
            if(num < 13)
            {
                    strcpy(suit,"of Spades");
            }
            else if(num >= 13 && num < 26)
            {
                    strcpy(suit,"of Clubs");
            }
            else if(num >= 26 && num < 39)
            {
                    strcpy(suit, "of Diamonds");
            }
            else
            {
                    strcpy(suit,"of Hearts");
            }
    
            if(num == 0 || num == 13 || num == 26 || num == 39)
            {
                    strcpy(number, "Two ");
            }
            else if(num == 1 || num == 14 || num == 27 || num == 40)
            {
                    strcpy(number, "Three ");
            }
            else if(num == 2 || num == 15 || num == 28 || num == 41)
            {
                    strcpy(number, "Four ");
            }
            else if(num == 3 || num == 16 || num == 29 || num == 42)
            {
                    strcpy(number, "Five ");
            }
            else if(num == 4 || num == 17 || num == 30 || num == 43)
            {
                    strcpy(number, "Six ");
            }
            else if(num == 5 || num == 18 || num == 31 || num == 44)
            {
                    strcpy(number, "Seven ");
            }
            else if(num == 6 || num == 19 || num == 32 || num == 45)
            {
                    strcpy(number, "Eight ");
            }
            else if(num == 7 || num == 20 || num == 33 || num == 46)
            {
                    strcpy(number, "Nine ");
            }
            else if(num == 8 || num == 21 || num == 34 || num == 47)
            {
                    strcpy(number, "Ten ");
            }
            else if(num == 9 || num == 22 || num == 35 || num == 48)
            {        strcpy(number, "Jack ");
            }
            else if(num == 10 || num == 23 || num == 36 || num == 49)
            {
                    strcpy(number, "Queen ");
            }
            else if(num == 11 || num == 24 || num == 37 || num == 50)
            {
                    strcpy(number, "King ");
            }
            else if(num == 12 || num == 25 || num == 38 || num == 51)
            {
                    strcpy(number, "Ace ");
            }
            cout << " " << number << suit << " ";
    }
    my apologies for how big it is, but if anyone can help with any tricks or what have you it'd be great, thanks!

  2. #2
    Bios Raider biosninja's Avatar
    Join Date
    Jul 2002
    Location
    South Africa
    Posts
    765
    Your code is very neat as it is.

    Maybe try using a switch?

  3. #3
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Code:
    char suits[][15] = { "of Spades", "of Clubs", "of Diamonds", "of Hearts" };
    char numbers[][15] = { "Two ", "Three ", "Four ", "Five ", "Six ", "Seven ", "Eight ", "Nine ", "Ten ", "Jack ", "Queen ", "King ", "Ace " };
    
    void show_card(int num)
    {
    	char suit[100];
    	char number[100];	
    
    	strcpy(suit, suits[num / 13]);
    	strcpy(number, numbers[num % 13]);
    	
    	cout << " " << number << suit << " ";
    }
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  4. #4
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    could be much neater.

    try something like this:

    Code:
    char *suits[] =
    {
      "Spades",
      "Hearts",
      "Diamonds",
      "Clubs"
    };
    
    char *cards[] =
    {
      "Ace",
      "Two",
      "Three",
      "Four",
      "Six",
      "Seven",
      "Eight",
      "Nine",
      "Ten",
      "Jack",
      "Queen",
      "King"
    };
    then something like

    Code:
    char card[40];
    
    sprintf (card, "%s of %s ", cards[num % 13], suits[num / 13]);
    which will produce the same results you're getting now (if you rearrange the suit list to match yours).
    Last edited by moi; 10-28-2002 at 04:02 PM.
    hello, internet!

  5. #5
    I am me, who else?
    Join Date
    Oct 2002
    Posts
    250
    Excellet! Thanks a bunch, its always good to get new interpretations of code, and different ways of looking at it. That'll help a lot!

  6. #6
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    ummm..... i just realized, before you use mine, add in the "Five" that i forgotor
    hello, internet!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-21-2006, 07:52 AM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM
  3. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 04:28 AM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 06:06 PM
  5. code optimization
    By gagig in forum C Programming
    Replies: 6
    Last Post: 02-21-2002, 04:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21