IN c language please, here is the code for number one, I\'m having real trouble
ID: 3820345 • Letter: I
Question
IN c language please,
here is the code for number one, I'm having real trouble to modify this code, please I need help.
#include <stdio.h>
#include <stdlib.h>
int i=0,k=0;
struct library
{
char title[80];
char author[30];
int year;
int price;
};
void insert(struct library[]);
void display(struct library[]);
int main()
{
struct library data[20];
int choice;
while(1)
{
printf(" ******************** ");
printf("Press 1 Insertion ");
printf("Press 2 Display ");
printf("Press 3 Exit ");
printf(" Enter choice(1-3) : ");
printf(" ******************** ");
scanf("%d", &choice);
switch (choice)
{
case 1:
insert(data);
break;
case 2:
display(data);
break;
case 3:
exit(0);
}
}
return 0;
}
void insert(struct library list[80])
{
{
printf(" Enter data for Record #%d ", i + 1);
printf(" Enter title : ");
fflush(stdin);
gets(list[i].title);
printf("Enter author : ");
gets(list[i].author);
printf("Enter year : ");
scanf("%d", &list[i].year);
printf("Enter price : ");
scanf("%d", &list[i].price);
k=i;
i=i+1;
}
printf("A new record was created");
}
void display(struct library list[80])
{
int y;
printf(" title author year price ");
for (y = 0; y <= k; y++)
{
printf("%s %s %d %d ", list[y].title, list[y].author, list[y].year, list[y].price);
}
}
Thank You
pring 2017 Spring 2017 Exercise 1: Modify the library database system that you created in Lab 2 so as to have the following functionalities: Remove a book. Remove a book from the library according to its title. Print all the books sorted form the oldest (publishing year to the newest if the users asks for it. Exercise 2: Write a function incList that takes as an argument a linked list of integers and creates as a result another list with the same length and each element has its values increased by 1. Example: L [1, 2, 3, 4, 5] incList(L) [2, 3, 4, 5, 6]Explanation / Answer
Answer to your first question:
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
int i=0,k=0;
struct library
{
char title[80];
char author[30];
int year;
int price;
};
void insert(struct library[]);
void display(struct library[]);
void removeBook(struct library[]);
void printSorted(struct library[]);
int main()
{
struct library data[20];
int choice;
while(1)
{
printf(" ******************** ");
printf("Press 1 Insertion ");
printf("Press 2 Display ");
printf("Press 3 Remove book ");
printf("Press 4 Sort the books according to published date ");
printf("Press 5 Exit ");
printf(" Enter choice(1-3) : ");
printf(" ******************** ");
scanf("%d", &choice);
switch (choice)
{
case 1:
insert(data);
break;
case 2:
display(data);
break;
case 3:
removeBook(data);
break;
case 4:
printSorted(data);
case 5:
exit(0);
}
}
return 0;
}
void insert(struct library list[80])
{
{
printf(" Enter data for Record #%d ", i + 1);
printf(" Enter title : ");
scanf("%s",list[i].title);
printf("Enter author : ");
scanf("%s",list[i].author);
printf("Enter year : ");
scanf("%d", &list[i].year);
printf("Enter price : ");
scanf("%d", &list[i].price);
k=i;
i=i+1;
}
printf("A new record was created");
}
void display(struct library list[80])
{
int y;
printf(" title author year price ");
for (y = 0; y <= k; y++)
{
printf("%s %s %d %d ", list[y].title, list[y].author, list[y].year, list[y].price);
}
}
void removeBook(struct library lib[])
{
int itr = 0, j = 0, flag = 0;
char title[80] = {''};
printf("Enter the title of the book you want to delete: ");
scanf("%s",title);
for(itr=0 ; itr <= k ;itr++){
if(!strcasecmp(lib[itr].title,title)){
for(j=itr; j < k ;j++){
snprintf(lib[j].title,sizeof(lib[j].title), lib[j+1].title);
snprintf(lib[j].author,sizeof(lib[j].author), lib[j+1].author);
lib[j].year = lib[j+1].year;
lib[j].price = lib[j+1].price;
}
i--;
k--;
printf("Book deleted successfully ");
flag = 1;
break;
}
}
if(!flag){
printf("Book not found in library ");
}
}
void printSorted(struct library lib[])
{
int itr =0 , j =0 , sortedIndex[80][2] = {0};
int earliest = 0 , year = 0, index = 0;
for(itr=0; itr <= k;itr++){
sortedIndex[itr][0] = itr;
sortedIndex[itr][1] = lib[itr].year;
//printf("%d %d ",sortedIndex[itr][0], sortedIndex[itr][1]);
}
for(itr=0; itr <= k;itr++){
earliest = sortedIndex[itr][1];
for(j=itr+1;j<=k;j++){
if(sortedIndex[j][1] < earliest){
earliest = sortedIndex[j][1];
year = earliest;
index = sortedIndex[j][0];
sortedIndex[j][0] = sortedIndex[itr][0];
sortedIndex[j][1] = sortedIndex[itr][1];
sortedIndex[itr][0] = index;
sortedIndex[itr][1] = year;
}
}
}
for(itr=0; itr<=k ;itr++){
// printf("%d %d ",sortedIndex[itr][0], sortedIndex[itr][1]);
printf("<%s> <%d> ",lib[sortedIndex[itr][0]].title, sortedIndex[itr][1]);
}
}
Answer to your second question:
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
int value;
struct node* next;
}Node;
Node* incList(Node* list)
{
Node* itr = list, *temp = NULL, *newList = NULL, *p = NULL;
if(incList == NULL){
printf("List is empty ");
return NULL;
}
while(itr != NULL){
temp = (Node*)malloc(sizeof(Node));
temp->value = itr->value + 1;
temp->next = NULL;
if(newList == NULL){
newList = p = temp;
}
else{
p->next = temp;
p = p->next;
}
itr = itr->next;
}
return newList;
}