Programming Fundamentals
| Details | Week 4 Tutorial |
|---|---|
| Pre-Teaching Checklist |
Everyone:
Tutors:
|
| Goals |
|
Help Sessions
- Remind students that Help sessions are currently running every week.
- Demonstrate how to access the Help Session Timetable, and tell them that they can refer to the Help Session Teams that they were all added to on Microsoft teams for further instructions on how to join the help session queue.
Census Date
Reminder: This Thursday is Census Date, please chat to your tutor or email cs1511@unsw.edu.au if you have any questions about it.
Revision Sessions
We will be running some revision sessions this week (Week 4) to help you catch up and consolidate course content we have covered so far! Revision sessions are flexible in its structure aimed to cater for the attendees of the session. Please sign up for the revision sessions via this link (Access code: "COMP1511").
The sessions will run:
Revision sessions (Week 4):
- Week 4 Wednesday 08/10/2025 2PM-4PM - Lyre Lab (K17 G12)
- Week 4 Thursday 09/10/2025 12PM-2PM - Online via COMP1511 Help Session + Revision Session Microsoft Teams
Revision Videos
We know that understanding key concepts can sometimes feel overwhelming, so we’ve created a series of short-form videos to help you quickly revise important topics from the course! These videos provide concise, easy-to-understand explanations of core concepts in the course, perfect for refreshing your memory. You can find our more here.
Assignment 1
Reminder: Assignment 1 has been released, it is available on the course website here and is due Monday 27th October at 17:00. It is a recommendation that you start early and watch the Assignment 1 Overview! If you have any questions about the assignment, please ask your tutor, attend a help session or make a post on the course forum.
Tutorial Overview:
- Part 1: Arrays Practice
- Part 2: Functions Practice
- Part 3: Style
Part 1: Arrays Practice
In this short section, we will discuss arrays, and complete a program to practice writing arrays.
Tutor Demo! (10 mins)
We have been provided the following program that intends to do the following steps but has some flaws.
- Create an integer array with at least 5 elements.
- Create a
whileloop which loops through every element of the array. - Write an
ifstatement which adds 1 to each even value. Do this within thewhileloop. - Write another while loop which goes through the array with a different iterator (i.e. if you used
ilast time, usej) - Print out the values in the array.
// part1_arrays.c, odd_only
//
// Written by YOUR-NAME (YOUR-ZID)
// on TODAYS-DATE
//
// This program adds 1 to any even element in an array and after prints
// all elements in the array
#include <stdio.h>
#define SIZE 5
int main(void) {
int array = {1, 2, 3, 4, 5};
int i = 0;
while (i < SIZE) {
if (array.i % 2 == 1) {
array.i += 1;
}
}
int j = 1;
while (j < SIZE) {
printf("%c ", array.j);
}
printf("\n");
return 0;
}
We will go through and debug the program so it executes as expected.
Your turn! (10 mins)
Copy Array
- Create an array of doubles with 3 elements, each with a non-zero value.
- Create another array of doubles with 10 elements where every element initialised to
0.0. - Create a while loop that loops through every element of the first array.
- Copy the elements of the first array into the second array (leave 0's at the end)
- Create a while loop that prints out all the elements of the second array.
Largest Character
- Create a character array with exactly 8 elements.
- Create a character variable called
largest_character, equal to the first character of the array. - Create a while loop to loop through the character array.
- Create an if statement to check if the current character has a higher ascii value than "largest_character"
- Print out the largest character you've found.
Divide the class into groups and assign them either Copy Array or Largest Character to start. If a group finishes early they can try solve the other problem.
(Online only): This task might require a bit of modification. You should setup a shared text area for students (perhaps repl.it with different files for each student), and use breakout rooms to get students working on the tasks.
Run the task. You should walk around, and be the "compiler" (tell students if they have an error in a particular place). If you find students are struggling, try and give them some hints.
If you find this activity is taking too long, just have each group do either Copy Array or Largest Character.
Part 2: Functions Practice
Tutor Demo! (10 mins)
The parts of a function
return_type function_name(parameters) {
// function body
return value;
}
Explain the parts of a function through creating a simple program which calculates and returns the sum of two numbers. Make sure to cover the following points:
- The function prototype is above the main function and defines the function's name,
return type, and parameters (if any). It follows the syntax:
return_type function_name( parameter_1 type parameter 1, parameter_2 type parameter_2, ...); - The function body contains the actual code that the function executes.
It is enclosed within curly braces
{}. - If the function has a return type other than void, it must contain a return statement to return a value to the caller.
- Parameters are placeholders for values that are passed into the function when it is called. They are defined in the function header and used within the function body.
- Local variables are declared within the function body and are only accessible within that function. They store temporary data used during the function's execution.
- The function is invoked or called by its name followed by parentheses
()containing the arguments (if any) being passed to it.
Solution below:
// functions.c
//
// Written by Sofia De Bellis (z5418801)
// on March 2024
//
// This program is a simple demonstration of functions
#include <stdio.h>
// Function prototype
int add(int number1, int number2);
int main() {
int result;
int num1 = 5;
int num2 = 3;
// Function call
result = add(num1, num2);
printf("Result: %d\n", result);
return 0;
}
// Function definition
int add(int number1, int number2) {
// Function body
int sum = number1 + number2;
// return statement
return sum;
}
Your turn! (20 mins)
We have been provided a program which simulates a bubble tea shop and is very similar to last weeks coffee shop activity. All the code for the program is currently in the main function and this task involved you working together to refactor the program to use a series of functions.
// bubble_tea.c
//
// Written by YOUR-NAME (YOUR-ZID) on TODAYS-DATE
//
// This program is a simple bubble tea store used to teach functions
#include <stdio.h>
#define LARGE 'L'
#define REGULAR 'R'
#define ADDED_COST 0.5
#define BASE_TEA 10
#define BASE_TOPPINGS 5
#define BASE_COST 6.5
// enum definitions
enum bubble_tea_type {
MILK_TEA,
FRUIT_TEA,
TARO_MILK,
MATCHA_LATTE,
};
enum topping_types {
TAPIOCA_PEARLS,
JELLY,
POPPING_PEARLS,
ALOE_VERA,
};
// struct definitions
struct bubble_tea {
enum bubble_tea_type type;
enum topping_types topping;
int topping_qty;
char size;
};
struct inventory {
int tea;
int toppings;
};
////////////////////////////////////////////////////////////////////////////////
////////////////////////////// FUNCTION PROTOTYPES /////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// TODO: Add functions prototypes here
////////////////////////////////////////////////////////////////////////////////
////////////////////////////// MAIN FUNCTION ///////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
int main(void) {
struct inventory inventory;
inventory.tea = BASE_TEA;
inventory.toppings = BASE_TOPPINGS;
// Take the user's bubble tea order
struct bubble_tea order;
int type;
printf("Enter bubble tea type (0: MILK TEA, 1: FRUIT TEA, 2: TARO MILK, "
"3: MATCHA LATTE): ");
scanf("%d", &type);
if (type == 0) {
order.type = MILK_TEA;
} else if (type == 1) {
order.type = FRUIT_TEA;
} else if (type == 2) {
order.type = TARO_MILK;
} else if (type == 3) {
order.type = MATCHA_LATTE;
}
printf("Enter topping (0: TAPIOCA PEARLS, 1: JELLY, 2: POPPING PEARLS, "
"3: ALOE VERA): ");
scanf("%d", &type);
if (type == 0) {
order.topping = TAPIOCA_PEARLS;
} else if (type == 1) {
order.topping = JELLY;
} else if (type == 2) {
order.topping = POPPING_PEARLS;
} else if (type == 3) {
order.topping = ALOE_VERA;
}
printf("Enter topping qty: ");
scanf("%d", &order.topping_qty);
printf("Enter size (L for Large, R for Regular): ");
scanf(" %c", &order.size);
// Check available stock and end the program if insufficient stock
if (inventory.tea - 1 <= 0 ||
inventory.toppings - order.topping_qty <= 0) {
printf("Sorry, we cannot fulfill your bubble tea order due to "
"insufficient stock.\n");
return 1;
}
// Calculate the cost
double total_cost = BASE_COST;
if (order.size == LARGE) {
total_cost += ADDED_COST;
}
if (order.type == MILK_TEA ||
order.type == TARO_MILK ||
order.type == MATCHA_LATTE) {
total_cost += ADDED_COST;
}
int count = 0;
while (count < order.topping_qty) {
total_cost += ADDED_COST;
count++;
}
// Print the final order and cost
printf("Order:\n");
printf("Type: %d\n", order.type);
printf("Topping Qty: %d\n", order.topping_qty);
printf("Size: %c\n", order.size);
printf("Total Cost: %.2lf\n", total_cost);
// Update the stock after fulfilling the order
if (order.size == LARGE) {
inventory.tea -= 2;
} else {
inventory.tea -= 1;
}
inventory.toppings -= order.topping_qty;
// Print the remaining inventory
printf("The current inventory is: ");
printf("Bubble tea(s): %d\n", inventory.tea);
printf("Topping(s): %d\n", inventory.toppings);
return 0;
}
////////////////////////////////////////////////////////////////////////////////
///////////////////////////// FUNCTION DEFINITIONS /////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// TODO: Write function definitions here
Extra Tip: When to Create Your Own Functions?
In the above activity, we have seen functions created for the specific context. However, when you are creating your own functions, if you are unsure what code you should put into functions, here are some example operations that programmers often create functions for:
- Printing out values from an array
- Checking inputs from the user are valid (e.g. within a valid range of values)
- Modifying arrays in a specific way (e.g. sorting an array of integers in ascending order)
- Searching for a particular value in a collection such as an array
- Mathematical operations which require multiple lines of code
- Handling and printing error messages
- Memory allocation and value initialisation for a data structure (this will be addressed later in the term)
Part 3: Style (10 mins)
This Kahoot will cover points from the COMP1511 Style guide