This program needs to create, sort, and search a novel array. Each novel in the collection will have the following attributes:
Title
Author
Publishing Year
Number of Pages

The novels I will be sorting are in a file called data.txt

I need to use bubble sort to sort the array based on the year in increasing order.

In the program the user is asked to input a year and then binary search needs to be used to find and display all books published in that year.


so far I have this much of the code but I need help with my functions and implementation.

#include<stdio.h>
#include<stdlib.h>

#define BOOKNUM 10

struct book
{
        char title[50];
        char author[50];
        int year;
        int pages;
};

typedef struct book BOOK;

void extractString(char* input, char output[50])
{
   /* i need code here */

}

int extractInt(char* input)
{

    /* i need code here */
}
void showbook(BOOK book)
{

        printf("TITLE: %s \n", book.title);
        printf("AUTHOR: %s \n", book.author);
        printf("YEAR:  %d \n", book.year);
        printf("PAGES:  %d \n", book.pages);
}

void show(BOOK books[BOOKNUM])
{
        int i ;
        for(i=0; i<BOOKNUM; i++)
        {
                printf("----------------------------\n");
                showbook(books[i]);
}
        printf("----------------------------\n");
}
void load(FILE* fp, BOOK books[BOOKNUM])
{
        char value[50]; 
    int count=0;
        while(!feof(fp))
        {
        
                fgets(value, 50, fp);
        
                extractString(value, books[count].title);
                fgets(value, 50, fp);

                extractString(value, books[count].author);
                fgets(value, 50, fp);
        
                books[count].year = extractInt(value);
                fgets(value, 50, fp);
                
                books[count].pages = extractInt(value);
            count++;
        }

}       
        
void bubblesort(BOOK books[BOOKNUM])
{
         
   /* i need code here */
}
int binarysearch(BOOK books[BOOKNUM], int year)
{
  /* i need code here */
}               
                

int main()
{
    BOOK books[BOOKNUM];
int year = 0;
        FILE* fp = fopen("data.txt", "r");
    load(fp, books);
        bubblesort(books);
        printf("after sorting\n");
        show(books);
        printf("Please input Year: "); scanf("%d", &year);

        if(binarysearch(books, year)==0)
                printf("NO SUCH BOOK");
   
        return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.