Answer to Question #194959 in C++ for haseena

Question #194959

Create a library management system in which you have a pile of 4 books stacked over one another. Each book has a book number. Make a program in which a librarian stacks a pile of books over one another. A student wants to issue a book. The program first searches if the book is present in the stack or not. If the book has not been issued, the program finds out the index of the book. It also pops other books on the top of it to issue the book to the student. Display the books in the stack. (Hint: report the count of the books above the required book and use the pop() function “count” times.)


1
Expert's answer
2021-05-18T13:01:38-0400
#include <iostream>
using namespace std;
class Library{
    int *books;
    int size;
    public:
    Library(){
        size = 4;
        books = new int[size];
        for(int i = 0; i < 4; i++) books[i] = i + 1;
    }
    bool find(int n){
        for(int i = 0; i < size; i++) if(books[i] == n) return true;
        cout<<"\nBook "<<n<<" not found\n";
        return false;
    }
    void pop(int n){
        if(find(n)){
            cout<<"\nBook found\n";
            int temp;
            for(int i = 0; i < size; i++){
                if(books[i] == n){
                    temp = books[i];
                    books[i] = books[size - 1];
                    books[size - 1] = temp;
                }
            }
            cout<<"\nBook "<<n<<" issued";
            books = (int*)realloc(books, (--size) * sizeof(int));
        }
    }
    void display(){
        cout<<"\nBooks present\n";
        for(int i = 0; i < size; i++) cout<<books[i]<<endl;
    }
};
int main(){
    Library library;
    library.display();
    library.pop(1);
    library.display();
    library.pop(1);


    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog