Question

In: Computer Science

I am stuck on this problem and I am not sure what the solution is. In...

I am stuck on this problem and I am not sure what the solution is. In C

Write item.h and item.c.

In item.h, typedef a struct (of type t_item) which contains the following information: t_item: char name[MAX_ITEM_NAME_STRING]; char description[MAX_ITEM_DESCRIPTION_STRING];

Make sure that MAX_ITEM_NAME_STRING and MAX_ITEM_DESCRIPTION_STRING are defined with suitable sizes in your item.h.

Typical values are, 25 and 80, respectively. Add the following interface definition to item.h: int item_load_items(t_item items[], int max_items, char *filename);

Returns the number of objects loaded from the filename (or -1 if unable to open the file and load the data). Fills the t_item array items with the contents of a file entitled "items.txt" int item_find_item(t_item items[], int max_items, char *item_name);

Returns the array index of the item with the item_name or -1 if not present. Make sure that you have a header guard around your declarations in item.h Header guards: (https://www.learncpp.com/cpp-tutorial/header-guards/) Implement the item_load_items and item_find_item functions in the item.c file. Use the provided main program to test your item.h and item.c files. Test it with the provided file, items.txt.

Here is my main.

**********************************************************
#include <stdio.h>

#include "item.h"

#define MAX_ITEMS 10

int main()
{
int place;
int num_items = 0;
t_item items[MAX_ITEMS];
  
if( (num_items = item_load_items(items, MAX_ITEMS, "items.txt")) > 0 )
{
for(int i=0; i<num_items; i++)
   {
   printf("%d\t\"%s\"\t\"%s\"\n", i, items[i].name, items[i].description);
   }


for(int i=0; i<num_items; i++)
   {
   if( (place = item_find_item(items, num_items, items[i].name)) > -1)
   {
   printf("Item %s is found at location %d\n", items[i].name, place);
   }
   else
   {
   printf("Can't find %s\n", items[i].name);
   }
   }
}
else
{
printf("ERROR: Unable to load items\n");
}
}

****************************************************************

This is text file

"kinfe","a large knife"
"spike","a large spike made of metal"
"sword","buster sword"
"bow","reflexive bow"

Solutions

Expert Solution

Code

item.h

#ifndef ITEMH

#define ITEMH

#define MAX_ITEM_NAME_STRING 25

#define MAX_ITEM_DESCRIPTION_STRING 80

typedef struct Item

{

char name[MAX_ITEM_NAME_STRING];

char description[MAX_ITEM_DESCRIPTION_STRING];

}t_item;

int item_load_items(t_item items[], int max_items, char *filename);

int item_find_item(t_item items[], int max_items, char *item_name);

#endif

item.c

#include"item.h"

#include<stdio.h>

#include<stdlib.h>

#include <string.h>

int item_load_items(t_item items[], int max_items, char *filename)

{

int count=0;

FILE *fp = fopen(filename, "r");

char *token,*pos;

int i,len;

if(fp != NULL)

{

char line[120];

while(fgets(line, sizeof(line), fp) != NULL)

{

token = strtok(line, ",");

for(i=0;i<2;i++)

{

if(i==0)

{

strcpy(items[count].name,token);

token = strtok(NULL,",");

} else {

strcpy(items[count].description,token);

if ((pos=strchr(items[count].description, '\n')) != NULL)

*pos = '\0';

}

}

count++;

}

fclose(fp);

}

return count;

}

int item_find_item(t_item items[], int max_items, char *item_name)

{

int i;

for(i=0;i<max_items;i++)

{

if(strcmp(items[i].name, item_name)==0)

return i;

}

return -1;

}

main.c

#include "item.h"

#include <stdio.h>


#define MAX_ITEMS 10

int main()

{

int place;

int num_items = 0;

t_item items[MAX_ITEMS];

if( (num_items = item_load_items(items, MAX_ITEMS, "items.txt")) > 0 )

{

for(int i=0; i<num_items; i++)

{

printf("%d\t\"%s\"\t\"%s\"\n", i, items[i].name, items[i].description);

}


for(int i=0; i<num_items; i++)

{

if( (place = item_find_item(items, num_items, items[i].name)) > -1)

{

printf("Item %s is found at location %d\n", items[i].name, place);

}

else

{

printf("Can't find %s\n", items[i].name);

}

}

}

else

{

printf("ERROR: Unable to load items\n");

}

}

output

If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.


Related Solutions

Hi! I am in an intro level Finance course and I am stuck on this problem....
Hi! I am in an intro level Finance course and I am stuck on this problem. Any help would be greatly appreciated. I am deciding on opening a restaurant. I was able to scrape together some capital from friends and family, but I must pay them back in 4 years at 12% per annum. I figure that it will cost me $165,000 to start up with rent, deposits, equipment, salaries, chicken, basil, rice, etc. for the first year, but I...
I am working on this problem for the company AT & T and am not sure...
I am working on this problem for the company AT & T and am not sure how to start it. Draw a chart of the main inter-organizational linkage mechanisms (e.g., long -term contacts, strategic alliances, mergers) that your organization uses to manage its symbiotic resource interdependencies. Using resource dependence theory and transaction cost theory, discuss why the organization to manage its interdependencies in this way. Do you think the organization has selected the most appropriate linkage mechanisms? Why or why...
Java ArrayList Parking Ticket Simulator, Hello I am stuck on this problem where I am asked...
Java ArrayList Parking Ticket Simulator, Hello I am stuck on this problem where I am asked to calculate the sum of all fines in the policeOfficer class from the arraylist i created. I modified the issueParking ticket method which i bolded at the very end to add each issued Parking ticket to the arrayList, i think thats the right way? if not please let me know. What I dont understand how to do is access the fineAmountInCAD from the arrayList...
This is a question for my problem-solving class. I am really stuck and I can't see...
This is a question for my problem-solving class. I am really stuck and I can't see much of a pattern so I would appreciate if someone could draw out for each thief and explain the pattern to get the answer for 40 thieves! Question: Forty thieves, all different ages, steal a huge pile of identical gold coins and must decide how to divide them up. They settle on the following procedure. The youngest divides the coins among the thieves however...
Hello, I am stuck on this problem....... A histogram of height measurements among middle students would...
Hello, I am stuck on this problem....... A histogram of height measurements among middle students would most likely be: This was a homework question I am confused because there is no data for how many kids or how tall they were. I am taking a guess thinking that the kids are would be normally distributed? Am I correct? Could you please solve? Thank you normally distributed orthogonal skewed left skewed right
What is an equilibrium solution? I am in an Elementary Differential Equations course. The problem is...
What is an equilibrium solution? I am in an Elementary Differential Equations course. The problem is dy/dx = e^y-1; (0,0) and (1,1) and wants the equilibrium solutions. Been a few years since I've taken a math course I'm not finding any good explainations for what it is, thanks.
I almost finished my pre-lab... but I am stuck! What are the energies of (-)-menthone and...
I almost finished my pre-lab... but I am stuck! What are the energies of (-)-menthone and (+)-isomenthone? How can I find this (how to do it)? Thank you for the help!
I just need 3 and 5. I am not sure what I am doing wrong. I...
I just need 3 and 5. I am not sure what I am doing wrong. I get different numbers every time. Superior Markets, Inc., operates three stores in a large metropolitan area. A segmented absorption costing income statement for the company for the last quarter is given below: Superior Markets, Inc. Income Statement For the Quarter Ended September 30 Total North Store South Store East Store Sales $ 4,800,000 $ 960,000 $ 1,920,000 $ 1,920,000 Cost of goods sold 2,640,000...
HI There, there is no solution for a problem that I am looking for. Advanced Accouting...
HI There, there is no solution for a problem that I am looking for. Advanced Accouting - Chapter 4, problem 30. Can you please assist? Thanks. Posada Company acquired 7,000 of the 10,000 outstanding shares of Sabathia Company on January 1, 2013, for $840,000. The subsidiary’s total fair value was assessed at $1,200,000 although its book value on that date was $1,130,000. The $70,000 fair value in excess of Sabathia’s book value was assigned to a patent with a 5-year...
I am stuck on this problem. Comparing three depreciation methods Dexter Industries purchased packaging equipment on...
I am stuck on this problem. Comparing three depreciation methods Dexter Industries purchased packaging equipment on January 8 for $108,000. The equipment was expected to have a useful life of three years, or 21,600 operating hours, and a residual value of $5,400. The equipment was used for 8,640 hours during Year 1, 6,480 hours in Year 2, and 6,480 hours in Year 3. Required: 1. Determine the amount of depreciation expense for the three years ending December 31, by (a)...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT