Skip to content

luc083.c

Problem Statement

Library menu-driven program (Add, Display, List by Author, List by Title, Count, List sorted).

Metadata

Property Detail
Author Amit Dutta amitdutta4255@gmail.com
Date 08 Feb 2026
License MIT License (See the LICENSE file for details)
Difficulty Advanced (index: 6 / 10)

Concepts

Beta Feature

This concept detection system is still in beta and may occasionally show incorrect or incomplete results.

  • Array
  • Pointers
  • Iteration
  • Sorting (possible)
  • Recursion

Actions

Raw View on GitHub

You can print or save this file by opening Raw and using your browser.

Source Code

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

struct library
{
    int acc_no;
    char title[50];
    char author[50];
    float price;
    int is_issued; // 1 = Yes, 0 = No
};

void add_book(struct library *lib, int *count);
void display_books(struct library *lib, int count);
void list_by_author(struct library *lib, int count);
void list_title_by_acc(struct library *lib, int count);
void sort_by_acc(struct library *lib, int count);

int main()
{
    struct library books[100];
    int count = 0;
    int choice;

    while (1)
    {
        printf("\n--- Library Menu ---\n");
        printf("1. Add Book Info\n");
        printf("2. Display Book Info\n");
        printf("3. List books of given author\n");
        printf("4. List title of specified accession number\n");
        printf("5. List count of books\n");
        printf("6. List books in order of accession number\n");
        printf("7. Exit\n");
        printf("Enter choice: ");
        scanf("%d", &choice);

        switch (choice)
        {
        case 1: add_book(books, &count); break;
        case 2: display_books(books, count); break;
        case 3: list_by_author(books, count); break;
        case 4: list_title_by_acc(books, count); break;
        case 5: printf("Total books in library: %d\n", count); break;
        case 6: sort_by_acc(books, count); display_books(books, count); break;
        case 7: exit(0);
        default: printf("Invalid choice!\n");
        }
    }
    return 0;
}

void add_book(struct library *lib, int *count)
{
    printf("Enter Accession No, Title, Author, Price, Issued(1/0):\n");
    scanf("%d", &lib[*count].acc_no);
    scanf("%s", lib[*count].title); // Using %s for simplicity (no spaces)
    scanf("%s", lib[*count].author);
    scanf("%f", &lib[*count].price);
    scanf("%d", &lib[*count].is_issued);
    (*count)++;
}

void display_books(struct library *lib, int count)
{
    int i;
    for (i = 0; i < count; i++)
        printf("%d: %s by %s ($%.2f) Issued: %d\n", 
               lib[i].acc_no, lib[i].title, lib[i].author, lib[i].price, lib[i].is_issued);
}

void list_by_author(struct library *lib, int count)
{
    char author[50];
    int i, found = 0;
    printf("Enter author name: ");
    scanf("%s", author);
    for (i = 0; i < count; i++)
    {
        if (strcmp(lib[i].author, author) == 0)
        {
            printf("%s\n", lib[i].title);
            found = 1;
        }
    }
    if (!found) printf("No books found.\n");
}

void list_title_by_acc(struct library *lib, int count)
{
    int acc, i;
    printf("Enter Accession No: ");
    scanf("%d", &acc);
    for (i = 0; i < count; i++)
    {
        if (lib[i].acc_no == acc)
        {
            printf("Title: %s\n", lib[i].title);
            return;
        }
    }
    printf("Book not found.\n");
}

void sort_by_acc(struct library *lib, int count)
{
    struct library temp;
    int i, j;
    for (i = 0; i < count - 1; i++)
    {
        for (j = 0; j < count - i - 1; j++)
        {
            if (lib[j].acc_no > lib[j + 1].acc_no)
            {
                temp = lib[j];
                lib[j] = lib[j + 1];
                lib[j + 1] = temp;
            }
        }
    }
}

Explanation

Explain with AI

Copy the prompt below and paste it into any AI assistant.

    You are explaining a C programming code to a beginner.

    STRICT RULES:

    - Only use the given code. Do NOT assume anything not present.

    - Do NOT add extra examples.

    - Keep explanation clear and short.

    - If something is unclear, say "Not clear from code".

    - Follow the exact format below. Do NOT change headings.

    FORMAT:

    [START]

    ## What it does

    (Explain the overall purpose in 1-2 sentences)

    ## Step-by-step

    (Explain how the code works in steps, simple language)

    ## Key Concepts

    (List concepts like loop, condition, function, etc.)

    ## Notes

    (Mention any limitations, errors, or assumptions)

    [END]

    CODE (luc083.c):

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

    struct library
    {
        int acc_no;
        char title[50];
        char author[50];
        float price;
        int is_issued; // 1 = Yes, 0 = No
    };

    void add_book(struct library *lib, int *count);
    void display_books(struct library *lib, int count);
    void list_by_author(struct library *lib, int count);
    void list_title_by_acc(struct library *lib, int count);
    void sort_by_acc(struct library *lib, int count);

    int main()
    {
        struct library books[100];
        int count = 0;
        int choice;

        while (1)
        {
            printf("\n--- Library Menu ---\n");
            printf("1. Add Book Info\n");
            printf("2. Display Book Info\n");
            printf("3. List books of given author\n");
            printf("4. List title of specified accession number\n");
            printf("5. List count of books\n");
            printf("6. List books in order of accession number\n");
            printf("7. Exit\n");
            printf("Enter choice: ");
            scanf("%d", &choice);

            switch (choice)
            {
            case 1: add_book(books, &count); break;
            case 2: display_books(books, count); break;
            case 3: list_by_author(books, count); break;
            case 4: list_title_by_acc(books, count); break;
            case 5: printf("Total books in library: %d\n", count); break;
            case 6: sort_by_acc(books, count); display_books(books, count); break;
            case 7: exit(0);
            default: printf("Invalid choice!\n");
            }
        }
        return 0;
    }

    void add_book(struct library *lib, int *count)
    {
        printf("Enter Accession No, Title, Author, Price, Issued(1/0):\n");
        scanf("%d", &lib[*count].acc_no);
        scanf("%s", lib[*count].title); // Using %s for simplicity (no spaces)
        scanf("%s", lib[*count].author);
        scanf("%f", &lib[*count].price);

    ... (truncated for brevity)