A: Structure: Book's Title Price (RM) Quantity Sold
A: Structure: Book's Title Price (RM) Quantity Sold
Q1 Table 12.1 shows a list of best seller books in a book store. Write a program to
calculate the total price for all the books sold. If the quantity sold is greater than or equal to
40 copies, a discount of 10% will be deducted from the books price. Display books title,
price, quantity, total price, discounted amount and price after discount for each book, and
finally calculate the total of amount payable to the books supplier.
Requirement:
(a) Define and declare struct bookType with title (char), price (float), qty (int)
and total (float) as members.
(b) Declare an array named discount to store calculated discount amount.
(c) Declare an array named after_discount to store calculated total amount
after discount.
(d) Declare an array of structure bookType named book to store all the
information for five (5) books.
Sample Output:
The following program creates a sequential file named student.txt. The program will get information from
keyboard and write the information into the file student.txt.
#include<stdio.h>
int main ( )
{
long int MatrixID;
char name[30];
float cgpa;
if ((cfPtr = fopen("student.txt", "w")) == NULL) //create and open file for writing
printf("\nFile can't be opened");
else
{
printf("Enter Matrix No, Name and CGPA\n");
printf("Enter EOF to end input\n");
printf(": ");
scanf("%ld %s %f", &MatrixID, name, &cgpa);
stdin - is a standard input stream. A system file pointer for keyboards input stream.
stdout - is a standard output stream. A system file pointer for screens output stream.
stderr - is an error output stream.
student.txt - is a text file that is created and saved in the hard disk. Find the file student.txt in the
same directory of your program run. Open the file and you will see the data that you
have keyed in.
Type information below and check the student.txt, the file should contain below information.
90812345 Ahmad 3.5
90854321 Salina 3.9
90823456 Lee 3.75
90865432 Jason 3.3
a. The following program creates a random access file. This program creates 10 blank records to a file
named credit.dat. Type, compile and run the following program.
#include <stdio.h>
int main( )
{
int i;
b. The previous program in (a) illustrates how to write data sequentially to a random access file, now, the
following programs in (b) and (c) show how to write data randomly to a random access file and how to
read data from the file. Type, compile and run both programs.
#include <stdio.h>
struct clientData
{
int acctNum;
char lastName[15];
char firstName[15];
float balance;
};
int main ( )
{
FILE *cfPtr;
c. The following program shows how to read data from a random access file.
#include <stdio.h>
struct clientData
{
int acctNum;
char lastName[15];
char firstName[15];
float balance;
};
int main ( )
{
FILE *cfPtr;
Compare the data on the screen. Is the data displayed same as your input data?