Skip to content

Exercise 1: Conditionals

Prerequisite

You have completed Exercise 0.

Learning Outcomes

Be comfortable writing simple C programs that involve conditional statements and logical expressions.

Deadline

This is part of CS1010 formative assessment. Submit before 11 September 2023, 23:59 to receive feedback and earn your achievement "badges".

The link to accept the exercise is not available publicly. Visit Canvas for the link.

Concepts and Difficulty

Question Conditions Comparison Logical Expression Recursion Difficulty
1 Odd
2 Sum
3 Candy
4 Triangle
5 Leap
6 Multiple
7 Date^
8 Pressure^
9 GCD^
10 Digits
11 Power
12 Suffix
13 Taxi

^: Questions 7 - 9 serve as in-class exercise during lab session in Week 4.

Question 1: Odd

Write a program odd (source file odd.c) that reads in an integer \(n\) and prints the smallest odd number that is larger than \(n\) to the standard output.

Your program must find the answer within a function that takes in an integer and returns an integer called find_next_odd.

Sample Runs

1
2
3
4
5
6
ooiwt@pe112:~/ex01-ooiwt$ ./odd
1234
1235
ooiwt@pe112:~/ex01-ooiwt$ ./echo
-1
1

Note: There is a less straightforward way to solve this question without using conditional statements. See if you can solve it this way! (Credit: Hu Jialun (cohort 20/21))

Question 2: Sum

Write a program sum (source file sum.c) that reads in two integers, \(x\) and \(y\), ignores any negative numbers, and prints the sum of the remaining (non-negative) numbers. If both numbers are negatives, the sum is 0.

Your program must find the answer within a function named compute_sum_if_positive.

Sample Runs

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
ooiwt@pe111:~/ex01-ooiwt$ ./sum
123456
-100
123456
ooiwt@pe111:~/ex01-ooiwt$ ./sum
-1
-2
0
ooiwt@pe111:~/ex01-ooiwt$ ./sum
1
2
3

Question 3: Candy

Beng needs to pack a bunch of candies into as few boxes as possible. Each box can hold only a certain number of candies.

Write a program candy that reads from the standard input two positive integers representing the number of candies that Beng has and the maximum number of candies that a box can hold.

Your program must contain a function called compute_num_of_boxes that computes the minimum number of boxes that Beng needs to pack all the candies into. The function should take in the number of candies and the maximum number of candies a box can hold as arguments, and return the number of boxes needed.

Your program then prints, to the standard output, the number of boxes needed.

For example, if there are 8 candies and each box can hold a maximum of 2 candies, then Beng needs 4 boxes. If each box can hold a maximum of 3 candies, then only 3 boxes are needed.

Sample Runs

1
2
3
4
5
6
ooiwt@pe111:~/ex01-ooiwt$ ./candy
8 2
4
ooiwt@pe111:~/ex01-ooiwt$ ./candy
8 3
3

Question 4: Triangle

In a triangle, the sum of the length of any two sides is always longer than of the third side. The area of the triangle with sides \(a\), \(b\), \(c\), can be computed with Heron's formula:

\[\sqrt{s(s-a)(s-b)(s-c)}\]

where \(s\) is half of the perimeter:

\[s = \frac{a + b + c}{2}\]

Write a program triangle that reads from standard input three positive integers representing the three sides of a triangle. The program then either - prints, to the standard output, Possible if it is possible to create such a triangle, followed by the area of the triangle; or - prints Impossible if it is impossible to create such a triangle.

For instance, suppose that you are given the three sides of a triangle 3 4 5. Then the program prints:

1
2
Possible
6.0000

Suppose that you are given the three sides 4 4 10. Then the program prints:

1
Impossible

Your solution must contain two functions is_possible_triangle, which returns whether the input is a possible triangle, and compute_area, which computes the area of the triangle.

Sample Runs

1
2
3
4
5
6
7
ooiwt@pe111:~/ex01-ooiwt$ ./triangle
3 4 5
Possible
6.0000
ooiwt@pe111:~/ex01-ooiwt$ ./triangle
4 4 10
Impossible

Question Credit: Albert Sutiono (cohort 20/21)

Question 5: Leap Year

A leap year is a calendar year containing an extra day to synchronize the calendar to seasons and astronomical events. In the Gregorian calendar, years that are multiples of four (except for years divisible by 100 but not by 400) are leap years.

