modify the following code so that 1)add a function that ask the user to remove a
ID: 3766632 • Letter: M
Question
modify the following code so that
1)add a function that ask the user to remove a book. Remove a book from the library accodrding to its title.
2)Print all the books sorted from the oldest (publishing year) to the newest if the user asks for it .
#include<stdio.h>
#include<conio.h>
struct book
{
char Title[20];
int Year;
char Author[20];
};
struct book b[5];
void ins(int n)
{
printf("enter book Title");
scanf("%s",&b[n].Title);
printf("enter book Author");
scanf("%s",&b[n].Author);
printf("enter book Year");
scanf("%s",&b[n].Year);
printf("Inserted");
}
void print(int n)
{
int i;
for(i=0;i<n;i++)
{
printf("%s",b[i].Title);
printf("%s",b[i].Author);
printf("%d",b[i].Year);
}
}
void main()
{
//struct book b[5];
int a,i=0;
while(1)
{
clrscr();
printf("1. Insertion");
printf(" 2. Print");
printf(" 3. Exit");
scanf("%d",&a);
switch(a)
{
case 1:
ins (i);
i++;
break;
case 2:
print(i);
break;
case 3:
return;
}
}
}
Explanation / Answer
/* program to implement the above 2 statements using fuction declaration or protoype */
#include<stdio.h>
#include<conio.h>
int create(void);
int display(void);
int asort(void);
int del(void);
int n,i,item,pos=0;
struct book
{
char title[22];
int year;
char author[22];
};
struct book b[4];
void main()
{
int ch ;
clrscr();
while(1)
{
printf(" MENU ");
printf(" 1.create ");
printf(" 2.display");
printf(" 3.asort ");
printf(" 4.delete ");
printf(" 5.quit ");
printf(" Enter u r choice");
scanf("%d",&ch);
switch(ch)
{
case 1: create(); break;
case 2: display(); break;
case 3: asort(); break;
case 4: del(); break;
case 5: exit(1); break;
default:printf(" u have entered wrong choice");
break;
}
}
}
int create()
{
printf("enter the number of record:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf(" enter book title:");
scanf("%s",&b[i].title);
printf(" enter year:");
scanf("%d",&b[i].year);
printf(" enter author:");
scanf("%s",&b[i].author);
}
return n;
}
int display()
{
if(n<=0)
{
printf(" List is empty:");
return n;
}
printf(" book details:");
for(i=0;i<n;i++)
{
printf(" %s :",b[i].title);
printf(" %d :",b[i].year);
printf(" %s :",b[i].author);
}
return n;
}
int asort()
{
int j,temp;
if(n<=0)
{
printf(" List is empty:");
return n;
}
for(i=0;i<n;i++)
{
for(j=0;j<n-1;j++)
{
if(b[j].year > b[j+1].year)
{
temp=b[j].year;
b[j].year=b[j+1].year;
b[j+1].year=temp;
}
}
}
printf("the books are sorted in the ascending order");
return n;
}
int del()
{
if(n<=0)
{
printf(" List is empty:");
return n;
}
printf(" enter the title to be deleted:");
scanf("%s",b[i].title);
for(i=0;i<n;i++)
{
if(b[i].title==b[i].title)
{
pos=i+1;
break;
}
}
if(pos==0)
{
printf(" title not found");
return n;
}
for(i=pos-1;i<n-1;i++)
a[i]=a[i+1];
n--;
return n;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.