Assignment 1
Deadline
14 September, 2018 (Friday), 6:00pm.
Prerequisite
- You are able to access the CS1010 programming environment.
- You are familiar with basic UNIX CLI and using terminal-based editor
vim
. - You have gone through Exercise 1 and have already setup your
.gitconfig
. - You are familiar with the steps of accepting, downloading, and submitting your exercise and assignment.
- You are familiar with the directory structure and the common files included in your assignment skeleton.
Learning Outcomes
- Be comfortable writing simple C programs that involves arithmetic operations,
long
,double
, andbool
types, and conditionalif
/else
statements. - Be comfortable breaking down a problem into smaller subproblems which can be resolved using functions, including reusing existing functions written for other programs (with a tweak), writing a function that calls itself, designing what should the inputs and return values/types of a function be.
Setup
- Click on this link to accept the assignment.
- Login to one of the hosts of CS1010 programming environment
- Run:
~cs1010/get-as01
- You should see the folder
as01-<github id>
in your home directory with the assignment skeleton inside.
Solving The Assignments
- Edit the files
invest.c
,box.c
,digits.c
, andtaxi.c
to solve the corresponding question as described below. - To compile and run tests with the sample inputs and outputs:
make
- The test cases are given in the
inputs
andoutputs
subdirectory. You can usecat
orless
to look at the content of these test cases. You can add more test cases or edit the given ones if needed.
Submission
When you are ready, run the following command to submit:
~cs1010/submit-as01
The four files invest.c
, box.c
, digits.c
, and taxi.c
will be uploaded to GitHub. You can submit multiple times, but only the last submission will be graded.
Identifying Yourself
In every C file that you submit to CS1010, you need to identify yourself by writing your name and tutorial group. Marks will be deducted if you fail to do so. You need to edit the line:
@author XXXX (Group YYYY)
and change it to something like:
@author Hermione Granger (Group 9)
Grading
This assignment contributes towards 3% of your final grade. The total marks for this assignment is 30. For Programming Assignment 1, the sole criteria for grading is correctness.
Question 1: Invest (5 marks)
If you invest m dollars at r% interest rate compounded annually, after n years, your investment will grow to \frac{m(1 - (r/100)^{n+1})}{1 - r/100} dollars.
Write a program invest.c
that accepts three integers as input: principal m, rate r, and number of years n, and computes the amount of money earned after n years.
You may assume that r <= 100 r < 100.
Sample Run
ooiwt@pe111:~/as01-skeleton$ ./invest
100 10 5
111.1110
ooiwt@pe111:~/as01-skeleton$ ./invest
20000 5 10
21052.6316
Question 2: Box (5 marks)
Write a program box.c
that reads three positive integers representing the length, width, and height of a box, and output (i) its surface area, and (ii) the length of the diagonal between two vertices of the box that are furthest apart (see figure).
You may assume that the surface area of the box does not exceed the maximum value representable in the long
data type.
You should break down the problem into smaller ones:
- Write a new method
area_of_rectangle
that computes the area of a rectangle given the width and height of the rectangle, then use it to compute the surface area. - Modify the method
hypotenuse_of
seen in Unit 5 to compute the diagonal of the box. (hint: pay attention to the type of the parameter and the return value).
Sample Run
ooiwt@pe111:~/as01-skeleton$ ./box
12 3 10
372 15.9060
ooiwt@pe111:~/as01-skeleton$ ./box
10 20 30
2200 37.4166
Question 3: Digits (5 marks)
Write a program digits.c
that reads in a non-negative integer, and prints the sum of the individual digits in this integer.
For instance, if the input is 1933091
, then the sum is 1 + 9 + 3 + 3 + 0 + 9 + 1 = 26.
You should not use a loop to solve this, but rather, you should write a function sum_of_digits
that takes in an integer and return the sum of the digits of that integer, that calls itself:
- if the input to
sum_of_digits
has only one digit, return this digit. - Otherwise, use the modulo operator
%
and integer division/
to extract the last digit (e.g.,1
) and the rest of the digits (e.g.,193309
) respectively, and callsum_of_digits
on the rest of the digits to find its sum (e.g., 1+9+3+3+0+9 = 25). Finally we add the last digit to this sum to get the total we seek (e.g., 1 + 25 = 26).
Sample Run
ooiwt@pe111:~/as01-skeleton$ ./digits
1933091
26
ooiwt@pe111:~/as01-skeleton$ ./digits
0
0
Question 4: Taxi Fare (15 marks)
The taxi fare structure in Singapore must be one of the most complex in the world! Check out: http://www.taxisingapore.com/taxi-fare/.
For the purpose of this exercise, we will just use the following simpliļ¬ed fare structure:
Basic Fare | |
---|---|
The first 1 km or less (Flag Down) | $3.40 |
Every 400 m thereafter or less, up to 10.2 km | $0.22 |
Every 350 m thereafter or less, after 10.2 km | $0.22 |
Surcharge | ||
---|---|---|
Monday to Friday | 6:00 to 9:29 | 25% of metered fare |
Daily | 18:00 to 23:59 | 25% of metered fare |
Daily | 0:00 (midnight) to 5:59 | 50% of metered fare |
Note that the surcharge is applicable based on the boarding time. For instance, if the trip started at 17:50 and ended at 18:10, then no surcharged is incurred.
Write a program taxi.c
that computes the taxi fare. The program, called taxi
, takes in four integers as inputs:
- The first is the day of the week. It can only be the value
1
to7
,1
being Monday,7
being Sunday. - The second and the third is the starting time of the trip: the second input indicates the hours since midnight of the stated day, the third input indicates the minutes since the beginning of the stated hours.
- The forth and final input is the distance of the trip, in meters.
Your program should print a single floating point number, which is the cost of the fare in dollars.
Examine the following examples for more details:
Example 1
ooiwt@pe111:~/as01-skeleton$ ./taxi
1 17 59 1000
3.4000
- Start: Mon 17:59
- Distance: 1,000 m
The metered fare is $3.40 since the distance travelled is 1km. The boarding time is before 18:00 so there is no surcharge. The total fare is $3.40.
Example 2
ooiwt@pe111:~/as01-skeleton$ ./taxi
1 17 57 2000
4.0600
- Start: Mon 17:57
- Distance: 2,000 m
The metered fare for the first 1,000 m (1km) is $3.40. The next 1,000 m is charged $0.22 for every 400 m (or less) travelled. The pessengar is charged an additional 3 x $0.22, giving the total of metered fare of $4.06.
The boarding time is before 18:00 so there is no surcharge.
Example 3
ooiwt@pe111:~/as01-skeleton$ ./taxi
1 5 50 15000
17.3100
- Start: Mon 05:50
- Distance: 15,000 m
The metered fare for the first 1,000 m (1km) is $3.40. The next 9,200 m is charged $0.22 for every 400 m travelled. The pessengar is charged an additional 23 x $0.22 = $5.06. The remaining 4,800 m is charged $0.22 for every 350 m (or less) travelled). The pessengar is charged an additional 14 x $0.22 = $3.08. The total metered fare is $11.54.
The boarding time is before 6:00 so there is a 50% surcharge. Total fare is $17.31.
Hints
-
You can break down this problem into multiple subproblems, each can be solved by a function. Write one function to answer each question below:
- given the day of the week, is it a weekday?
- given the time of boarding, does it fall under the morning peak hour (6:00 - 9:29)?
- given the time of boarding, does it fall under the evening peak hour (18:00 - 23:59)?
- given the time of boarding, does it fall under the midnight peak hour (0:00 - 5:59)?
-
You can further breakdown the calculation of fare into two parts: the basic metered fare and the surcharge. Each of these can be its own function. Think about the four inputs to the
taxi
program. Which one is needed to compute the metered fare? Which ones are used to compute the surcharge? Pass in the appropriate arguments to the function that computes the metered fare and the function that computes the surcharge.