Question

In: Computer Science

Based on this player.h file, could you make a player.c file which defines the 4 functions?...

Based on this player.h file, could you make a player.c file which defines the 4 functions? Thanks.

player.h:

#ifndef PLAYERH
#define PLAYERH
#define MAX_ITEMS 10

typedef struct Player
{

int location;
int num_items;
int mitems[MAX_ITEMS]

}t_player;

void player_init_player(t_player *player, int location); //initialize player
void player_add_item(t_player *player, int item);
void player_remove_item(t_player *player, int item);
int player_has_item(t_player *player, int item); //returns -1 if no item exists

#endif

Solutions

Expert Solution

Hey There,

- I have understood your question and I have given the answer to the best of my knowledge.

- Here I have used C as my programming language and I have attached both the files and the screenshot showing that the code is working perfectly fine.

- I have also put comments into my code so that you can also understand what I did in the code.

- If you need any further explanations then do let me know in the comments box I will be more than happy to help you.

Answer:

- "player.h" file

#ifndef PLAYERH
#define PLAYERH
#define MAX_ITEMS 10

typedef struct Player
{

int location;
int num_items;
int mitems[MAX_ITEMS];
}t_player;

void player_init_player(t_player *player, int location); //initialize player
void player_add_item(t_player *player, int item);
void player_remove_item(t_player *player, int item);
int player_has_item(t_player *player, int item); //returns -1 if no item exists

#endif

- "player.c" file

// Including header files
#include<stdio.h>
#include "player.h"

// This function will initialize the location of the player
void player_init_player(t_player *player, int location){
    player->location = location;
}

// This function will add the given item
void player_add_item(t_player *player, int item){
    player->mitems[player->num_items] = item;
    player->num_items++;
}

// This function will remove the given item
void player_remove_item(t_player *player, int item){
    int i, j, count = 0; 
    for(i=0; i<player->num_items; i++){
        if(player->mitems[i] == item){
            printf("The given item removed!\n");
            count++;
            for(j=i; j<player->num_items-1; j++){
                player->mitems[j] = player->mitems[j+1];
            }
        }
        player->num_items--;
    }
    if(count == 0){
        printf("The given item is not present!");
    }
}

// This function will check if the given item is peresent or not
int player_has_item(t_player *player, int item){
    int count = 0, i;
    for(i=0; i<player->num_items; i++){
        if(player->mitems[i] == item){
            count++;
            return 1;
        }
    }
    if(count == 0){
        return -1;
    }
}

int main(){
        // creating struct type object t1
    t_player t1;
    
    // Initializing num_items equals to 0
    t1.num_items = 0;
    
    // Calling the first function - player_init_player
    player_init_player(&t1, 2);
    // Printing location to see if the above function worked correctly
    printf("The added Location is : %d\n\n", t1.location);
    
    // Calling the second function - player_add_item
    player_add_item(&t1, 3);
    // Printing total no. of items to see if the above function worked correctly
        printf("After adding %d the total Items = %d\n\n", 3, t1.num_items);
    
        // Again calling the second function - player_add_item
        player_add_item(&t1, 25);
    printf("After adding %d the total Items = %d\n\n", 25, t1.num_items);
    
        // Calling the third function - player_remove_item
        player_remove_item(&t1, 3);
    // Printing total no. of items to see if the above function worked correctly
        printf("After removing %d the total items = %d\n\n", 3, t1.num_items);
    
        // Calling the third function - player_has_item
        int result = player_has_item(&t1, 25);
    if(result == 1){
        printf("The given item %d is present!", 25);
    }
    if(result == -1){
        printf("The given item %d is not present!", 25);
    }
    return 0;
}

Screenshot of code working correctly:

Hope it helps:)


Related Solutions

