CS1101C Lab 1

The deadline for this lab is Friday 05 September 2008, 23:59:59 hours.

For all of these questions, you are not allowed to use the following keywords in your solution: if, else, switch, while, do, for, and the ?: operator. No credit will be given if this rule is violated.

Question 1 (Sphere Volume Calculator)

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

Write a simple program that asks the user to enter the radius of a sphere in meters, and calculates the volume of the sphere. Display the result correct to three decimal places.

Sample Runs

Assuming that the name of the executable file is lab1q1, sample run(s) of the program are shown below. User input is denoted in bold.

$ ./lab1q1

Enter radius of sphere in m: 5.67
The volume is: 763.551 m^3

$

Question 2 (Restaurant Bill Calculator)

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

Assuming that the name of the executable file is lab1q2, sample run(s) of the program are shown below. User input is denoted in bold.

Sample Runs

$ ./lab1q2

Enter restaurant price before taxes: $100

Price before Taxes: $ 100.00
10% Service Charge: $  10.00
                    --------
Sub-Total:          $ 110.00
                    --------
7% GST on $ 110.00: $   7.70
                    --------
Price after Taxes:  $ 117.70
                    --------

$ ./lab1q2

Enter restaurant price before taxes: $2345.67

Price before Taxes: $2345.67
10% Service Charge: $ 234.57
                    --------
Sub-Total:          $2580.24
                    --------
7% GST on $2580.24: $ 180.62
                    --------
Price after Taxes:  $2760.85
                    --------

$
Deduce as much as you can about the program and what it does from the sample runs above. Then, write a program called lab1q2.c that follows the sample runs precisely.

Hint: To print the % symbol, use %% in the printf format string.

Question 3 (Crossfire Silver Conversion)

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

Crossfire is a free multiplayer adventure game. In this game, 1 platinum = 5 gold = 10 silver. Write a simple program that converts x amount of silver coins to the equivalent amount of platinum, gold, and silver coins. The equivalent amount should have as few coins as possible.

Assuming that the name of the executable file is lab1q3, sample run(s) of the program are shown below. User input is denoted in bold.

Sample Runs

$ ./lab1q3

Enter number of silver coins: 5
5 silver coin(s) is equivalent to:
   0 platinum coin(s)
   2 gold coin(s)
   1 silver coin(s)

$ ./lab1q3

Enter number of silver coins: 197
197 silver coin(s) is equivalent to:
  19 platinum coin(s)
   3 gold coin(s)
   1 silver coin(s)

$

Note and Ponder

  1. Assume that all user input is valid.

  2. 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.

  3. Most importantly, have lots of fun programming!


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

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

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