Exercise 1: GCD, Leap, Days
This is a programming exercise for you to solve on your own. You can submit but it will not be graded. Test cases are provided for the exercises so that you can test and check on your own if your code is correct. Feel free to discuss your solution with your peers or your tutors.
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 a GitHub account and have setup
.gitconfig
(see Exercise 0).
Learning Outcomes
- Be comfortable writing correct C programs that involve
if
,else
, and logical statements.
Setup
- 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
ex01-<githubid>
in your current working directory, wheregithubid
is your GitHub ID. - Inside that directory, you should see a bunch of files:
gcd.c
,leap.c
, anddays.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. We use the same convention as Exercise 1 so you should 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.compile_flags.txt
: This file specifies the flags for compilation and is used by bothmake
andclang-tidy
.
Solving The Assignments
- Edit the .c files to solve the corresponding question as described below
- You can assume that all test inputs are valid inputs.
- To compile and run the given tests with the sample inputs and outputs, run on the command line,
1 |
|
This will compile all your C files 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
certain good code practices.
Submission
When you are ready, run the following command while you are in the exercise directory:
1 |
|
The .c files will be uploaded to GitHub. You can submit multiple times. As an exercise is not graded, submitting your code only serves the purpose of archiving your work for backup purposes.
Question 1: GCD
In Post-Lecture Diagnostic Quiz 2 Problem C on Recursion, the recursive function F computes the greatest common divisor of two positive integers using the Euclidean algorithm (aka Euclid's algorithm).
Complete a program gcd.c
so that it reads in two positive integers from the standard input and prints out their greatest common divisor.
Sample run:
1 2 3 4 5 6 |
|
Question 2: 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 3: Days Since 1 January
Write a program called days
that reads in two integers from the standard input, the first is the month (ranged 1 to 12, inclusive) and the second is the day (ranged 1 to 31, inclusive). The program should print to the standard output which day of the year it is. Assume that the year is not a leap year.
1 2 3 4 5 6 7 8 9 |
|