Week 02 Tutorial Sample Answers
Tutorial Overview:
- Part 1: Variables and constants
- Topics Covered: Declaring and initialising variables, data types, Calculating Values in Programs.
- Part 2: Calculating values in programs
- Topics Covered: Arithmetic, Logic, Comparitive operators.
- Part 3: Weird Data Types and Arithmetic
- Topics Covered: Complex Arithmetic expressions, order of operations.
- Part 4: Diagramming
- Topics Covered: Flow charts, If statements
- Part 5: Practical Programming Exercise
- Topics Covered: If statements, scanf.
Part 1: Variables and constants (5 mins)
Objectives: Understand how to declare and initialise variables and understand the different data types. Understand how and when to use constants.
In this short activity we will recap the parts of a variables and how to declare, initialise and print variables working off of the starter code below:
// part1_variables
//
// This program was written by YOUR-NAME (ZID),
// on [DATE]
//
// This program calculates the area of a circle
#include <stdio.h>
int main(void) {
// 1. Declare the variables
// 2. Initalise the variables
// 3. Calculate the area of the circle
// 4. Print the result
return 0;
}
In this activity we will write a short program that takes input from the user to calculate the area of a circle. In this activity we will recap the parts of variables, how to declare, initalise and print variables and also revise when to use constants.
Activity instructions:
While completing the program ask yourself:
- What are the three data types they have learnt so far?
int
,char
anddouble
.
- How do we declare a variable?
- In the form
data_type variable_name;
- In the form
- How do we initialise variables?
variable_name = value;
- How do we print the value of variables?
- We can print out values using the
printf(...)
function in the C standard input/output library. We can use this library and the fun tions available in it by adding#include <stdio.h>
at the top of our program.
- We can print out values using the
Note:
- First complete the program with the value of Pi not extracted to a constant, i.e.
area = radius * 3.14159
- Note that that since Pi is a magic number and the value of it will never change we should declare it as a constant at the top of our program.
Solution below:
// part1_variables
//
// This program was written by Sofia De Bellis (z5418801)
// on January 2024
//
// This program calculates the area of a circle
#include <stdio.h>
#define PI 3.14159
int main(void) {
// Declaring the variable
double radius;
printf("Please enter the radius of your circle: ");
// Initalising the variable with user input
scanf("%lf", &radius);
// Calculating the area
double area = (radius * radius) * PI;
printf("The area of the circle with a radius of %lf is %lf\n",
radius, area);
return 0;
}
Part 2: Calculating Values in Programs (5 mins)
Objectives: Understand the different types of operators, and how to use them.
In this short section, we will review what tools we have for doing mathematics in our programs. This section will be completed quickly, to make sure we have time for the rest of the tutorial.
Activity Setup:
Split into groups.
Setup whiteboard space and write down the operators. The full list you should get is shown below:
Type of Operator | Opterators |
Arithmetic | + - / % |
Logic | && || ! |
Comparison | < > <= >= != == |
If you think an operator is not on this list: Don't include it, but feel free to quickly discuss.
Some common culprits are
^
(which does NOT mean exponentiation);- other bitwise operators (not covered in 1091),
++
and--
(which are operators, but we won't count for our purposes today).
Activity instructions:
- Assign each group a type of operator (Arithmetic | Logic | Comparison).
- Give groups 60 seconds to recall as many operators of that type as possible.
- Go around each group in turn, and let them tell you one operator they found that isn't on the board yet.
- Ask yourself to compare what
%
does, vs./
.
Part 3: Weird Data Types and Arithmetic (10 mins)
In this short section, we will review how you can store values in programs.
These are short arithmetic expressions we will use during this tutorial:
(7 / 2)
(3.0 / 2) + 1
'a' + 5
'F' - 'A' + 'a'
double
, int
and char
.
Remind that how an integer divided by an integer is an integer, and a char plus an integer can be
a char.
Split your class into groups and assign one of the following to each group.
Ask them to tell you both what the 'mathematical' or 'sensible' answer is (if there is one), and
what the 'C' answer is.
Part 4: Diagramming (15 mins)
Objectives: Understand Flowcharts, and how to use them to break down a problem.
In this section, we'll work on diagramming a small program.
Flow Charts:
We'll start by discussing the following flowchart:
Print "Do you like puns?"
scan input
if yes:
print "Do you like computers more than books?"
scan input
if yes:
print "You will be glas to C this pun, then."
else:
print "I hope the police book you fro saying that"
else:
print "That's pUnfortunate."
Part 5: Practical Programming Exercise (rest of tutorial)
Objectives: Understand if statements, scanning and constants.
In this activity, you'll be writing a program to determine whether someone is tall enough to ride a roller coaster. It should:
- Scan in the users height.
- If the height is 0 or less, it should print an error message,
- If the height is below the minimum height, it should print a message telling the user they are not tall enough to ride,
- If the hight is above the minimum but below the ride alone threshold, it should print a message telling the user they can ride with an adult,
- If the height is or is above the ride alone threshold, it should print a message telling the user they can ride.
For the purposes of this activity the minimum height to ride if 100cm
and the minimum height to ride alone is 160cm
.
// part5_roller_coaster
//
// This program was written by YOUR-NAME, ZID
// on [DATE]
//
// This program:
// 1. Scans in the users height.
//
// 2. If the height is 0 or less,
// it should print an error message
//
// 3. If the height is below the minimum height,
// it should print a message telling the user they are not tall enough to ride
//
// 4. If the height is above the minimum but below the ride alone threshold,
// it should print a message telling the user they can ride with an adult
//
// 5. If the height is or is above the ride alone threshold,
// it should print a message telling the user they can ride.
#include <stdio.h>
int main(void) {
return 0;
}
Activity instructions
- Write a solution to the activity as a class taking suggestions for what to do at each stage from the class
- Inlcude minor bugs or typos in your code if possible to show students the debugging process with dcc and how to read error messages.
- Run
1091 style
on your solution in front of the class with1091 style <file_name>
1091 style part6_roller_coaster.c
- When considering step 4 in the problem (i.e.
If the hight is above the minimum but below the ride alone threshold, it should print a message telling the user they can ride with an adult
) write the inital solution with the AND operator (&&
), then ask students if they have any improvements for the program.- *Improve the program by removing the need for the
&&
operator, explaining that the condition was previously checked in the statement above.
- *Improve the program by removing the need for the
First solution below:
// part5_roller_coaster
//
// This program was written by Sofia De Bellis (z5418801)
// on January 2024
//
// This program:
// 1. Scans in the users height.
//
// 2. If the height is 0 or less,
// it should print an error message
//
// 3. If the height is below the minimum height,
// it should print a message telling the user they are not tall enough to ride
//
// 4. If the height is above the minimum but below the ride alone threshold,
// it should print a message telling the user they can ride with an adult
//
// 5. If the height is or is above the ride alone threshold,
// it should print a message telling the user they can ride.
#include <stdio.h>
#define MINIMUM_HEIGHT 100
#define RIDE_ALONE_HEIGHT 160
int main(void) {
printf("Please enter your height: ");
double height;
scanf("%lf", &height);
if (height <= 0) {
printf("Invalid height!\n");
} else if (height < MINIMUM_HEIGHT) {
printf("Sorry, you are not tall enough to ride :<\n");
} else if (height < RIDE_ALONE_HEIGHT && height >= MINIMUM_HEIGHT) {
printf("Yay! You are tall enough to ride. But only with an adult!\n");
} else {
printf("Congratulations! You are tall enough to ride alone!\n");
}
return 0;
}
Improved solution below:
// part5_roller_coaster_improved
//
// This program was written by Sofia De Bellis (z5418801)
// on February 2024
//
// This program:
// 1. Scans in the users height.
//
// 2. If the height is 0 or less,
// it should print an error message
//
// 3. If the height is below the minimum height,
// it should print a message telling the user they are not tall enough to ride
//
// 4. If the height is above the minimum but below the ride alone threshold,
// it should print a message telling the user they can ride with an adult
//
// 5. If the height is or is above the ride alone threshold,
// it should print a message telling the user they can ride.
#include <stdio.h>
#define MINIMUM_HEIGHT 100
#define RIDE_ALONE_HEIGHT 160
int main(void) {
printf("Please enter your height: ");
double height;
scanf("%lf", &height);
if (height <= 0) {
printf("Invalid height!\n");
} else if (height < MINIMUM_HEIGHT) {
printf("Sorry, you are not tall enough to ride :<\n");
} else if (height < RIDE_ALONE_HEIGHT) {
printf("Yay! You are tall enough to ride. But only with an adult!\n");
} else {
printf("Congratulations! You are tall enough to ride alone!\n");
}
return 0;
}