Thread: need help with functions

  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    4

    need help with functions

    Hey everyone,

    I am doing a project for school and I am stuck on a function that I need to make my program work. I DO NOT want the code written for me, I just want some tips on what I am doing wrong so I can fix it. I have multiple functions I need to write for this program but I just want to know what I am doing wrong with one of them. I will figure out the rest later.

    My teacher provided us with this file, it has all the shells of functions we will need in it but we have to write the code for each function, and then write a program simulating the card game Old Maid by calling in these functions. I cannot change any parameters in these functions. I am stuck on the function addToDeck.

    Can someone please tell me where I messed up so I can fix it and continue with my program?

    Thanks.

    Code:
    #ifndef CARDGAMES_H
    #define CARDGAMES_H
    
    #include <stdlib.h>
    #include <string.h>
    #include <stdbool.h>
    #include <stdio.h>
    #include <time.h>
    
    typedef struct {
        char suit;
        char label;
        int value;
    } card ;
    
    //function prototypes
    void shuffleCards(card deck[], int deckLength);
    void cardString(card c, char * lbl);
    bool drawCard(card deck[], card * returnCard, int * deckLength);
    int combineDecks(card deckToHold[], int * holdlength, card deckToTake[], int * takelength);
    void addToDeck(card deck[], card toAdd, int * deckLength);
    void printDeck(card deck[], int deckLength);
    int containsCard(card hand[], int nCards, card find);
    int containsMatch(card hand[], int nCards, card find);
    int compareCards(card c1, card c2);
    card removeCard(card deck[], int * deckLength, int index);
    bool isRed(card c);
    bool isBlack(card c);
    
    function: addToDec
    parameters:
    card deck[]: array of cards representing the deck
    card toAdd: the card to add to the deck
    int * deckLength: the number of cards currently held in the deck
    
    Adds the card toAdd to the end of deck (at position deckLength), and increments deckLength
    to reflect the added card.
    
    */
    void addToDeck(card deck[], card toAdd, int * deckLength)
    { 
        int i;
        for(i=0; i < deckLength; i++)
        {
            sprintf(toAdd, "%s\n", deck[i]);
        }
    }
    
    
    /*
    void printDeck
    parameters: 
    card deck[]: array of cards to print out
    int deckLength: number of cards in the deck
    
    This function uses the cardString function to print out each 
    card on the deck, with each card appearing on a new line.
    
    for each card in the deck
        get the string of that card using cardString
        print it out
        print a newline
    */
    void printDeck(card deck[], int deckLength)
    {
        int i;
        for(i=0; i < deckLength; i++)
        {
            char  lbl[100];
            cardString(deck[i], lbl);
            printf("%s\n", lbl);
        }
    }
    
    /*
    function: containsCard
    parameters:
    card hand[]: array that represents a hand of cards
    int nCards: the number of cards in a hand
    card find: the card we're trying to find in that hand
    
    returns:
    integer. if the card is in the hand, the array index that contains that card
        if the card is not in the hand, -1
    
    Looks through the entire array and uses the compareCards function to determine
    whether the hand contains that card. If it does contain that card, returns the index
    where that card is. If the card is not in the hand, returns -1.
    
    for each card in the hand
        if compareCards(the find card, the card in the hand) == 0
            return that card's index
    if we didn't find the card return -1
    
    */
    int containsCard(card hand[], int nCards, card find){
    
    }
    
    /*
    function: containsMatch
    parameters:
    card hand[]: array that represents a hand of cards
    int nCards: the number of cards in a hand
    card find: the card we're trying to find the match to in that hand, where a match
    is a card of the same value and same color suit, but not same suit (for example, king of hearts
    matches to king of diamonds; ten of spades matches to ten of clubs, etc).
    
    returns:
    integer. if the card is in the hand, the array index that contains that card
        if the card is not in the hand, -1
    
    Determine which suit we're looking for:
        if find.suit is diamonds, we're looking for     hearts
                hearts                diamonds
                clubs                spades
                spades                clubs
    
    for each card in the hand:    
        if the value of that card is equal to the value of the find card
            if the suit of that card is equal to the suit we're looking for
                return the index of that card
    
    if we didn't find the card, return -1.
    */
    int containsMatch(card hand[], int nCards, card find){
    
    }
    
    /*
    function: compareCards
    parameters:
    card c1: a card
    card c2: a card
    returns: 0 if card 1 has the same numerical value as card 2
             the difference of c2 - c1's values if they are not equal 
                  (so a positive value if c1 < c2 and a negative value if c2 > c1)
    */
    int compareCards(card c1, card c2)
    {
        return c2.value - c1.value;
    }
    
    /*
    function: removeCard
    parameters: 
    card deck[]: deck of cards to remove the card at the given index from
    int * deckLength: number of cards in the deck
    int index: index of the card to remove
    
    returns: the removed card
    
    Removes the card at index by doing the following:
    
    swap the card at index with the card at the end of the deck 
    declare a new card, and set it equal to the card at the end of the deck
    Decrement decklength to indicate that that card is gone
    return the new card you declared and set equal to the end of the deck
    
    */
    card removeCard(card deck[], int * deckLength, int index){
    
    }
    
    /*
    function: isRed
    parameters:
    card c: a card
    returns: true if the card is red (has suit diamond or heart)
    */
    bool isRed(card c)
    {
        if(c.suit == 'D' || c.suit == 'H')
        {
            return 1;
        }
        else 
        {
            return 0;
        }    
    }
    
    /*
    function: isBlack
    parameters:
    card c: a card
    returns: true if the card is black (has suit spade or club)
    */
    bool isBlack(card c){
        if(c.suit == 'S' || c.suit == 'C')
        {
            return 1;
        }
        else
        {
            return 0;
        }
    }
    
    //////////////////////////////////////////
    //    extra credit options        //
    //////////////////////////////////////////
    
    /*
    function: sortCards
    parameters:
    card deck[]: array of cards
    int decklength: number of cards in the deck[] array
    
    This function should sort the array of cards in the following order:
    by suits: clubs, diamonds, hearts, spades (alphabetic)
    within each suit: 2 3 4 5 6 7 8 9 T J Q K A (value of T = 10, J=11, value of Q=12, value of K=13, value of A = 14)
    */
    void sortCards(card deck[], int deckLength){
    
    }
    
    /*
    function: loadDeck
    parameters:
    card deck[]: array that the deck should be loaded into
    string filename: file name that contains the deck information. 
        the file should be in the format
        suit label value
        suit label value
        ...
        one line for each card.
        for example:
        H A 13    
        H 2 2
        H 3 3
        for ace, 2, and 3 of hearts.
        
        There are an undetermined number of cards in the file (maybe > 52).
        You will need to support any number of cards.
        You can do this through dynamic memory allocation or other methods.
    
    return: the number of cards loaded into the deck.
    */
    int loadDeck(card deck[], char * filename){
    
    }
    
    //////////////////////////////////
    //    provided functions     //
    //////////////////////////////////
    
    /*
    function: loadDefaultDeck
    parameters:
    card deck[]: array that the deck should be loaded into
    
    Loads the standard deck into the deck array.
    
    return: the number of cards loaded into the deck.
    */
    int loadDefaultDeck(card deck[]){
        int suit;
        int value;
        int ctr = 0;
        for (suit = 0; suit < 4; suit++){
            for (value = 2; value < 15; value++){
                switch (suit){
                    case 0:
                        deck[ctr].suit = 'D';
                        break;
                    case 1:
                        deck[ctr].suit = 'H';
                        break;
                    case 2:
                        deck[ctr].suit = 'S';
                        break;
                    case 3:
                        deck[ctr].suit = 'C';
                        break;
                }
    
                deck[ctr].value = value;
            
                switch (value){
                    case 2:
                    case 3:
                    case 4:
                    case 5:
                    case 6:
                    case 7:
                    case 8:
                    case 9:
                        deck[ctr].label = value + 48;
                        break;
                    case 10:
                        deck[ctr].label = 'T';
                        break;
                    case 11:
                        deck[ctr].label = 'J';
                        break;
                    case 12:
                        deck[ctr].label = 'Q';
                        break;
                    case 13:
                        deck[ctr].label = 'K';
                        break;
                    case 14:
                        deck[ctr].label = 'A';
                        break;
                }
                ctr++;
            }
        }
        return ctr;
    }
    
    /*
    function: cardString
    parameters:
    card c: the card to print the name of
    char * lbl: an already-allocated character array
    
    This function should return the string name of the card passed in in the 
    parameter string lbl.
    For example, "queen of hearts", "ace of spades", etc.
    
    */
    
    void cardString(card c, char * lbl){
        switch (c.label){
            case '2':
                strcpy(lbl, "two of ");
                break;
            case '3':
                strcpy(lbl, "three of ");
                break;
            case '4':
                strcpy(lbl, "four of ");
                break;
            case '5':
                strcpy(lbl, "five of ");
                break;
            case '6':
                strcpy(lbl, "six of ");
                break;
            case '7':
                strcpy(lbl, "seven of ");
                break;
            case '8':
                strcpy(lbl, "eight of ");
                break;
            case '9':
                strcpy(lbl, "nine of ");
                break;
            case 'T':
                strcpy(lbl, "ten of ");
                break;
            case 'J':
                strcpy(lbl, "Jack of ");
                break;
            case 'Q':
                strcpy(lbl, "Queen of ");
                break;
            case 'K':
                strcpy(lbl, "King of ");
                break;
            case 'A':
                strcpy(lbl, "Ace of ");
                break;
        }
    
        switch (c.suit){
            case 'D':
                strcat(lbl, "diamonds");
                break;
            case 'H':
                strcat(lbl, "hearts");
                break;
            case 'C':
                strcat(lbl, "clubs");
                break;
            case 'S':
                strcat(lbl, "spades");
                break;
        }
    }
    #endif
    I took out some of the functions I haven't gotten to for easier to read.

  2. #2
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Code:
    /*
    function: addToDec
    
    parameters:
    card deck[]: array of cards representing the deck
    card toAdd: the card to add to the deck
    int * deckLength: the number of cards currently held in the deck
     
    Adds the card toAdd to the end of deck (at position deckLength), and increments deckLength
    to reflect the added card.
     
    */
    void addToDeck(card deck[], card toAdd, int * deckLength)
    { 
        int i;
        for(i=0; i < deckLength; i++)
        {
            sprintf(toAdd, "%s\n", deck[i]);
        }
    
    }
    Based on the function description you don't need to iterate over the deck like that, you can simply add the card toAdd at position *deckLength in the deck array. After that increment *decklength and you should be done.

  3. #3
    Registered User
    Join Date
    Oct 2013
    Posts
    4
    Quote Originally Posted by Hodor View Post
    Code:
    /*
    function: addToDec
    
    parameters:
    card deck[]: array of cards representing the deck
    card toAdd: the card to add to the deck
    int * deckLength: the number of cards currently held in the deck
     
    Adds the card toAdd to the end of deck (at position deckLength), and increments deckLength
    to reflect the added card.
     
    */
    void addToDeck(card deck[], card toAdd, int * deckLength)
    { 
        int i;
        for(i=0; i < deckLength; i++)
        {
            sprintf(toAdd, "%s\n", deck[i]);
        }
    
    }
    Based on the function description you don't need to iterate over the deck like that, you can simply add the card toAdd at position *deckLength in the deck array. After that increment *decklength and you should be done.

    ok I don't fully understand. what I am having problems with now are that I get an error saying invalid operands for binary ( "int" + "card")...

    heres what I got now.
    Code:
    void addToDeck(card deck[], card toAdd, int * deckLength)
    { 
        int i;
        for(i=0; i < deckLength; i++)
        {
            deck[i] = * deckLength + toAdd;
        }
    }

  4. #4
    Registered User
    Join Date
    Jun 2010
    Location
    Michigan, USA
    Posts
    143

    Questions to which you need to decide the answers

    It is clear to me that you do not understand what your data structures look like or in other words contain. You need to decide for yourself before you start coding what your data structures would look like for some specific situations. You seem to be coding before you are done designing.

    Here are some questions that you need to answer for yourself before you try to write the contents of addToDeck function. Base you answers on the definition of the typedef card and the loadDefaultDeck function which you were given.

    How would you define an "empty" card deck that could hold the normal 52 cards? What data structures and values would you need?

    How would you print that "empty" card deck?

    Consider making a main program that would define and print an empty card deck.

    How would you define a "normal" card deck using the loadDefaultDeck() function?

    Write a main program (or modify the one from above) to define and print a normal card deck after using loadDefaultDeck fuction.

    If I started with an "empty" card deck as you defined above and made a valid call to addToDeck function, what would the data structure look like after the call?

    When would an addToDeck function call cause undefined behavior or just not be a good idea?

  5. #5
    Registered User
    Join Date
    Oct 2013
    Posts
    4
    I just realized that I forgot to mention this before. This file is a .h file and we were provided with a .c test file that will test each of the functions in this one so we can make sure that they are right before using them in our own program. So as far as printing out an empty deck, the test file has a few cards defined so that it is supposed to print those out. the printDeck function I have above is working properly as well as the isblack and isred functions. I am also not allowed to change anything in this file except the code inside of the function shells because my teacher needs that to be the same for when she grades every ones programs. However, thank you for your feedback and for trying to help me. I will think about your questions and keep working on it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 05-27-2013, 06:43 PM
  2. WinAPI functions - similar functions as part of VS C++
    By jlewand in forum Windows Programming
    Replies: 2
    Last Post: 02-02-2012, 08:54 AM
  3. Creating Functions & passing information to other functions
    By RyanLeonard in forum C Programming
    Replies: 4
    Last Post: 10-28-2010, 12:17 PM
  4. Replies: 6
    Last Post: 05-06-2003, 03:08 PM
  5. Replies: 1
    Last Post: 01-20-2002, 11:50 AM