Please Explain this code Step by Step (How Code Works).   #include #include #include char password[10] = "1234"; struct library { int id; char title[40]; char author[20]; float price; }; //dynamic books struct library *b[100]; int num = 0; void Add() { int count = 0; printf("How many books' info do you want to enter? "); scanf(" %d", &count); for (int i = 0; i < count; i++) { b[num] = (struct library *)malloc(sizeof(struct library)); //local variables int id; char title[40]; char author[20]; float price; printf("Enter the following information about the book:\n"); printf("Enter ID: "); scanf("%d%*c", &id); fflush(stdin); fflush(stdin); printf("Enter title: "); scanf("%[^\n]%*c", title); printf("Enter author's name: "); scanf("%[^\n]%*c", author); printf("Enter price(in Tk): "); scanf("%f", &price); //check if information exists already for (int i = 0; i < num; i++) { if (b[i]->id == id) { printf("Book with id %d already present in the library", id); return; } } b[num]->id = id; strcpy(b[num]->title, title); strcpy(b[num]->author, author); b[num]->price = price; num++; } } void Disp() { printf("\tID\tName\tAuthor\tPrice(Tk)\n"); for (int i = 0; i < num; i++) { printf("\t%d\t%s\t%s\t%f\n", b[i]->id, b[i]->title, b[i]->author, b[i]->price); } }

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Please Explain this code Step by Step (How Code Works).

 

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

char password[10] = "1234";
struct library
{
int id;
char title[40];
char author[20];
float price;
};

//dynamic books
struct library *b[100];
int num = 0;

void Add()
{
int count = 0;
printf("How many books' info do you want to enter? ");
scanf(" %d", &count);

for (int i = 0; i < count; i++)
{
b[num] = (struct library *)malloc(sizeof(struct library));

//local variables
int id;
char title[40];
char author[20];
float price;

printf("Enter the following information about the book:\n");
printf("Enter ID: ");
scanf("%d%*c", &id);
fflush(stdin);
fflush(stdin);

printf("Enter title: ");
scanf("%[^\n]%*c", title);

printf("Enter author's name: ");
scanf("%[^\n]%*c", author);

printf("Enter price(in Tk): ");
scanf("%f", &price);

//check if information exists already
for (int i = 0; i < num; i++)
{
if (b[i]->id == id)
{
printf("Book with id %d already present in the library", id);
return;
}
}
b[num]->id = id;
strcpy(b[num]->title, title);
strcpy(b[num]->author, author);
b[num]->price = price;

num++;
}
}

void Disp()
{

printf("\tID\tName\tAuthor\tPrice(Tk)\n");

for (int i = 0; i < num; i++)
{
printf("\t%d\t%s\t%s\t%f\n", b[i]->id, b[i]->title, b[i]->author, b[i]->price);
}
}

void Count()
{

printf("\nNo of books avalable in the library = %d\n", num);
}

void List()
{

char str[20];

printf("Enter the author's name: ");

scanf("%s", str);

for (int i = 0; i < num; i++)
{

if (strcmp(str, b[i]->author) == 0)

printf("\n\t%d\t%s\t%s\t%f\n", b[i]->id, b[i]->title, b[i]->author, b[i]->price);
}
}

int validateLibrarion()
{
char passwd[10];
printf("Enter password: ");
scanf("%s", passwd);

if (strcmp(passwd, password) == 0)
{
printf("\nPassword Authentication successful..\n");
return 1;
}
else
{
printf("Incorrect password....\n");
return 0;
}
}

int main()
{

int option = 0;

do
{

printf("\n\nWelcome to the library\nPlease Select an Option: \n");
printf("-----------------------------------------------------------\n");
printf("1.Add book details\n2.Display book details\n3.List all books of a given author\n4.Show total no. of books in the library.\n5.Exit\n");
printf("-----------------------------------------------------------\n");

scanf("%d", &option);

switch (option)
{

case 1:
if (validateLibrarion())
Add();

break;

case 2:
Disp();

break;

case 3:
List();

break;

case 4:
Count();

break;
}

} while (option != 5);

return 0;
}

Expert Solution
steps

Step by step

Solved in 10 steps with 2 images

Blurred answer
Knowledge Booster
Array
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education