Create a code for information about a list of books


Discussion:

Q: C program which reads in information about a list of books from the keyboard, stores the information in an array of structure, and finally displays it on the screen. The information about a book includes its code (an integer value) and price (a floating point value). Code 9999 serves as a sentinel value indicating the end of input. A sample input and output are shown below:

Sample Input
Code of Book ? 7731
Price of Book ? 12.89
Code of Book ? 2175
Price of Book ? 8.50
Code of Book ? 9999
Sample Output
CODE PRICE
7731 12.89
2173 8.50

In this project, you should add more functions to this program while preserving its organization. Consider the following functions:

Count the number of books that cost less than $10.

Sort all books by Code (You may modify the selection sort function shown in Fig 2.2 on P. 31 accordingly.)

Search the list for a book given its code.

In your main program, add statements using the three functions and showing the results on the screen.
/***************************************************************/
/* Author:
/* Course No/Section:
/* Date:
/* File Name:
/* Project No:
/* Description:
/****************************************************************/
#include
#define MAX 100
struct Book {
int code;
float price;
};
void loadArray (struct Book list[], int* numOfBooksPtr);
void printArray (struct Book list[], int numOfBooks);
main()
{
int numOfBooks;
struct Book list[MAX];
loadArray (list, &numOfBooks);
printArray (list, numOfBooks);
return 0;
}
void loadArray (struct Book list[], int* numOfBooksPtr)
/* loads array list with input data from keyboard */
{
int aCode;
float aPrice;
*numOfBooksPtr = 0;
printf ("Code of Book ? ");
scanf("%d", &aCode);
while ( aCode != 9999 )
{
printf ("Price of Book ? ");
scanf("%f", &aPrice);
list[*numOfBooksPtr].code = aCode;
list[*numOfBooksPtr].price = aPrice;
(*numOfBooksPtr)++;
printf ("Code of Book ? ");
scanf("%d", &aCode);
}
}
void printArray (struct Book list[], int numOfBooks)
/* display contents of array list on screen */
{
int index;
printf("\n\n%10s%10s", "CODE", "PRICE");
printf("\n");
for (index = 0; index < numOfBooks; index++)
{
printf("%10d%10.2f\n", list[index].code, list[index].price);
}
}

Solution Preview :

Prepared by a verified Expert
C/C++ Programming: Create a code for information about a list of books
Reference No:- TGS01931933

Now Priced at $20 (50% Discount)

Recommended (91%)

Rated (4.3/5)