Skip to content

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

Learning Outcomes

  • Be comfortable writing correct C programs that involve if, else, and logical statements.

Setup

1
~cs1010/get-ex01
  • You should see a new subdirectory ex01-<githubid> in your current working directory, where githubid is your GitHub ID.
  • Inside that directory, you should see a bunch of files:
    • gcd.c, leap.c, and days.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. We use the same convention as Exercise 1 so you should 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 .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
make

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
~cs1010/submit-ex01

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
ooiwt@pe112:~/ex01-ooiwt$ ./gcd
48 32
16
ooiwt@pe112:~/ex01-ooiwt$ ./gcd
1 1
1

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 " 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:~/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 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
ooiwt@pe112:~/ex01-ooiwt$ ./days
1 1
1
ooiwt@pe112:~/ex01-ooiwt$ ./days
8 15
227
ooiwt@pe112:~/ex01-ooiwt$ ./days
12 31
365