Assignment 1
Deadline
15 Feb 2021 (Monday), 4pm. (Note that this is an extended deadline due to CNY.)
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 0 and have already set up 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 involve arithmetic operations,
long
,double
, andbool
types, and conditionalif
/else
statements. - Be comfortable breaking down a problem into smaller sub-problems 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.
Setup
- First, accept the assignment on GitHub
- Then, log in to one of the hosts of CS1010 programming environment and run
1 |
|
- You should see the folder
as01-<github username>
in your home directory with the assignment skeleton inside. - Inside that directory, you should see the following files:
box.c
,digits.c
,suffix.c
, andtaxi.c
are the most important files. They are the skeleton C code that you should edit to solve the assignment and the only files that you should change.inputs
andoutputs
are subdirectories that contain test inputs and test outputs. We use the same convention as Exercise 0 so you should already be familiar with them.Makefile
: This is the configuration for the toolmake
that we use to automate the compilation and testing of the programs.test.sh
: This is a bash script for testing your code.compile_flags.txt
: This file specifies the flags for compilation and is used by bothmake
andclang-tidy
.
Solving The Assignments
- Edit the files
box.c
,digits.c
,suffix.c
, andtaxi.c
to solve the corresponding question as described below. -
To compile and run tests with the sample inputs and outputs:
This command will compile all your C files with the appropriate settings and if there is no error, run the test scripts. If you pass the test cases, it will run1
make
clang-tidy
to check if your code follows good code practices. -
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:
1 |
|
The four files box.c
, digits.c
, suffix.c
, and taxi.c
will be uploaded to GitHub. You can submit multiple times, but only the last submission will be graded.
Manipulating Files on GitHub
Do not manipulate (e.g., edit) the files on GitHub or close the pull requests on GitHub. Doing so would interfere with the scripts and the grading/testing process of CS1010.
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:
1 |
|
and change it to something like:
1 |
|
Grading
This assignment contributes towards 3% of your final grade. The total marks for this assignment are 30 marks. For Programming Assignment 1, the sole criterion for grading is correctness -- this includes the correctness of syntax, practices, approach, and logic, and whether you correctly follow our instructions. Note that even if your solution produces the correct output every time, it may not get full marks if the approach is wrong.
Question 1: 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
1 2 3 4 5 6 |
|
Question 2: Digits (5 marks)
Write a program digits.c
that reads in a non-negative integer, and prints the sum of the square of individual digits in this integer.
For instance, if the input is 1933091
, then the sum is \(1^2 + 9^2 + 3^2 + 3^2 + 0^2 + 9^2 + 1^2 = 182\).
You should not use a loop to solve this, but rather, you should write a recursive function sum_of_digits_square
. This function takes in an integer and returns the sum of the square of individual digit of that integer, and it should call itself to solve this question, for a smaller integer.
- if the input to
sum_of_digits_square
has only one digit, return the square of 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_square
on the rest of the digits to find its sum (e.g., \(1^2+9^2+3^2+3^2+0^2+9^2 = 181\)). Finally, we add the last digit to this sum to get the total we seek (e.g., \(1 + 181 = 182\)).
Sample Run
1 2 3 4 5 6 |
|
Question 3: Ordinal Suffix (5 marks)
In English, an ordinal number is written with numerals, followed by its letter suffixes. For instance: 1st, 2nd, 3rd, 4th, 11th, 31st, etc. The rule is that a number that ends with digit 1 should have a suffix "st" (except if it ends with 11), a number that ends with 2 should have a suffix "nd" (except if it ends with 12), and a number that ends with 3 should have a suffix "rd" (except if it ends with 13). All other numbers should end with "th".
Write a program suffix
that reads in an integer number from the standard input and prints out the number with its ordinal suffix.
Your program should include a void
function print_with_suffix(long n)
that takes in the input and prints out the number followed by its suffix.
1 2 3 4 5 6 7 8 9 10 11 12 |
|
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.20 |
Every 400 m thereafter or less, up to 10 km | $0.22 |
Every 350 m thereafter or less, after 10 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 surcharge 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 fourth 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
1 2 3 |
|
- Start: Mon 17:59
- Distance: 1,000 m
The metered fare is $3.20 since the distance traveled is 1km. The boarding time is before 18:00 so there is no surcharge. The total fare is $3.20.
Example 2
1 2 3 |
|
- Start: Mon 17:57
- Distance: 2,000 m
The metered fare for the first 1,000 m (1km) is $3.20. The next 1,000 m is charged $0.22 for every 400 m (or less) traveled. The passenger is charged an additional 3 x $0.22, giving the total of metered fare of $3.86.
The boarding time is before 18:00 so there is no surcharge.
Example 3
1 2 3 |
|
- Start: Mon 05:50
- Distance: 15,000 m
The metered fare for the first 1,000 m (1km) is $3.20. The next 9,000 m is charged $0.22 for every 400 m traveled. The passenger is charged an additional 23 x $0.22 = $5.06. The remaining 5,000 m is charged $0.22 for every 350 m (or less) traveled). The passenger is charged an additional 15 x $0.22 = $3.30. The total metered fare is $11.56.
The boarding time is before 6:00 so there is a 50% surcharge. The total fare is $17.34.
Instructions
-
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)?
-
Further break the calculation of fare down 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.