Share
Explore BrainMass

This C++ program reads in information about a list of books

The attached is a 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 <stdio.h>
#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("nn%10s%10s", "CODE", "PRICE");
printf("n");
for (index = 0; index < numOfBooks; index++)
{
printf("%10d%10.2fn", list[index].code, list[index].price);
}
}

Attachments

Solution Summary

The expert examines C++ program reading information about a list of books.

$2.19