please don\'t change definition of a struct , prototypes or Parameters. this .h
ID: 3546847 • Letter: P
Question
please don't change definition of a struct , prototypes or Parameters.
this .h file i need your help in writing .c file and the main.
#ifndef _hand_h
#define _hand_h
#include "Card.h"
#define HAND_SIZE 52
/*
* Here is the definition of a struct for a hand of cards
* cards - an array of cards
* count - an int indicating the "logical" number of cards in the hand
*/
typedef struct
{
CardT cards[HAND_SIZE];
int count;
}HandT;
/*
* Function prototypes for the Deck ADT
*/
/*
* Initializes the hand to an "empty" state
*
* Parameters:
* a pointer to the hand to be initialized
* Return value:
* none
*/
void initHand(HandT *);
/*
* Adds the card to the array of cards in the hand
*
* Parameters:
* a pointer to the hand
* a copy of the card to be added
* Return value:
* none
*/
void addToHand(HandT *, CardT);
/*
* Sort all the cards currently in the hand
* Note: Sort cards based on face value
*
* Parameters:
* a pointer to the hand to be sorted
* Return value:
* none
*/
void sortHand(HandT *);
/*
* Displays all cards currently in the hand
* Note: display one card per line
*
* Parameters:
* a pointer to the hand
* Return value:
* none
*/
void displayHand(HandT *);
#endif
Explanation / Answer
// save as hand.c
// if u face any error. please let me know.
#include<stdio.h>
#include "hand.h"
void initHand(HandT *hand)
{
hand->count = 0;
}
void addToHand(HandT *hand, CardT card)
{
hand->cards[count] = card;
hand->count=hand->count+1;
}
void sortHand(HandT *hand)
{
int toFill;
int smallPos;
int pos;
CardT temp;
//STEP 1: repeat each position toFill from 0 to n-2
for(toFill=0; toFill<=hand->count-2; toFill++)
{
//STEP 2.1:intialize smallPos to toFill
smallPos = toFill;
// STEP 2.2: repeat pos ranging from toFill to n-1
for(pos = toFill; pos<=hand->count-1; pos++)
{
//STEP 2.3: if hand[pos] is smaller than hand[smallPos]
if(hand->cards[pos].value<hand->cards[smallPos].value)
//STEP 2.4 copy pos to smallPos
smallPos = pos;
} // end for
// STEP 3: if smallPos not equal to toFill swap list[smallPos] to list[toFill]
if(smallPos!=toFill)
{
temp = hand->cards[smallPos];
hand->cards[smallPos] = hand->cards[toFill];
hand->cards[toFill] = temp;
}
} //end for
}
void displayHand(HandT *hand)
{
int index;
for(index=0; index<hand->count; index++)
displayCard(hand->cards[index]);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.