Week 03 Extra Exercises
Information
- This page contains extra exercises for week 03.
- These exercises are not compulsory, nor do they provide any marks in the course.
- You cannot submit any of these exercises, however autotests are available for them (Command included at bottom of each exercise).
Exercise
(●◌◌)
:
Loop Sum
Write a program called loop_sum.c
that reads an integer
n from standard input, and then scans in n integers from
standard input, adds them together, then prints the sum.
You can assume that n is non-negative (>= 0).
You can assume that you are given exactly n + 1 integers.
You can assume that scanf succeeds (you do not need to check for errors).
Make your program match the examples below exactly.
Note: you are not permitted to use an array in this exercise.
Examples
dcc loop_sum.c -o loop_sum ./loop_sum How many numbers: 2 1 2 The sum is: 3
How many numbers: 3 -3 4 13 The sum is: 14
How many numbers: 5 -2 -1 0 1 2 The sum is: 0
When you think your program is working,
you can use autotest
to run some simple automated tests:
1091 autotest loop_sum
Exercise
(●◌◌)
:
What Zodiac Animal are you?
Download zodiac_animal.c here
Or, copy these file(s) to your CSE account using the following command:
1091 fetch-activity zodiac_animal
Complete the C program zodiac_animal.c
, which should first ask the user to
enter their birth year. It should then print out what (approximate) Chinese
zodiac animal that year corresponds to.
(For simplicity, we'll assume that the each calendar year corresponds exactly to a zodiac animal).
The starter code contains an enum zodiac_animal
, which defines the different
Chinese zodiac animals.
In your program, you should aim to:
- Scan in the year (as an integer)
- Calculate which zodiac animal that year corresponds to, and convert it into an
enum zodiac_animal
variable - Use that enum to decide which message to print out.
Examples
dcc zodiac_animal.c -o zodiac_animal ./zodiac_animal Enter the year of your birth: 1999 You were born in the year of the Rabbit! ./zodiac_animal Enter the year of your birth: 2000 You were born in the year of the Dragon! ./zodiac_animal Enter the year of your birth: 2001 You were born in the year of the Snake! ./zodiac_animal Enter the year of your birth: 2002 You were born in the year of the Horse! ./zodiac_animal Enter the year of your birth: 2003 You were born in the year of the Goat! ./zodiac_animal Enter the year of your birth: 2004 You were born in the year of the Monkey! ./zodiac_animal Enter the year of your birth: 2005 You were born in the year of the Rooster! ./zodiac_animal Enter the year of your birth: 1900 You were born in the year of the Rat! ./zodiac_animal Enter the year of your birth: 1901 You were born in the year of the Ox! ./zodiac_animal Enter the year of your birth: 2022 You were born in the year of the Tiger! ./zodiac_animal Enter the year of your birth: 1970 You were born in the year of the Dog! ./zodiac_animal Enter the year of your birth: 1971 You were born in the year of the Pig!
Assumptions/Restrictions/Clarifications
- You can assume the year entered will be no earlier than 1900, (we will not test any years before 1900).
- You may assume that your program will only scan in integers
- Your program is not required handle any years before 1900 (You don't need to check this).
When you think your program is working,
you can use autotest
to run some simple automated tests:
1091 autotest zodiac_animal
Exercise
(●●◌)
:
Sawtooth
Write a program sawtooth.c
that reads in two non-negative integers, a
height
and a length
. The height
represents how tall the sawtooth pattern
will be and the length
represents how long the sawtooth will be.
A sawtooth pattern looks like the below:
* * * * * * ** ** ** ** ** ** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ** ** ** ** ** *
In this example, the height
is 6
and the length
is 36
Examples
dcc sawtooth.c -o sawtooth ./sawtooth Please enter the height of the sawtooth: 4 Please enter the length of the sawtooth: 16 * * * * ** ** ** ** * * * * * * * * * ** ** ** * ./sawtooth Please enter the height of the sawtooth: 6 Please enter the length of the sawtooth: 36 * * * * * * ** ** ** ** ** ** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ** ** ** ** ** * ./sawtooth Please enter the height of the sawtooth: 8 Please enter the length of the sawtooth: 45 * * * * * * ** ** ** ** ** ** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ** ** ** ** **
Assumptions/Restrictions/Clarifications
- All heights will be non-negative integers
- All lengths will be positive integers. Although a length of
0
is possible, it can be interpreted differently and will not be tested.
When you think your program is working,
you can use autotest
to run some simple automated tests:
1091 autotest sawtooth
Exercise
(●●◌)
:
Circle Generator
Write a program called circle_generator.c
that prompts the user to enter an
integer r
that corresponds to the radius of the circle. They will also be
prompted as to whether the circle is hollow or not.
The program will then print a coloured circle of radius r
with an outline.
Examples
dcc circle_generator.c -o circle_generator ./circle_generator Please enter a radius: 4 Is the circle hollow? y . . # # # # # . . . # . . . . . # . # . . . . . . . # # . . . . . . . # # . . . . . . . # # . . . . . . . # # . . . . . . . # . # . . . . . # . . . # # # # # . . ./circle_generator Please enter a radius: 4 Is the circle hollow? n . . # # # # # . . . # * * * * * # . # * * * * * * * # # * * * * * * * # # * * * * * * * # # * * * * * * * # # * * * * * * * # . # * * * * * # . . . # # # # # . . ./circle_generator Please enter a radius: 15 Is the circle hollow? n . . . . . . . . . . # # # # # # # # # # # . . . . . . . . . . . . . . . . . . # # * * * * * * * * * * * # # . . . . . . . . . . . . . . # # * * * * * * * * * * * * * * * # # . . . . . . . . . . . # # * * * * * * * * * * * * * * * * * # # . . . . . . . . . # * * * * * * * * * * * * * * * * * * * * * # . . . . . . . # * * * * * * * * * * * * * * * * * * * * * * * # . . . . . # # * * * * * * * * * * * * * * * * * * * * * * * # # . . . . # * * * * * * * * * * * * * * * * * * * * * * * * * # . . . # * * * * * * * * * * * * * * * * * * * * * * * * * * * # . . # * * * * * * * * * * * * * * * * * * * * * * * * * * * # . # * * * * * * * * * * * * * * * * * * * * * * * * * * * * * # # * * * * * * * * * * * * * * * * * * * * * * * * * * * * * # # * * * * * * * * * * * * * * * * * * * * * * * * * * * * * # # * * * * * * * * * * * * * * * * * * * * * * * * * * * * * # # * * * * * * * * * * * * * * * * * * * * * * * * * * * * * # # * * * * * * * * * * * * * * * * * * * * * * * * * * * * * # # * * * * * * * * * * * * * * * * * * * * * * * * * * * * * # # * * * * * * * * * * * * * * * * * * * * * * * * * * * * * # # * * * * * * * * * * * * * * * * * * * * * * * * * * * * * # # * * * * * * * * * * * * * * * * * * * * * * * * * * * * * # # * * * * * * * * * * * * * * * * * * * * * * * * * * * * * # . # * * * * * * * * * * * * * * * * * * * * * * * * * * * # . . # * * * * * * * * * * * * * * * * * * * * * * * * * * * # . . . # * * * * * * * * * * * * * * * * * * * * * * * * * # . . . . # # * * * * * * * * * * * * * * * * * * * * * * * # # . . . . . # * * * * * * * * * * * * * * * * * * * * * * * # . . . . . . . # * * * * * * * * * * * * * * * * * * * * * # . . . . . . . . . # # * * * * * * * * * * * * * * * * * # # . . . . . . . . . . . # # * * * * * * * * * * * * * * * # # . . . . . . . . . . . . . . # # * * * * * * * * * * * # # . . . . . . . . . . . . . . . . . . # # # # # # # # # # # . . . . . . . . . .
Assumptions/Restrictions/Clarifications
- You can assume that the radius given is non-negative and valid
- You can assume that hollowness of the circle will be either 'y' or 'n'
- You can calculate the distance between two points by finding their Euclidean distance
- To determine if a point is in the border region of the circle, perform the
following steps:
- Take the distance from the point to the center of the circle
- If the integer component of this distance is equal to the radius, the point is on the border
#include <math.h>
at the top of your code to use thesqrt()
function. This function takes in adouble
and returns the square root of it- You are not permitted to use an array in this exercise
</math.h>
When you think your program is working,
you can use autotest
to run some simple automated tests:
1091 autotest circle_generator
Exercise
(●●●)
:
Numbered Hourglass
Write a program called numbered_hourglass.c
that prompts the user to enter an
integer n
and prints an n x n
pattern containing an hourglass. This
hourglass will contain positive integers that decrease from the middle, all
surrounded by hyphens "-".
For examples:
Please enter a size: 9 - - - - - - - - - - 1 1 1 1 1 1 1 - - - 2 2 2 2 2 - - - - - 3 3 3 - - - - - - - 4 - - - - - - - 3 3 3 - - - - - 2 2 2 2 2 - - - 1 1 1 1 1 1 1 - - - - - - - - - -
This exercise is designed to give you practice with while loops and if statements. Do not use arrays for this exercise!
Examples
dcc numbered_hourglass.c -o numbered_hourglass ./numbered_hourglass Please enter a size: 3 - - - - 1 - - - - ./numbered_hourglass Please enter a size: 7 - - - - - - - - 1 1 1 1 1 - - - 2 2 2 - - - - - 3 - - - - - 2 2 2 - - - 1 1 1 1 1 - - - - - - - - ./numbered_hourglass Please enter a size: 15 - - - - - - - - - - - - - - - - 1 1 1 1 1 1 1 1 1 1 1 1 1 - - - 2 2 2 2 2 2 2 2 2 2 2 - - - - - 3 3 3 3 3 3 3 3 3 - - - - - - - 4 4 4 4 4 4 4 - - - - - - - - - 5 5 5 5 5 - - - - - - - - - - - 6 6 6 - - - - - - - - - - - - - 7 - - - - - - - - - - - - - 6 6 6 - - - - - - - - - - - 5 5 5 5 5 - - - - - - - - - 4 4 4 4 4 4 4 - - - - - - - 3 3 3 3 3 3 3 3 3 - - - - - 2 2 2 2 2 2 2 2 2 2 2 - - - 1 1 1 1 1 1 1 1 1 1 1 1 1 - - - - - - - - - - - - - - - - ./numbered_hourglass Please enter a size: 25 - - - - - - - - - - - - - - - - - - - - - - - - - - 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 - - - 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 - - - - - 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 - - - - - - - 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 - - - - - - - - - 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 - - - - - - - - - - - 6 6 6 6 6 6 6 6 6 6 6 6 6 - - - - - - - - - - - - - 7 7 7 7 7 7 7 7 7 7 7 - - - - - - - - - - - - - - - 8 8 8 8 8 8 8 8 8 - - - - - - - - - - - - - - - - - 9 9 9 9 9 9 9 - - - - - - - - - - - - - - - - - - -1010101010 - - - - - - - - - - - - - - - - - - - - -111111 - - - - - - - - - - - - - - - - - - - - - - -12 - - - - - - - - - - - - - - - - - - - - - - -111111 - - - - - - - - - - - - - - - - - - - - -1010101010 - - - - - - - - - - - - - - - - - - - 9 9 9 9 9 9 9 - - - - - - - - - - - - - - - - - 8 8 8 8 8 8 8 8 8 - - - - - - - - - - - - - - - 7 7 7 7 7 7 7 7 7 7 7 - - - - - - - - - - - - - 6 6 6 6 6 6 6 6 6 6 6 6 6 - - - - - - - - - - - 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 - - - - - - - - - 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 - - - - - - - 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 - - - - - 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 - - - 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 - - - - - - - - - - - - - - - - - - - - - - - - - -
Assumptions/Restrictions/Clarifications
- You can assume that
n
will always be odd - You can assume that
n >= 3
When you think your program is working,
you can use autotest
to run some simple automated tests:
1091 autotest numbered_hourglass