Programming Fundamentals
Information
- This page contains extra challenge exercises for week 02.
- These exercises are not compulsory, nor do they provide any marks in the course.
- These exercises are intended for students that want more challenge in the course.
- You cannot submit any of these exercises, however autotests are available for them (Command included at bottom of each exercise).
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:
1511 autotest order3
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:
1511 autotest order3_challenge1
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:
1511 autotest order3_challenge2
Exercise
(●●●)
:
Labour Day
Labour Day is a public holiday that is held in NSW on the first Monday of October. Related link https://en.wikipedia.org/wiki/Labour_Day
Write a program named labour_day.c
that outputs the date of Labour Day in NSW when provided a given year.
Examples
dcc labour_day.c -o labour_day ./labour_day Enter a year: 2023 In 2023, Labour Day is October 2nd ./labour_day Enter a year: 2031 In 2031, Labour Day is October 6th ./labour_day Enter a year: 2016 In 2016, Labour Day is October 3rd
When you think your program is working,
you can use autotest
to run some simple automated tests:
1511 autotest labour_day