Skip to content

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

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

1
~cs1010/get-ex03
  • You should see a new subdirectory ex03-<username> in your current working directory, where username 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 and hdb.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.
    • Makefile: The configuration for the tool make that we use to automate the compilation and testing of the programs. You do not have to understand how to write a Makefile for CS1010. If you are interested to learn how to write a Makefile, talk to either Wei Tsang or Google.
    • test.sh: A bash script for testing your code. You do not have to edit this file. If you are interested to learn how to write bash script, talk to either Wei Tsang or Google.
    • compiler_flags.txt and .clang-tidy are two files used to configure clang and clang-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
@author XXXX (Group YYYY)

and change it to something like:

1
@author Alita (Group A10)

Solving The Assignments

  • Edit the files three.c, factor.c, parity.c, nine.c, and hdb.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
make

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
cat inputs/echo.3.in

To see the expected output:

1
cat outputs/echo.3.out

To see what output your program gives,

1
./echo < inputs/echo.3.in

To test the program for a particular question (say, echo), instead of all programs,

1
./test.sh echo

Submission

When you are ready, run the following command while you are in the exercise directory:

1
~cs1010/submit-ex03 

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
ooiwt@pe112:~/ex03-ooiwt$ ./three
1 10
3
6
9
ooiwt@pe112:~/ex03-ooiwt$ ./three
4 5
ooiwt@pe112:~/ex03-ooiwt$ ./three
3 24
3
6
9
12
15
18
21
24

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
ooiwt@pe112:~/ex03-ooiwt$ ./factor
48
8
ooiwt@pe112:~/ex03-ooiwt$ ./factor
49
1
ooiwt@pe112:~/ex03-ooiwt$ ./factor
1
0

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
odd: X
even: Y
where 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
albertsutz@pe119:~/ex03-albertsutz$ ./parity
123456
odd: 3
even: 3
albertsutz@pe119:~/ex03-albertsutz$ ./parity
111333
odd: 6
even: 0

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
ooiwt@pe119:~/ex03-ooiwt$ ./nine
900
3
ooiwt@pe119:~/ex03-ooiwt$ ./nine
9999999
1
ooiwt@pe119:~/ex03-ooiwt$ ./nine
123456780
0

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
ooiwt@pe119:~/2122s1/ex03-ooiwt$ ./hdb
3 3
###
###
###
ooiwt@pe119:~/2122s1/ex03-ooiwt$ ./hdb
10 15
##########
##########
##########
##########
##########
##########
##########
##########
##########
##########
##########
##########
##########
##########
##########
ooiwt@pe119:~/2122s1/ex03-ooiwt$ ./hdb
24 10
########################
########################
########################
########################
########################
########################
########################
########################
########################
########################