Week 04 Tutorial Questions
Tutorial Overview:
- Part 1: Arrays Practice
- Part 2: Functions Practice
- Part 3: Style
First Hour: Arrays Practice
In this short section, we will discuss arrays, and complete a program to practice writing arrays.
Tutor Demo!
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!
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.
Solution below:
// part1_arrays, even_only // // Written by Sofia De Bellis, z5418801 // on March 2024 // // This program adds 1 to any even values in an array #include <stdio.h> #define SIZE 5 int main(void) { int array[SIZE] = {1, 2, 3, 4, 5}; int i = 0; while (i < SIZE) { if (array[i] % 2 == 0) { array[i] += 1; } i++; } int j = 0; while (j < SIZE) { printf("%d ", array[j]); j++; } printf("\n"); return 0; }
Second Hour: Functions Practice
Tutor Demo!
The parts of a function
return_type function_name(parameters) { // function body return value; }
Your turn! Tutorial Performance Hurdle
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
mainfunction 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
This Kahoot will cover points from the DPST1091/CPTG1391 Style guide