Complete the program leap.c so that it reads in an integer representing a year from the standard input and prints out " is a leap year" if the input is a leap year. Otherwise, print " is not a leap year" to the standard output.

Your program should include a bool function is_leap_year that takes in the input year and returns true if the input is a leap year and returns false otherwise.

Sample Runs

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
ooiwt@pe112:~/ex01-ooiwt$ ./leap
1995
1995 is not a leap year
ooiwt@pe112:~/ex01-ooiwt$ ./leap
1996
1996 is a leap year
ooiwt@pe112:~/ex01-ooiwt$ ./leap
1900
1900 is not a leap year
ooiwt@pe112:~/ex01-ooiwt$ ./leap
2000
2000 is a leap year

Question 6: Multiple

Given two integers, we want to check if one is a multiple of the other. By definition, 0 is the multiple of any number.

Write a program multiple that reads two integers from the standard input, and prints true to the standard output if one integer is the multiple of the other. Print false otherwise.

Your program should include a bool function is_multiple that takes in the two integers and return if one is the multiple of the other.

Sample Runs

1
2
3
4
5
6
7
8
9
ooiwt@pe112:~/ex01-ooiwt$ ./multiple
3 8
false
ooiwt@pe112:~/ex01-ooiwt$ ./multiple
8 2
true
ooiwt@pe112:~/ex01-ooiwt$ ./multiple
0 190
true

Question 7: Date

A date consists of two integers, a month (1 - 12) and a day (1 - 31). Given three dates (of the same year), we want to find out if the given dates are sorted in increasing order.

Write a program date (source file date.c) that reads in three pairs of integers from the standard input, in the order of \(m_1\), \(d_1\), \(m_2\), \(d_2\), \(m_3\), and \(d_3\). Print yes to the standard output the date (\(m_1, d_1\)) is strictly before (\(m_2, d_2\)), and the date (\(m_2, d_2\)) is strictly before (\(m_3, d_3\)). Print no otherwise.

Think: Can you break this problem down into a simpler sub-problem?

Sample Runs

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
ooiwt@pe112:~/ex01-ooiwt$ ./date
1 1
12 31
6 6
no
ooiwt@pe112:~/ex01-ooiwt$ ./date
5 5
6 1
7 3
yes

Question 8: Pressure

A blood pressure measurement is given by two numbers, \(s/d\), where \(s\) is the systolic reading and \(d\) is the diastolic reading.

Given the two readings, the following diagram classifies a given blood pressure measurement into four regions: low, ideal, pre-high, and high.

BP chart

Readings that fall on the borders are considered to be in the region to the top and to the right. For instance, blood pressure 100/80 and 120/70 are considered to fall under the "pre-high" region.

Write a program pressure that does the following:

  1. Reads in a blood pressure measurement from the standard input as two positive integers,
  2. Determines which region the given blood pressure measurement belongs to according to the chart above, and
  3. Prints the region (either low, ideal, pre-high, or high) to the standard output,

Steps 2 and 3 should be solved in the function below:

1
2
3
4
void print_blood_pressure(long systolic, long diastolic)
{
  :
}

Sample Runs

1
2
3
4
5
6
ooiwt@pe112:~/ex01-ooiwt$ ./pressure
80 50
low
ooiwt@pe112:~/ex01-ooiwt$ ./pressure
100 50
ideal

Question 9: GCD

The GCD, or greatest common divisor, of two integers \(a\) and \(b\), is the largest positive integer that divides both \(a\) and \(b\). For example, the GCD of 48 and 18 is 6.

The GCD of two numbers \(a\) and \(b\) is the same as the GCD of \(b\) and \(a\) % \(b\). To find the GCD of \(a\) and \(b\), we thus repeatedly replace (\(a\), \(b\)) with (\(b\), \(a\) % \(b\)) until we have the pair (\(c\), 0). \(c\) is the GCD of \(a\) and \(b\).

For example, to find the GCD of 48 and 18, (48, 18) \(\rightarrow\) (18, 12) \(\rightarrow\) (12, 6) \(\rightarrow\) (6, 0). The GCD of 48 and 18 is 6.

Write a program gcd (source file gcd.c) that reads in two positive integers, \(a\) and \(b\), and prints their GCD. The computation should be performed in a function called compute_gcd that takes in \(a\) and \(b\) and returns their GCD.

