CSC 101 Lab Week 5

Array Math and Hangman

DUE: Tuesday, 3 May 2011, at the beginning of lab

Overview

There are two exercises for this lab. The first exercise is about using arrays to do some simple math calculations. The second exercise is the game of hangman.

The first exercise involves processing that is similar to what you will be doing in Program 3. Both exercises involve the use of loops and arrays.

Exercise 1: Array Math

Write a program that inputs two lists of numbers and stores them in arrays named x and y. The lists have the same length, and a maximum length of 20 numbers. The lists are stored in two files named in1.dat and in2.dat. The C functions to open and read from files are summarized on Page 100 of the book. When reading the files, use the end-of-file sentinel style of loop discussed on pages 254 and 255 of the book.

Once the numbers are read in, your program performs and displays the results of two computations. For the first computation, compute the product of corresponding elements of x and y and store the results in a third array named z. Output the results of this computation in a three- column table, where the columns show the values of the arrays x, y, and z. Each column should be 12 characters wide. All numeric values should be shown to three decimal places of precision.

For the second computation, compute and display the square root of the sum of the items in z. The output is a single line that shows the result of the computation.

Make up your own data, and be sure to test your program on at least one data set with number lists of exactly 20 items. One data set should have lists of 21 or greater, and one set should have significantly shorter lists. It is up to you to determine how best to handle lists with greater than 20 elements. You should inform the user in an appropriate way, and for sure, your program should not crash or otherwise mis-behave with lists of greater than 20 elements.

Name this program arraymath.c.

Exercise 2: Hangman

Write an interactive program that plays a game of hangman. For those of you who may not know this game, it's described here: http://en.wikipedia.org/wiki/Hangman_(game). Your program will let a user play hangman in a form like the Wikipedia "Example Game", but without drawing the pictures.

Here's an outline of your program design. Store the word to be guessed in successive elements of an array of individual characters called word. The player will guess the letters belonging to word by inputting successive letters on the terminal. The program should terminate when either all letters have been guessed correctly (the player wins) or a specified number of incorrect guesses have been made (the computer wins). Hint: Use another array, guessed, to keep track of the solution so far. Initialize all elements of guessed to the '_' character. Each time a letter in word is guessed, replace the corresponding '_' in guessed with that letter.

Name this program hangman.c.

Interface Design for Hangman

You are free to design your own hangman user interface, as long as the requirements described above are met. When we review your game, it will be assessed on the quality of the interaction with the user. Your program should be intuitive when run by someone that has not used it before, should prompt the user when appropriate, and present output in a well formatted manner.

You are encouraged to have your classmates, friends, roommates and others run and evaluate your program before you hand it in. Use feedback from users to enhance the input and output interaction of your program.

One of the more interesting aspects of a hangman program is how the word to be guessed is stored in the program. It's OK for this lab if your program is compiled with a fixed word, and in order to change it, you need to recompile the program. However, you should think about alternative, more dynamic ways to come up with the word to be guessed.

Makefile

You are required to write a Makefile for this program. It should allow you to build the two programs with the commands:
gcc -ansi -pedantic -Wall -Werror -lm -o arraymath arraymath.c
gcc -ansi -pedantic -Wall -Werror -lm -o hangman hangman.c

Submitting Your Program

After your program has been checked in person during lab, submit it on vogon using the command:
    handin djanzen 101_lab5 arraymath.c hangman.c Makefile
Complete this survey.