Searching for a beer should prompt the user for an ID number and the result shou
ID: 3779953 • Letter: S
Question
Searching for a beer should prompt the user for an ID number and the result should display its quantity and price, if it is in your inventory. A view of the entire inventory will display all the beers with their ID number, price and quantity in ascending order by price. This sorting should be done using either Recursive Bubble or Recursive Selection sort. When placing an order an invoice of the order should be printed to the screen. This should be an array of structures in the programming language C
Explanation / Answer
#include<stdio.h>
struct inventory
{
int price;
int quan;
int id;
};
//Print bear details
void printBeerDetails(struct inventory* beers,int n)
{int i;
for ( i = 0; i < 3; ++i)
{
printf("ID of beer %d ",beers[i].id);
printf("Price of beer %d ",beers[i].price);
printf("Ouantity of beer %d ",beers[i].quan);
printf("=================================== ");
}
}
//Search beer
int searchBeer(struct inventory* beers,int id,int n)
{int i;
for ( i = 0; i < n; ++i)
{
if(beers[i].id==id)
{
printf("Do you want to Buy this Beer(yes/no) ");
char ch[200];
scanf("%s",&ch);
if(strcmp(ch,"yes")==0)
{
printf("Enter Quantity to buy " );
int q;
scanf("%d",&q);
beers[i].quan=beers[i].quan-q;
printf("*********Invoice of Beer************* ");
printf("ID of beer %d ",beers[i].id);
printf("Price of beer %d ",beers[i].price*q);
printf("Ouantity of beer buy %d ",q);
printf("Remaining Quantity of beer%d ",beers[i].quan);
printf("=================================== ");
}
return 1;
}
}
printf("Beer not found ");
return 0;
}
//Recursive Selection sort
void selectionSort(struct inventory* beers, int n)
{
int i;
struct inventory temp;
if (n>=1)
{
for (i = 0; i < n; i++)
{
if (beers[i].price<beers[0].price)
{
temp=beers[0];
beers[0]=beers[i];
beers[i]=temp;
}
}
selectionSort(&beers[1],n-1);
}
}
int main(int argc, char const *argv[])
{
struct inventory beers[4];int n=3;
beers[0].price=500;
beers[1].price=200;
beers[2].price=30;
beers[0].quan=10;
beers[1].quan=20;
beers[2].quan=30;
beers[0].id=1;
beers[1].id=2;
beers[2].id=3;
printBeerDetails(beers,3);
printf("Enter ID of beer to search ");
int id;
scanf("%d",&id);
printf("**************After sorting according to price********************* ");
selectionSort(beers,3);
printBeerDetails(beers,3);
searchBeer(beers,id,3);
return 0;
}
========================================================================
Output:
akshay@akshay-Inspiron-3537:~/Chegg$ ./a.out
ID of beer 1
Price of beer 500
Ouantity of beer 10
===================================
ID of beer 2
Price of beer 200
Ouantity of beer 20
===================================
ID of beer 3
Price of beer 30
Ouantity of beer 30
===================================
Enter ID of beer to search
3
**************After sorting according to price*********************
ID of beer 3
Price of beer 30
Ouantity of beer 30
===================================
ID of beer 2
Price of beer 200
Ouantity of beer 20
===================================
ID of beer 1
Price of beer 500
Ouantity of beer 10
===================================
Do you want to Buy this Beer(yes/no)
yes
Enter Quantity to buy
5
*********Invoice of Beer*************
ID of beer 3
Price of beer 150
Ouantity of beer buy 5
Remaining Quantity of beer25
===================================
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.