Back to main

Lab 3 for C programming

Exercises:

Exercise 8: Arrays

Background

An array is an ordered sequence of values; you cannot rearrange values without changing the meaning). A two-dimensional array is just a normal "table".

In C, arrays are indexed (accessed) starting from 0. For example,

int array[4] = {3, -2, 987, 12};

creates an array in memory, storing each value at the appropriate index:

Index0123
Value3-298712

The array's size can only be specified when you create it. If you want to store more information in that array, you need to create a new (bigger) array, then copy information from the old array to the new array.


Due to the way C handles arrays and pointers (coming later), you can modify an array inside a function without needing to return anything. The function needs to know the array's size, though!

Technical details

Initializing and displaying:

#include <stdio.h>

int main() {
    int array[8] = {1, 2, 8, 3, -5, -1, 1};

    int i;
    // display the array
    for (i=0; i<8; i++) {
        printf("%i ", array[i]);
    }
    printf("\n");

    // change the array
    for (i=0; i<8; i++) {
        array[i] = 2 * array[i] - 1;
    }

    // display the array again
    for (i=0; i<8; i++) {
        printf("%i ", array[i]);
    }
    printf("\n");
    getchar();
}

Arrays and functions:

#include <stdio.h>

void printArray(int arr[], int size) {
    int i;
    for (i=0; i<size; i++) {
        printf("%i ", arr[i]);
    }
    printf("\n");
}

void changeArray(int arr[], int size) {
    int i;
    for (i=0; i<size; i++) {
        arr[i] = arr[i] * arr[i] + 3;
    }
}

int main() {
    int array[8] = {1, 2, 8, 3, -5, -1, 1};
    printArray(array, 8);
    changeArray(array, 8);
    printArray(array, 8);
    getchar();
}

Your task...

The Fibonacci numbers are a famous sequence of numbers. They begin with 0 and 1, and then the next value in the sequence is the sum of the previous two values.

0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765

(fib[8] is 21 -- remember to start counting from 0!)

Write a program that calculates the Fibonacci sequence.

... show your work to a demonstrator


Exercise 9: Two-Dimensional Arrays

Background

Just like a one-dimensional array is an ordered sequence of values, a two-dimensional array is an ordered sequence of one-dimensional arrays. By convention, we think of these 1-D arrays as being stacked vertically.

Array NumberArray
0
1-D array01234
1
1-D array37214
2
1-D array42856

Technical details

Using 2-D arrays:

#include <stdio.h>

// we MUST specify the size for a 2-D array
// (unless you use pointers)
void printArray(int values[5][3]) {
    int i, j;
    for (i=0; i<5; i++) {
        for (j=0; j<3; j++) {
            printf("%i ", values[i][j]);
        }
        printf("\n");
    }
}

int main() {
    int array[5][3];
    int i, j;

    // fill array with some values
    for (i=0; i<5; i++) {
        for (j=0; j<3; j++) {
            array[i][j] = 3*i + j;
        }
    }
    printArray(array);
    getchar();
}
C does not check that you are only accessing an array within the bounds of the array. If you ask for array[-1][-1] then the computer will tell you what was at that memory location, even though that memory does not belong to that variable!

Your task...

Write a program which creates a "game board". We will use this in the next exercise to write a tic-tac-toe game (also known as "noughts and crosses", "tick tack toe", "X's and O's"). If you're not familiar with the game, see Wikipedia's page on tic-tac-toe, or ask a lab instructor.

... show your work to a demonstrator

Move on to Lab 4


Creative Commons LicenseUnless otherwise noted, all materials on these pages are licenced under a Creative Commons Licence.