Exercise 3: Three, Factor, Parity, Nine, HDB
Deadline
This is an ungraded exercise designed to expose you to different patterns of using loops. There is no deadline, but we encourage you to complete it before Thursday's lab so that you do not fall behind.
Prerequisite
- You can 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 2 of 4 questions from Exercise 2.
Learning Outcomes
Be comfortable writing simple C programs that involve loops, including
- loops with conditional body
- loops with conditional body and counting
- loops with early return
- nested loops
- loops that step through digits in a number
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
ex03-<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:
three.c
,factor.c
,parity.c
,nine.c
andhdb.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
three.c
,factor.c
,parity.c
,nine.c
, andhdb.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. 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 ungraded exercises.
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 three.c
, factor.c
, parity.c
, nine.c
and hdb.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 exercise is not graded.
Question 1: Three
Write a program called three
that reads in two integers \(x\) and \(y\) from the standard input, and prints to the standard outputs, all multiple of threes between \(x\) and \(y\) (inclusive).
Sample Runs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
Question 2: Factor
Given a number \(n\), we want to find out how many factors \(n\) has, excluding the trivial factor 1 and \(n\).
Write a program factor
that reads, from the standard input, a positive integer \(n\), and prints, to the standard output, the factors of \(n\) between 2 and \(n-1\), inclusive).
Sample Runs
1 2 3 4 5 6 7 8 9 |
|
Question 3: Parity
Write a program parity
, that reads from standard input a positive integer \(n\)
print, to the standard output, 2 lines,
1 2 |
|
X
represents the number of odd digits and Y
represents the number of even digits.
Note that there is exactly one space between the colon :
and X
or Y
and
there are no trailing spaces. You may create any function if you need to.
The purpose of this question is for you to practice using loops. Hence, try to solve it without using any form of recursion for this question.
Sample Runs
1 2 3 4 5 6 7 8 |
|
Question Credit: Albert Sutiono (cohort 20/21)
Question 4: Nine
Write a program that looks for the least significant occurrence of digit 9 in a given number.
Your program, nine
, should read a positive number from the standard input and print out the position of the least significant occurrence of 9. The rightmost digit has the position of 1, the second last has the position of 2, etc. If the number 9 does not appear in the given number, print 0.
Sample Runs
1 2 3 4 5 6 7 8 9 |
|
Question 5: HDB
ASCII Art refers to the art of drawing with only common letters, numbers, and symbols on our keyboard. My daughter has discovered that, if we draw rows of
#
symbols together, it approximately looks like an HDB flat!
Write a program hdb
that takes in two positive integers \(w\) and \(h\), and draw \(h\) rows of #
symbols, each row containing \(w\) #
, with no spaces before, in between, and after.
Sample Runs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
|