Back to main

Lab 4 for C programming

Exercises:

Exercise 10: Tic-Tac-Toe

Background

This lab combines everything we've done so far. If you didn't understand any of the previous exercises, you may want to ask questions about them before beginning this one!

Technical details

Nothing new.


Your task...

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.


(optional: instead of having the computer move randomly, try to make it play intelligently.)

... show your work to a demonstrator


Exercise 11: Strings are Arrays of Characters

Background

You may recall that in Lab 1, we defined strings variables as:

char animal[100] = "cat";

As you may guess now, we were actually creating an array of characters.


You may have noticed that we can print out a string without needing to specify the total number of characters:

char animal[100] = "ferocious kitten";
printf("The %s sleeps!", animal);

The above code will print out "The ferocious kitten sleeps", even though the char array animal contains 100 chars. This is because the final character is a special ascii-null character: '\0' (added automatically by the compiler in this case).

Technical details

Dealing with strings:

#include <stdio.h>
#include <string.h> // extra include!

int main() {
    char animal[100] = "ferocious kitten";

    // length of the string
    int length = strlen(animal);

    int i;
    for (i=0; i<length; i++) {
        printf("%c", animal[i]);
    }
    printf("\n");
    getchar();
}

We can also copy strings.

#include <stdio.h>
#include <string.h>

int main ()
{
    char orig[100]= "Cats are awesome";
    char next[100];

    // 99 is the maximum number of chars
    strncpy(next,orig,99);

    printf("%s\n", next);
    getchar();
}

Your task...

The Coleman-Liau Index test[1] is an easily-computable estimation of the difficulty of reading some text. It gives an approximate "grade level" (in US school system: grade 1 is for students 5-6 years old, and the numbers increase linearly until grade 12).

In the above formula, "character" means "letter" -- for the purposes of that formula, spaces and periods do not count as characters.

[1] Coleman, M.; and Liau, T. L. (1975); A computer readability formula designed for machine scoring, Journal of Applied Psychology, Vol. 60, pp. 283-284.
Original journal paper (campus access only)

Write a program that calculates the Coleman-Liau Index of some text.


(optional: research and implement the Automated Readibility Index (ARI). Compare those results with the Coleman-Liau results.)

... show your work to a demonstrator

Move on to Lab 5

Back to main


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