Exercise 2: Leap, Suffix, 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 TAs.
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 1).
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:
~cs1010/get-ex02
- You should see a new subdirectory
ex02-<githubid>
in your current working directory, wheregithubid
is your GitHub ID. - Inside that directory, you should see a bunch of files:
leap.c
,suffix.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.
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,
make
This will compile all your C files and if there is no error, run the test scripts.
Submission
When you are ready, run the following command while you are in the exercise directory:
~cs1010/submit-ex02
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: 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 (with the exception of years divisible by 100 but not by 400) are leap years.
Write a program that 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:
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 2: Ordinal 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 a suffix "st" (except if it ends with 11), a number that ends with 2 should have a 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 in 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.
ooiwt@pe112:~/ex02-ooiwt$ ./suffix
2
2nd
ooiwt@pe112:~/ex02-ooiwt$ ./suffix
13
13th
ooiwt@pe112:~/ex02-ooiwt$ ./suffix
412
412th
ooiwt@pe112:~/ex02-ooiwt$ ./suffix
3
3rd
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. You can reuse the method print_suffix
from the previous question.
ooiwt@pe112:~/ex02-ooiwt$ ./days
1 1
1st
ooiwt@pe112:~/ex02-ooiwt$ ./days
8 15
227th
ooiwt@pe112:~/ex02-ooiwt$ ./days
12 31
365th