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.
Program in C Make both a SOURCE AND HEADER FILE WITH FUNCTIONS to run the program....
Program in C Make both a SOURCE AND HEADER FILE WITH FUNCTIONS to run the program. Input data from csv file. Two files. grades.c and grades.h Thank you data.csv file Mark Prest,37468,36,113,In Person Andy Joe,6785923,19,98,Online Caden Miller,237741,20,70.1,In Person Luke Jr,2347878,18,45.9,In Online Ally Rogers,8467483,30,89.99,Online Maya Jank,5674930,30,90,In Person Expected output Name: Mark Prest ID: 37468 Age: 36 Grade: 113.00 Attending: In Person Name: Andy Joe ID: 6785923 Age: 19 Grade: 98.00 Attending: Online Name: Caden Miller ID: 237741 Age: 20 Grade: 70.10...
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?
Write a program in PYTHON which: *Defines the following 5 functions: whoamI() This function prints out...
Write a program in PYTHON which: *Defines the following 5 functions: whoamI() This function prints out your name and PRINTS OUT any programming course you have taken prior to this course. isEven(number) This function accepts one parameter, a number, and PRINTS OUT a message telling if the numberer is even or odd. Keep in mind that a number is even if there is no remainder when the number is divided by two. printEven(number) This function accepts one parameter, a number,...
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...
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."""...
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 you modify the code make output arranged into 5x5, like this: 1 2 3 4...
Could you modify the code make output arranged into 5x5, like this: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 The code is: import java.util.*; public class RandomGen { public static void main(String[] args) { int[][] ArrayRand = new int [5][5]; Random rand = new Random(); int sum = 0; System.out.println("The generated random matris is: ");    for(int i = 0; i <...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT