CS1101C Lab 5

The deadline for this lab is Friday 10 October 2008, 23:59:59 hours.

For all of these questions, you can choose whether to create your own functions in your solution. However, no extra credit will be given if functions are used.

Software Failure Analysis

The owner of "Seng Moi Huat" provision store (let's call him Ah Seng) runs a thriving business in the heartlands of Singapore. The reason behind the success of his business is due to the fact that the store charges customers to the nearest dollar, thus absorbing all coin denominations. He reasons that for every customer, the amount of losses incurred can only be between 0 to 99 cents. So at the end of a typical day, his losses will come to less than a dollar per customer. Moreover, customers may purchase more goods in order to take advantage of the pricing scheme, so it is a win-win situation for both the customers and store owner.

Though Ah Seng sees more and more customers frequenting his store everyday (which he says is probably due to the good name of the store), he feels that he is making losses, and suspects that his store assistants are helping themselves to the till. He has since installed security cameras in the store, but have so far found no suspicious activity behind the cash register.

Ah Seng is getting desperate and the last thing he suspects is the in-house retail software. He has engaged you, the software failure analyst, to help verify and validate the workings of the system. He is a demanding person and wants the problem rectified in three days time (by Friday, 23:59:59 to be exact), and is prepared to pay you $10,000.

Day #1: Understand the system

Ah Seng has given you a copy of the program. The first thing you need to do is to try out the program.

You will realize that the system does not keep a log of the customer transactions. Your first task is to update the system to include the transaction history which will be output to a log file. The log file will include the specific quantity and amounts of different items, and also some important data values. As an example, consider the following transaction:

New Customer? (1 for yes; 0 for no): 1

Enter quantity and amount (-1 -1 to quit): 1 1.50
Enter quantity and amount (-1 -1 to quit): 2 3.30
Enter quantity and amount (-1 -1 to quit): -1 -1
The total purchase is : $8.

New Customer? (1 for yes; 0 for no): 1

Enter quantity and amount (-1 -1 to quit): 1 4.60
Enter quantity and amount (-1 -1 to quit): -1 -1
The total purchase is : $4.

New Customer? (1 for yes; 0 for no): 1

Enter quantity and amount (-1 -1 to quit): 3 2.50
Enter quantity and amount (-1 -1 to quit): 1 12.50
Enter quantity and amount (-1 -1 to quit): 2 0.50
Enter quantity and amount (-1 -1 to quit): -1 -1
The total purchase is : $21.

New Customer? (1 for yes; 0 for no): 0
The transactions log file (log.out) will be

1 1.50 2 3.30 -1 -1
8 8.10 0.10

1 4.60 -1 -1
4 4.60 0.60

3 2.50 1 12.50 2 0.50 -1 -1
21 21.00 0.00
The details of every customer transaction will constitute two lines in the log file.

The name of your C program file must be called lab5q1.c, files with any other name will not be marked.

Day #2: Logging the transactions history

On the second day, many customers came to patronize the shop, and the store cashier was seen busy punching the keys of the cash register. Unknowingly to him, the entries made by the cashier is secretly logged. The log file is given to you as key.out. If you observe the file closely, the values are exactly what would have been entered by the cashier when using the system. Note that the final line is a 0 to signify the end of the day. Using this log file, you can simulate the data entry from the cashier by running your program this way.
$ gcc -Wall lab5q1.c -o lab5q1
$ ./lab5q1 < key.out
Expect to see a huge chunk of printout when executing the program. You can ignore these, as they are the merely the printf statements of your program when running. If you are using sunfire, you can execute the program in the following way.
$./lab5q1 < key.out > /dev/null
Now inspect the log.out file generated by your program and check for inconsistencies. Pay particular attention to the losses made. Remember that the losses should be between 0 to 99 cents.

Day #3: Debug the program

You will find that the losses for some transactions (such as the last one) could be up to a dollar. There is an obvious bug in the program. Your task now is to rectify the program of lab5q1.c to handle this anomaly.

As a guide, here is the correct log that you must generate as the new log.out

The name of your C program file must be called lab5q2.c, files with any other name will not be marked.

Note and Ponder

  1. Assume that all user input is valid.

  2. Assume that the total cost of purchase per customer will never exceed $9999.99.

  3. If your program does not work as you expect (logical errors), use extra printf statements to print out all the values of your variables to aid in your debugging.

  4. Don't forget to include the header comments in the programs you submit.

  5. Most importantly, have lots of fun programming!


This document, index.html, has been accessed 32 times since 25-Jun-24 11:57:13 +08. This is the 2nd time it has been accessed today.

A total of 23 different hosts have accessed this document in the last 444 days; your host, nsrp-source.comp.nus.edu.sg, has accessed it 10 times.

If you're interested, complete statistics for this document are also available, including breakdowns by top-level domain, host name, and date.