In: Computer Science
I need write a small "c" program that has locations of items on a shelving unit. Im not familiar with this language. Can you guys give me some hint so I could start writing one or as example. This is my first time learning programming C.
Specifically, your program should:
Solution(Code):
#include<stdio.h>
#include<string.h>
struct item
{
char name[20];
int price;
};
int main()
{
int nsl,ns,i,j,price,shelf,slot;
char choice,name[20],dummy;
printf("Enter number of shelves in the unit:");
scanf("%d",&ns);
printf("Enter number of slots for each shelf:");
scanf("%d",&nsl);
struct item items[ns][nsl];
for(i=0;i<ns;i++)
{
for(j=0;j<nsl;j++){
strcpy(items[i][j].name,"");
items[i][j].price=-1;
}
}
printf("Enter -1 for name to stop adding
items\n");
while(1)
{
scanf("%s%d%d%d",name,&price,&shelf,&slot);
scanf("%c",&dummy);
if(strcmp(name,"-1")==0)
break;
strcpy(items[shelf-1][slot-1].name,name);
items[shelf-1][slot-1].price=price;
}
printf("Enter the co-ordinate of an item in the
shelving unit:");
scanf("%d%d",&shelf,&slot);
if(items[shelf-1][slot-1].price!=-1)
printf("Item details\nItem
name:%s\nItem
price:%d\n",items[shelf-1][slot-1].name,items[shelf-1][slot-1].price);
else
printf("Empty slot\n");
return 0;
}
Output: