These questions have not been updated this term. They may be useful for revision,
but will not be used as part of teaching.
Functions
What is a function?
What functions have we learnt and used so far?
How do functions work? What are the benefits of duplicating our code in the main function vs. using functions?
(Hint: Imagine having to implement printf() yourself everytime you want to use it)
Here is an example of a function, what is its structure? What keywords are used?
What function type tells C that a function will not return anything? Why would we want a function that does not return any information back?
Create a function called is_even which takes in an integer
and returns 1 if it is an even number, and 0 if it is not.
Here is a program change_number.c, what will this code print before
and after the function is called?
#include<stdio.h>voidchange_number(intnum);intmain(void){intnum;printf("Enter a number: ");scanf("%d",&num);printf("Before function: %d\n",num);change_number(num);printf("After function: %d\n",num);return0;}// Change the given variable "num" to be the value 10voidchange_number(intnum){num=10;}
Arrays
What is an array?
Create an array called zeros of a size 7 and initialise
all of the elements to 0.
How do we access elements of an array? How do we print out the elements of the zeros array above to ensure they are all 0?
How would we do the same thing but this time have the elements initialised to 1?
If an array is declared as int numbers[20]; and your
program assigns a value to each element in the array, what is the problem with
the statement int x = numbers[20];?
Write a function array_print(int length, int arr[]) that prints all the contents in
an array, separated by new lines. Why do we need a length parameter?
Here is a program change_element.c, what will this code print before and after the function is called? Is this output expected?
#include<stdio.h>#define SIZE 1000voidchange_array_element(intarr[SIZE],intindex);intmain(void){intarray[SIZE]={0};intindex;printf("Enter index to change: ");scanf("%d",&index);printf("Before function: %d\n",array[index]);change_array_element(array,index);printf("After function: %d\n",array[index]);}// Change an element of an array at a given index to// now have 42voidchange_array_element(intarr[SIZE],intindex){arr[index]=42;}
Write a C code function to store, in each element of this array, the
square of the index of that element, e.g., squares[5] would
contain the value 25.
Using scanf() in a loop
scanf is a function that can have a result. This result
will usually be the number of values it has read and assigned to variables.
For example:
intresult=scanf("%d",&variable);
The variable result will have the value 1 if one integer was read from
the user. We call input from the user "standard input".
What would scanf() return in each of these cases?
Scanf // Input
13
20.5
apple
22 21
37.5 42
56 hello
hello 56
scanf("%d");
scanf("%lf");
scanf("%c");
scanf("%d %d");
Discuss how we could create a loop that would continually read values in
from standard input until the user inputs anything other than a number (or the
input ends).
This question will be continued in one of the Lab exercises this
week.
Write a program, character_loop.c that scans in characters
until CTRL+D is pressed in VLAB (other platforms may differ!), at which point, the program ends. The program
should print out the character if it is a letter, e.g. 'a' to 'z' including
upper/lowercase.
An example of the program output is below.
./character_loopa
'a' scanned in!
z
'z' scanned in!
A
'A' scanned in!
Z
'Z' scanned in!
.;{B
'B' scanned in!
^4M
'M' scanned in!
$
Debugging
Name all the elements of good style that you can remember!
This program 'won't work'. What's wrong with it? Split off into groups and discuss why. Line numbers are included for reference.
1// The Bureau of Meteorology's flood watch system.2// The program will warn the user if there is an3// imminent flood, and keeps a history of the last4// 100 floodwater depths.5// The user can input floodwater depths until CTRL+D6// is pressed.78#include<stdio.h>910intmain(void){11intMAX_HISTORY=100;1213// Variable to store floodwater depth14inta;1516// Variables to store history of floodwater_depths17inttotal_depths=0;18int[MAX_HISTORY]past_floodwater_depths;1920while(scanf("%c",#a)==2){21// Check for floods22flooood_warning(a);2324// Record floodwater depth25past_floodwater_depths[total_depths]=a;2627// Increment for next depth recording28total_depths+=100;29}3031printf("The second recorded floodwater depth is %d.\n",past_floodwater_depths[2]);3233intflood_warning(charfloodwater_depth){34if(floodwater_depth>15){35printf("There is an imminent flood. Please evacuate the area.\n");36}else{37printf("There is no imminent flood. Please remain cautious of heavy rainfall.\n");38}39}4041}
CS Explorer
CS Explorer is your second major assignment this term! The theme of the assignment
is a dungeon explorer game. Just like Assignment 0, have fun, and if you have any questions about the assignment,
feel free to ask in your lab, on the forum, or in a
help session!
Shortly, fast-passes will be added to Hale for help sessions for the next few weeks.
The assignment specification is found on the top bar - just click the button that says "Assignment 1"!
Have you watched Sasha’s lecture detailing the assignment?
When is the CS Explorer Livestream?
Have you downloaded the starter code and have it ready to work on in your
coding environment?
Census Date
Reminder: This Sunday is Census Date, please chat to your tutor if you have any questions
about it.
Part 1: Arrays Practice
In this short section, we will discuss arrays, and complete an "arrays round robin" task, to practice writing
arrays.
You will use these tasks as part of this tutorial:
Setup
Create three procedures, named "odd_only", "copy_array", and "largest_character"
Odd Only (Tutor Demo)
Create an integer array with at least 5 elements.
Create a while loop which loops through every element of the array.
Write an if statement which adds 1 to each even value. Do this within the while loop.
Write another while loop which goes through the array with a different iterator (i.e. if you used i last time, use j)
Print out the values in the array.
Copy Array (Student Round Robin)
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 (Student Round Robin)
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.
Ensure your code would pass "1511 style"
Go join other teams, and sit with their groups to help them finish.
Part 2: Scanning in Loops (with a fixed number of inputs)
In this section, we will write a program that scans a fixed number of values into an array.
Part 3: Scanning in Loops (with a variable number of inputs)
In this section, we will write a program that will keep scanning in values
until the user presses CTRL + D.
The following line of code will be useful to begin this process:
intresult=scanf("%d",&variable);
Part 4: Functions Practice
In this section, we will write a program that create functions to help us
deal with colours.
The following struct will be useful for this tutorial:
structcolour{intred;intgreen;intblue;};
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 ints 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)