Skip to content

Assignment 1

Deadline

8 September 2020 (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, and bool types, and conditional if/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

1
~cs1010/get-as01
  • 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, and taxi.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 and outputs 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 tool make 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 both make and clang-tidy.

Solving The Assignments

  • Edit the files box.c, digits.c, suffix.c, and taxi.c to solve the corresponding question as described below.
  • To compile and run tests with the sample inputs and outputs:

    1
    make
    
    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 run clang-tidy to check if your code follows good code practices.

  • The test cases are given in the inputs and outputs subdirectory. You can use cat or less 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
~cs1010/submit-as01

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
@author XXXX (Group YYYY)

and change it to something like:

1
@author Dolores Abernathy (Group E01)

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

diagonal of a box

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
ooiwt@pe111:~/as01-ooiwt$ ./box
12 3 10
372 15.9060
ooiwt@pe111:~/as01-ooiwt$ ./box
10 20 30
2200 37.4166

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 call sum_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
ooiwt@pe111:~/as01-ooiwt$ ./digits
1933091
182
ooiwt@pe111:~/as01-ooiwt$ ./digits
0
0

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
ooiwt@pe112:~/as01-ooiwt$ ./suffix
2
2nd
ooiwt@pe112:~/as01-ooiwt$ ./suffix
13
13th
ooiwt@pe112:~/as01-ooiwt$ ./suffix
412
412th
ooiwt@pe112:~/as01-ooiwt$ ./suffix
3
3rd

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 to 7, 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
ooiwt@pe111:~/as01-ooiwt$ ./taxi
1 17 59 1000
3.2000
  • 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
ooiwt@pe111:~/as01-ooiwt$ ./taxi
1 17 57 2000
3.8600
  • 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
ooiwt@pe111:~/as01-ooiwt$ ./taxi
1 5 50 15000
17.3400
  • 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.