DPST1091 Revision If While Functions Variables IO Exercises
Revision Exercise: Remove non-digits from input (●◌◌)
digit_filter.c
which reads characters from its input
and writes character to its output.
Digits ('0' .. '9'), spaces and newline characters should be written unchanged.
All other characters should be replaced with a '.' character.
Your program should stop only at the end of input.
For example:
dcc -o digit_filter digit_filter.c ./digit_filter This is week 9 of COMP1511. .... .. .... 9 .. ....1511. I paid $10.50 for 3 cups of coffee . .... .10.50 ... 3 .... .. ...... i is the symbol for the square root of -1 . .. ... ...... ... ... ...... .... .. .1 0123456789abcdef 0123456789......
When you think your program is working you can use autotest
to run some simple automated tests:
1091 autotest digit_filter
Revision 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
Revision Exercise: Print an Hourglass (●●◌)
Write a program called hourglass.c
that prompts the user to enter
an integer n and prints an nxn pattern containing an hourglass. This
hourglass will be made up of *'s', all surrounded by hyphens "-".
Please enter a size: 9 ********* -*******- --*****-- ---***--- ----*---- ---***--- --*****-- -*******- *********
You can assume n is >= 3.
Make your program match the examples below exactly.
This exercise is designed to give you practice with while loops and if statements. Do not use arrays for this exercise!
Note: you are not permitted to use an array in this exercise.
Examples
dcc hourglass.c -o hourglass ./hourglass Please enter a size: 3 *** -*- *** ./hourglass Please enter a size: 6 ****** -****- --**-- --**-- -****- ****** ./hourglass Please enter a size: 7 ******* -*****- --***-- ---*--- --***-- -*****- ******* Please enter a size: 10 ********** -********- --******-- ---****--- ----**---- ----**---- ---****--- --******-- -********- ********** ./hourglass Please enter a size: 15 *************** -*************- --***********-- ---*********--- ----*******---- -----*****----- ------***------ -------*------- ------***------ -----*****----- ----*******---- ---*********--- --***********-- -*************- *************** ./hourglass Please enter a size: 25 ************************* -***********************- --*********************-- ---*******************--- ----*****************---- -----***************----- ------*************------ -------***********------- --------*********-------- ---------*******--------- ----------*****---------- -----------***----------- ------------*------------ -----------***----------- ----------*****---------- ---------*******--------- --------*********-------- -------***********------- ------*************------ -----***************----- ----*****************---- ---*******************--- --*********************-- -***********************- *************************
When you think your program is working you can use autotest
to run some simple automated tests:
1091 autotest hourglass
Revision Exercise: Ordering Three Integers (●◌◌)
Write a C program order3.c
using if statements (no loops)
that reads 3 integers and prints them from smallest to largest.
Examples
dcc order3.c -o order3 ./order3 Enter integer: 23 Enter integer: 5 Enter integer: 27 The integers in order are: 5 23 27 ./order3 Enter integer: 3 Enter integer: 6 Enter integer: 27 The integers in order are: 3 6 27 ./order3 Enter integer: 9 Enter integer: 8 Enter integer: 7 The integers in order are: 7 8 9
You can assume the user supplies 3 integers. You do not have to check the return value from scanf.
When you think your program is working you can use autotest
to run some simple automated tests:
1091 autotest order3
Revision Exercise: Ordering Three Integers Without If Statements (●●●)
Write a C program order3_challenge1.c
that reads 3 integers and prints them
from smallest to largest.
You are only permitted to use parts of C covered in the weeks 1 and 2 lectures. This means,
- You are not permitted to use
if
statements - You are not permitted to use loops (e.g.
while
) - You are not permitted to use the ternary
?
operator - You are not permitted to use arrays. You are not permitted to define functions
You are not permitted to call functions other than printf
and scanf
. For
example, you are not permitted to use functions from the math
library.
You are not permitted to use printf
inside expressions - you can only use
printf
as a statement (the way it has been used in lectures).
You are not permitted to assign variables inside expressions - you can only assign variables as a statement (the way it has been done in lectures).
For example, both of these are invalid:
(a < b) && printf("a"); // invalid
(a < b) && (a = b); // invalid
You can use printf
to print the value of an expression, in other words you
can have an expression inside printf
.
Examples
dcc order3_challenge1.c -o order3_challenge1 ./order3_challenge1 Enter integer: 23 Enter integer: 5 Enter integer: 27 The integers in order are: 5 23 27 ./order3_challenge1 Enter integer: 3 Enter integer: 6 Enter integer: 27 The integers in order are: 3 6 27 ./order3_challenge1 Enter integer: 9 Enter integer: 8 Enter integer: 7 The integers in order are: 7 8 9
This is more puzzle than a programming exercise.
Try to invent your own solution - don't google or ask others how do it.
Autotest is available to help you test your program - but it doesn't check that your code meets the above restrictions.
When you think your program is working you can use autotest
to run some simple automated tests:
1091 autotest order3_challenge1
Revision Exercise: Ordering Three Integers Without If Statements and With Only Three Variables (●●●)
Write a C program order3_challenge1.c
that reads 3 integers and prints them
from smallest to largest.
You are only permitted to use parts of C covered in the weeks 1 and 2 lectures. This means,
- You are not permitted to use
if
statements - You are not permitted to use loops (e.g.
while
) - You are not permitted to use the ternary
?
operator - You are not permitted to use arrays. You are not permitted to define functions
For this exercise you also cannot use more than 3 variables
You are not permitted to call functions other than printf
and scanf
. For
example, you are not permitted to use functions from the math
library.
You are not permitted to use printf
inside expressions - you can only use
printf
as a statement (the way it has been used in lectures).
You are not permitted to assign variables inside expressions - you can only assign variables as a statement (the way it has been done in lectures).
For example, both of these are invalid:
(a < b) && printf("a"); // invalid
(a < b) && (a = b); // invalid
You can use printf
to print the value of an expression, in other words you
can have an expression inside printf
.
Examples
dcc order3_challenge1.c -o order3_challenge1 ./order3_challenge1 Enter integer: 23 Enter integer: 5 Enter integer: 27 The integers in order are: 5 23 27 ./order3_challenge1 Enter integer: 3 Enter integer: 6 Enter integer: 27 The integers in order are: 3 6 27 ./order3_challenge1 Enter integer: 9 Enter integer: 8 Enter integer: 7 The integers in order are: 7 8 9
This is more puzzle than a programming exercise.
Try to invent your own solution - don't google or ask others how do it.
Autotest is available to help you test your program - but it doesn't check that your code meets the above restrictions.
When you think your program is working you can use autotest
to run some simple automated tests:
1091 autotest order3_challenge2
Revision 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
Revision 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
When you think your program is working you can use autotest
to run some simple automated tests:
1091 autotest circle_generator
Revision 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