Assignment 1
Deadline
7 September 2021 (Tuesday), 23:59 pm.
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 that 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:
pyramid.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.compiler_flags.txt
and.clang-tidy
are two files used to configureclang
andclang-tidy
respectively. You do not need to edit this.
Solving The Assignments
-
Edit the files
pyramid.c
,digits.c
,suffix.c
, andtaxi.c
to solve the corresponding question as described below. -
To compile and run the given tests with the sample inputs and outputs, run on the command line,
1 |
|
This command will compile the C files. If there is no compilation error, it will run the test scripts.
To compile (and only compile) your code for a specific problem, pass in the name as an argument. Example:
1 |
|
To test (and only test) your program for a specific problem, run the test.sh
script:
1 |
|
You can read more about how you can become a power user of make
in CS1010.
This list of common clang
warnings and error messages might be helpful.
Clean Compilation
This is a reminder that, for your lab assignments and practical exams, any submission that cannot compile will receive 0. Further, each warning will lead to a -1 mark deduction. So, please make sure that your code does not generate any error or warning. You can ensure this by running make
before submission.
Submission
When you are ready, run the following command to submit:
1 |
|
The four files pyramid.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 lab 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: Pyramid (5 marks)
A right rectangular pyramid is a 3D shape with a rectangle as a base and four isosceles triangular sides that meet at its apex. We can specify such a pyramid with three parameters, the width \(w\) of the base, the length \(l\) of the base, and the height \(h\) of the apex (perpendicular distance between the apex and the base).
Our task is to calculate the surface area of a pyramid, by decomposing this calculation into three smaller functions. To complete this task, in pyramid.c
,
- Write a function
hypotenuse_of
to compute the hypotenuse of a right-angled triangle, given the length of the legs (the sides adjacent to the right angle). - Write a function
area_of_rectangle
that computes the area of a rectangle given the width and height of the rectangle. - Write a function
area_of_triangle
that computes the area of a triangle using Heron's formula (see Exercise 1), given the length of the three sides.
Finally, complete the program pyramid.c
so that it reads, from the standard input, three positive integers representing the width, length, and height of the pyramid (in that order) and prints, to the standard output, its surface area. Solve this by using composing the three functions above. You may add additional functions if needed.
Pay attention to the types (long
or double
) used in the inputs, calculations, and outputs. Keep the variable as integer type (long
) as long as possible.
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 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 must not use a loop to solve this. Instead write a recursive function sum_of_digits
. This function takes in an integer and returns the sum of the individual digit of that integer, and it should call itself to solve this question, for a smaller integer.
A solution that uses a loop in any way will receive 0 marks for this question.
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 complicated in the world! Check out: http://www.taxisingapore.com/taxi-fare/.
For this exercise, we will just use the following simplified fare structure:
Basic Fare | |
---|---|
The first 1 km or less (Flag Down) | $3.70 |
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.70 since the distance traveled is 1km. The boarding time is before 18:00 so there is no surcharge. The total fare is $3.70.
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.70. 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 metered fare of $4.36.
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.70. 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 $12.06.
The boarding time is before 6:00 so there is a 50% surcharge. The total fare is $18.09.
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 a 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.