Week 5 Tutorial

In this weeks tutorial, you will be reviewing

Code Review

This week’s code review is danishFlag.

How a code review works:

The code review for next week will be the rotateThirteen activity. Select a student pair who will present their code next week.

Characters & ASCII

Work through the parrot exercise in the tutorial.

Computers store all data as numbers and text is no exception. One of the earliest standards that was used to represent text was ASCII. This used 7-bit values to represent 128 different characters, 96 printing characters and 32 control characters. This means we can store lower-case and upper-case Latin alphabet and punctuation with room to spare.

Change your code for parrot so that it turns all lower-case letters into upper-case letters.

Arrays

Construct a program that uses a function called setArrayValues with the following prototype.

void setArrayValues(int length, int array[], int value);

This function should take an array and its length and set every member of that array to the given value.

Demonstrate some unit tests you could use to show that your function works.

Strings

Strings are arrays of characters. There are several ways that a string can be created.

We can create an array of numeric values.

char string[6] = {72, 101, 108, 108, 111, 0};

We can also use character literals to construct an array.

char string[6] = {'H', 'e', 'l', 'l', 'o', '\0'};

We can also use a string literal to create a copy of it as an array.

char string[6] = "Hello";

In all of these cases, we create an array within the function that is full of characters and ends with a null terminator which is a byte with the value of zero. This tells anything reading the string where the string ends. In the cases above, we can use the array we create and change the individual characters.

Pointers to String Literals

There is a fourth way to represent a string, which we often use to pass constant strings into functions. We create a pointer to an existing character literal.

char *string = "Hello";

This is special because the string being pointed to is stored in a special area of memory that we cannot modify. If we try to modify it the program crashes and we see a segmentation fault.

Devowel

Create a program with a function called devowel with the following prototype:

void devowel(char *string);

devowel should take a single string as an argument and display it using putchar and skipping all of the vowels.

Try using the function with the different kinds of strings that were listed above without changing the function or its prototype in any way.

Pointers

Pointers allow a program to store the location of a variable and to tell a function where a variable lives so that the function can change the value that the variable stores.

This allows a function to change data in a program other than by returning a value.

If a function changes a value using a pointer it is considered a side-effect. Other side-effects a function might have include changing what is shown on the computer screen or modifying files.

Run through the swapFunction activity.

Lab Partners

This week, everyone will be changing lab partners. Make sure to select a new lab partner that you will work with for the next few weeks.

Please make sure that you create a lab pair group in WebCMS with your new partner.

In the Lab

There may look like a lot of activities this week, however, this week we are focussing on code reuse. You will find that many of the functions you write in earlier activities can be re-used to help solve later ones.