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
- You are able to access the CS1010 programming environment.
- You are familiar with basic UNIX CLI and using terminal-based editor
vim
. - You are familiar with basic C syntax and arithmetic operations, and have completed at least 3 of 5 questions from Exercise 0.
Learning Outcomes
- Be comfortable writing simple C programs that involve conditional statements and logical expressions.
Accepting and Retrieving Assignments
- Click on this link to accept the exercise.
- Log in to one of the hosts of CS1010 programming environment (PE)
- Run the following on the command line on one of the PE hosts:
1 |
|
- You should see a new subdirectory
ex02-<username>
in your current working directory, whereusername
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
, andmultiple.c
are the most important files. They are the skeleton C code that you should edit to solve the exercise.inputs
andoutputs
are subdirectories that contain test inputs and test outputs.Makefile
: The configuration for the toolmake
that we use to automate the compilation and testing of the programs. You do not have to understand how to write aMakefile
for CS1010. If you are interested to learn how to write aMakefile
, talk to either Wei Tsang or Google.test.sh
: Abash
script for testing your code. You do not have to edit this file. If you are interested to learn how to writebash
script, talk to either Wei Tsang or Google.compiler_flags.txt
and.clang-tidy
are two files used to configureclang
andclang-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 |
|
and change it to something like:
1 |
|
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 |
|
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 |
|
To see the expected output:
1 |
|
To see what output your program gives,
1 |
|
To test the program for a particular question (say, echo
), instead of all programs,
1 |
|
Submission
When you are ready, run the following command while you are in the exercise directory:
1 |
|
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 |
|
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 |
|
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 "
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 |
|
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 |
|
Question Credit: Hu Jialun (Cohort 20/21)