Which of the following is a logical conclusion one could make based on the passage?
Graveyard walk Steven walked through the graveyard every day on his way home from school. It was a convenient shortcut, and in broad daylight, the tombstones and dark cypress trees seemed mild and unthreatening. Things were different this evening. Steven stole through the gates as quickly and quietly as possible. Fearful that he was being watched. He ran to the deep shadows of a mausoleum and caught his breath, heart pounding. He tried to listen for ominous noises, but could hear nothing...
The project requires you to implement 4 functions of your choice in a file called Utilities.java....
The project requires you to implement 4 functions of your choice in a file called Utilities.java. The functions may relate to statistics, graphics/animation, audio, text, or image applications. Deliverables (files that need to be submitted): 1. A word document that briefly describes the functions that were implemented and the API. 2. Utilities.java file with the 4 functions and a Test.java file that provides examples of using the functions in Utilities.java.
Describe a setting where you could use exponential functions to make investment decisions. What kind of...
Describe a setting where you could use exponential functions to make investment decisions. What kind of information could exponential functions tell you that would be valuable?
Please make an Array-based implementation of a Binary Tree in Python based on the given file...
Please make an Array-based implementation of a Binary Tree in Python based on the given file below. Make sure to keep the Abstract Data File of the starter code below when building the Array-based implementation. Python Starter Code Given Below: class ArrayBinaryTree(BinaryTree): """Linked representation of a binary tree structure.""" # -------------------------- nested _Node class -------------------------- class _Node: def __init__(self, element, parent=None, left=None, right=None): # -------------------------- nested Position class -------------------------- class Position(BinaryTree.Position): """An abstraction representing the location of a single element."""...
Based on the following scenario, which project do you support if you make a decision based...
Based on the following scenario, which project do you support if you make a decision based on the IRR alone?   Project A requires an initial investment (or you may say, ‘cash outflow’) of $525,000 and is expected to generate the following net cash inflows: Year 1: $120,000 Year 2: $110,000 Year 3: $120,000 Year 4: $110,000 Year 5: $120,000 Year 6 : $130,000 The expected rate of return is 5%. Project B also requires an initial investment (or you may...
4. You manage a crew of 160 workers who could be assigned to make either of...
4. You manage a crew of 160 workers who could be assigned to make either of two products. Product A requires two workers per unit of output. Product B requires four workers per unit of output. a) Write an equation to express the combinations of products A and B that could be produced using exactly 160 workers. b) Measuring the quantity of product A on the horizontal axis and the quantity of product B on the vertical axis, use vertical...
Just Make up a project Pick a project with which you are familiar. It could be...
Just Make up a project Pick a project with which you are familiar. It could be the project you described in Chapter 1 or any other project. Briefly describe the project. Once you have provided an overview of the project develop a project scope statement. As described in the textbook, a project scope statement should contain the following items: Project objective Deliverables Milestones Technical requirements Limits and exclusions
Could make a answer fast you can please! Thank you! Learning Objectives: CHAPTER 4 Describe fraud...
Could make a answer fast you can please! Thank you! Learning Objectives: CHAPTER 4 Describe fraud and its impact Explain the objectives and components of internal control Design and use a bank reconciliation Evaluate internal controls over cash receipts and cash payments Construct and use a cash budget Report cash on the balance sheet EXAMPLE OF WHAT I'M LOOKING FOR: One thing I found challenging was the credits and debits concept from chapter two and matching them up, (common stock...
1. You are trying to make decisions on which mutual fund you should invest in based...
1. You are trying to make decisions on which mutual fund you should invest in based on the past performance of two fund managers. Manger A averaged a 17% return with a portfolio beta of 1.5, and manager B averaged a 15% return with a portfolio beta of 1.2. If the T-bill rate was 5% and the market return during the period was 13%, which fund manager was the better stock picker? A.         Advisor A was better because he generated a...
Question 1 0 / 1 pts Which statement below best defines social darwinism? It based on...
Question 1 0 / 1 pts Which statement below best defines social darwinism? It based on the idea that there are groups in society that are more evolved than other. The British and the United States used it in order to justify the conquering and subjugation of people all over the world. It is Darwin's theory of evolution where it explains that certain species die off while others survive overtime. It is the theory that says that we are the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT