Week 05 Tutorial Sample Answers

Arrays Practice

This week, we will discuss arrays, and complete a program to practice writing arrays.

Diagram of an array

Tutor Demo! (10 mins)

We have been provided the following program that intends to do the following steps but has some flaws.

Flowchart of activity

// part1_arrays.c, odd_only
//
// Written by YOUR-NAME (YOUR-ZID)
// on TODAYS-DATE 
//
// This program adds 1 to any odd 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 == 0) {
            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
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. 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.