/****************************************************** * CP-I Warmup 1999-2000 * Problem #10 *****************************************************/ /* * Question: * * Write a program to declare an array of structures to contain * the details of 10 books. The inforamtion of the book number, * the title of the book and the name of the author(s) should be * made available. If the user enters the book number, other details * of the corresponding book should be displayed. */ #include <stdio.h> #define NO_OF_BOOKS 10 #define TITLE_LENGTH 40 #define AUTHORS_LENGTH 60 struct book{ int number; char title[TITLE_LENGTH]; char authors[AUTHORS_LENGTH]; }; struct book books[NO_OF_BOOKS]; int no_of_books; main() { int i,opt,b_num; printf("Give the no. of books: "); scanf("%d",&no_of_books); if(no_of_books>NO_OF_BOOKS) { printf("The program can not hold %d books\n",no_of_books); printf("It should be less than %d\n",NO_OF_BOOKS); exit(0); } for(i=0;i<no_of_books;i++) { printf("Give the Details of %d book: \n",i+1); printf("Book Number: "); scanf("%d",&books[i].number); getchar(); printf("Title: "); scanf("%[^\n]",books[i].title); getchar(); printf("Authors: "); scanf("%[^\n]",books[i].authors); getchar(); } printf("\n\n\n\n\n\n\n\n\n"); while(1) { printf("Give 1 to Search(default); 0 to Quit: "); scanf("%d",&opt); printf("\n"); if(opt==0) break; printf("Enter the Book Number: "); scanf("%d",&b_num); printf("\n"); opt=search(b_num); if(opt==-1) printf("No book is found with the number : %d\n",b_num); else { printf("\n\n\n"); printf("Book Number: %d\n",b_num); printf("Title: %s\n",books[opt].title); printf("Author(s): %s\n",books[opt].authors); printf("\n\n\n"); } } } int search(int num) { int i; for(i=0;i<no_of_books;i++) if(books[i].number==num) return i; return -1; }