Skip to content

Exercise 2: Date, GCD, Leap, Multiple

Deadline

This is an ungraded exercise. There is no deadline, but we encourage you to complete it before Thursday's lab so that you do not fall behind.

Prerequisite

Learning Outcomes

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

Accepting and Retrieving Assignments

1
~cs1010/get-ex02
  • You should see a new subdirectory ex02-<username> in your current working directory, where username is your GitHub username.
  • We will call this directory your exercise directory or assignment directory.

  • Inside that directory, you should see a bunch of files:

    • date.c, gcd.c, leap.c, and multiple.c are the most important files. They are the skeleton C code that you should edit to solve the exercise.
    • inputs and outputs are subdirectories that contain test inputs and test outputs.
    • Makefile: The configuration for the tool make that we use to automate the compilation and testing of the programs. You do not have to understand how to write a Makefile for CS1010. If you are interested to learn how to write a Makefile, talk to either Wei Tsang or Google.
    • test.sh: A bash script for testing your code. You do not have to edit this file. If you are interested to learn how to write bash script, talk to either Wei Tsang or Google.
    • compiler_flags.txt and .clang-tidy are two files used to configure clang and clang-tidy respectively. You do not need to edit this.

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 Kamala Khan (Group A10)

Solving The Assignments

  • Edit the files date.c, gcd.c, leap.c, multiple.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
make

This command will compile the C files. If there is no compilation error, it will run the test scripts. make is smart enough that if you did not change the C file, it will not recompile the files again. 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 compilation warning will lead to a -1 mark deduction. So, please make it a habit to ensure that you can compile cleanly, starting with this ungraded exercise.

Examining Individual Input/Output Files

If your code prints the wrong output for some of the test inputs, familiarity with UNIX CLI would be helpful. Suppose that your code for problem echo fails on test case 3. To see the input of this test case, run:

1
cat inputs/echo.3.in

To see the expected output:

1
cat outputs/echo.3.out

To see what output your program gives,

1
./echo < inputs/echo.3.in

To test the program for a particular question (say, echo), instead of all programs,

1
./test.sh echo

Submission

When you are ready, run the following command while you are in the exercise directory:

1
~cs1010/submit-ex02 

The files date.c, gcd.c, leap.c, and multiple.c will be uploaded to GitHub. You can submit multiple times.

You are not allowed to interact with your CS1010 GitHub repositories using git commands or edit your files directly on GitHub's website. Doing so would interfere with the automation that we use for grading and would result in penalties (for graded assignments).

Grading

This assignment is not graded.


Question 1: 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\)).

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

Sample run:

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

Question 2: GCD

The GCD, or greatest common divisor, of two integers, is the largest positive integer that divides each of the integer. For example, GCD of 8 and 12 is 4.

The famous Greek mathematician, Euclid, introduce the following recursive method to compute the GCD of two integers \(x\) and \(y\) (we assume \(x \ge y\) without loss of generality):

  • If the two integers are the same (\(x\) is the same as \(y\)) then the GCD of these two integers is just \(x\).
  • Otherwise, the GCD of \(x\) and \(y\) is the same as the GCD of \(x - y\) and \(y\).

For example, the GCD of 8 and 12, is the same as the GCD of 8 and 4 (since 12 - 8 is 4), which is the same as the GCD of 4 and 4. So the answer is 4.

Write a program gcd (source file gcd.c) that reads in two positive integers, \(x\) and \(y\), and prints their GCD.

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

Sample run:

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

Question 3: 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 run:

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

Question 4: 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 print true to the standard output if one integer is the multiple of the other. Print false otherwise.

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

Question Credit: Hu Jialun (Cohort 20/21)