Programming Fundamentals
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 thewhile
loop. - Write another while loop which goes through the array with a different iterator (i.e. if you used
i
last time, usej
) - 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:
int result = 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:
struct colour {
int red;
int green;
int blue;
};
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)