Your program must solve this problem recursively, without using any loops.

Sample Runs

1
2
3
4
5
6
ooiwt@pe112:~/ex01-ooiwt$ ./gcd
48 32
16
ooiwt@pe112:~/ex01-ooiwt$ ./gcd
1 1
1

Question 10: Digits

Write a program digits.c that reads in a non-negative integer, and prints the sum of the cube of individual digits in this integer.

For instance, if the input is 1933091, then the sum is \(1^3 + 9^3 + 3^3 + 3^3 + 0^3 + 9^3 + 1^3 = 1514\).

You must not use a loop to solve this. Instead, write a recursive function sum_of_digits_cubed. This function takes in an integer and returns the sum of the cube of individual digits of that integer, and it should call itself to solve this question.

A solution that uses a loop in any way will be considered incorrect for this question.

Sample Runs

1
2
3
4
5
6
ooiwt@pe111:~/ex01-ooiwt$ ./digits
1933091
1514
ooiwt@pe111:~/ex01-ooiwt$ ./digits
10
1

Question 11: Power

Write a program power.c that reads in two values: (i) an integer base \(x\) and (ii) a non-negative integer exponent \(y\), and compute \(x^y\).

You must not use a loop to solve this. Instead, write a recursive function compute_power that takes in takes in the base \(x\) and the exponent \(y\) and returns \(x^y\). This function should call itself to solve this question.

A solution that uses a loop in any way will be considered incorrect for this question.

Sample Runs

1
2
3
4
5
6
7
8
9
ooiwt@pe111:~/ex01-ooiwt$ ./power
2 3
8
ooiwt@pe111:~/ex01-ooiwt$ ./power
1 0
1
ooiwt@pe111:~/ex01-ooiwt$ ./power
-2 62
4611686018427387904

Question 12: Suffix

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 the suffix "st" (except if it ends with 11), a number that ends with 2 should have the 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 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.

Sample Runs

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
ooiwt@pe112:~/ex01-ooiwt$ ./suffix
2
2nd
ooiwt@pe112:~/ex01-ooiwt$ ./suffix
13
13th
ooiwt@pe112:~/ex01-ooiwt$ ./suffix
412
412th
ooiwt@pe112:~/ex01-ooiwt$ ./suffix
3
3rd

Question 13: Taxi Fare

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.90
Every 400 m thereafter or less, up to 10 km $0.24
Every 350 m thereafter or less, after 10 km $0.24
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 0 to 6, 0 denotes Sunday, 1 denotes Monday, ..., and 6 denotes Saturday.
  • The second and the third inputs denote the starting time of the trip: the second input indicates the hours since midnight of the stated day, and 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:~/ex01-ooiwt$ ./taxi
1 17 59 1000
3.9000
  • Start: Mon 17:59
  • Distance: 1,000 m

The metered fare is $3.90 since the distance traveled is 1 km. The boarding time is before 18:00, so there is no surcharge. The total fare is $3.90.

Example 2

1
2
3
ooiwt@pe111:~/ex01-ooiwt$ ./taxi
1 17 57 2000
4.6200
  • Start: Mon 17:57
  • Distance: 2,000 m

The metered fare for the first 1,000 m (1 km) is $3.90. The next 1,000 m is charged $0.24 for every 400 m (or less) traveled. The passenger is charged an additional 3 x $0.24 = $0.72, giving the total metered fare of $4.62.

The boarding time is before 18:00, so there is no surcharge.

Example 3

1
2
3
ooiwt@pe111:~/ex01-ooiwt$ ./taxi
1 5 50 15000
19.5300
  • Start: Mon 05:50
  • Distance: 15,000 m

The metered fare for the first 1,000 m (1 km) is $3.90. The next 9,000 m is charged $0.24 for every 400 m traveled. The passenger is charged an additional 23 x $0.24 = $5.52. The remaining 5,000 m is charged $0.24 for every 350 m (or less) traveled. The passenger is charged an additional 15 x $0.24 = $3.60. The metered fare is $3.90 + $5.52 + $3.60 = $13.02

The boarding time is before 6:00, so there is a 50% surcharge. The total fare is $19.53.

Instructions

  • Break down this problem into multiple sub-problems, 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)?
  • Furthermore, 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.