Objectives:
Use your knowledge of select statements, arrays, loops and string functions to write code for the Hangman game (Part 2)and to write a program that computes statistics on the words in a text file (Part 3).
Understanding function return values and input parameters.
Understand arrays of characters, arrays of strings to decrypt an encrypted message.
This lab is to be worked in groups.
Lectures 13/14 on loops, lecture 15 on arrays and lecture 16 on functions.
Answer questions 1-3 on the answer sheet.
Open up a unix terminal and make a new directory named
lab8.
cd
mkdir lab8
cd lab8
Download the files named hangdemo and hang.c
.
Run the demo program hangdemo (At the Unix prompt type: hangdemo).
./hangdemo
Watch how the demo program works. You're going to write a similar program. If you have a problem running this program then at the Unix prompt type
chmod u+rwx hangdemo
We will give you the skeleton code in hang.c that includes the steps that you need to go through in your program (see step 4. Implementation below). But of course, you can start to write the program from scratch if you prefer. You should consider using the following functions in your program:
The complete flow-chart for the program is shown
below.
Use the flow chart above and
open gedit to type your code in the file named hang.c that you
downloaded.
After you've completed your program, then answer questions #4 and
#5 on the Answer sheet.
1. Requirements Specification (Problem Definition)
In this part of the lab, you will learn how to call a C function
and pass input arguments to parameters. You will also code a
couple simple C functions which will allow you to match strings,
and calculate simple statistics.
Download three files named lab8.c, lab8.h and shelterfromthestorm.txt into your lab8 directory.
Use gedit to open the skeleton source file 'lab8.c' as your starting point.
When compiling, name your
executable file 'lab8' by using the -o switch, e.g. at the Unix prompt
type,
> gcc lab8.c -o lab8
and if you don't have any error messages, run your program by
typing,
> ./lab8
Note: not all the details of the code are listed in the steps below. For example, if you code a loop that uses the variable 'i' then it is assumed that you know that you must declare this variable in your function.
You will not have to write any prototypes since this has already
been done for you.
Use gedit to open the file 'lab8.h' (do not change anything in
this file) and notice that the prototypes for all functions in
this lab have already been typed for you.
Why should you use the lab8.h file?
Many practical programs consist of more than one function, and
each might be coded in different .c files. To make sure that the
entire program shares a consistent set of functions (names of
functions, data type of the input parameters etc) and data
structures, we create a header file (a .h file) and put all the
function prototypes, as well as commonly used constants in that
file. This is the case for MP2. In this lab, we'll use a header
file lab8.h to give you practice in reading .h files containing
prototypes and constants.
Since C (and Unix) is case sensitive, it is best to copy all the
function prototypes from the .h file to your actual .c file to
start implementing the functions. That will ensure that you are
using exactly the same function names (e.g. showMenu, not showmenu
nor ShowMenu), and you have the data type for each parameter as
well as the return value ready. We have already copied the
prototypes from the lab8.h file into the lab8.c file for you.
2. Analysis---Refine, Generalize, Decompose the problem
definition
The input for this program will come from two sources. Input
will come from a keyboard. We will display a menu and request an
integer response. The scanf function will be used to read the
users response. We will also read words from a file. To do this we
will use a new function named fscanf. The fscanf fuction requires
the use of pointers. Since we haven't covered pointers in lectures
yet all code for the fscanf function has been written for you
already.
3. Design---Develop Algorithm
The program can be broken into the following steps :
Complete the code for the function showMenu
that shows the main menu, and returns any input from the user.
The following is our menu:
Main Menu
----------
1. Input words
2. Display words
3. Search for an exact match
4. Calculate the average length
0. Exit
Please enter a choice :
You will need to add a
scanf to read the response the user types from the keyboard.
Save and compile your code.
gcc lab8.c -o lab8
Run your program.
./lab8
At this point you will see a menu but since you haven't
completed the switch statement your code will only respond to
user response 0.
The function inputWords returns the count of the number of words
read, so you will have to modify the return statement in the inputWords function.
Next, modify the main function. In the swtich statment, add a case for 1, i.e. case
1: and call inputWords (note that this function requirest
that you pass the words array as an input argument) and assign the value that inputWords returns to the variable named count in main. Don't
forget to add a break; statement.
The third function is exactMatch. The function exactMatch counts how many times a certain target word appears. The user will provide the target word. The input parameters are count and words. Do not declare these again in your code.
Declare a local integer num , which will will hold the number of matches. Initialize it to 0.
Declare a variable named target of data type string.
Prompt the user to enter a target string, with which he/she wants to search for an exact match.
Use a single scanf statement to read the string into target.
Write a for loop to search for exact match between the entered words and a target string. To compare two strings, use strcmp function declared in string.h. The strcmp function returns 0 if two strings are the same, and non-zero value otherwise. If there is an exact match, increment a local counter num. If you use an index variable in your for loop say i, (for refrencing words[i] then make sure to declare this variable.
After the for loop, print out the number num of exact matches using a printf statement. You can remove the comments and complete the call to the printf function.
Update main to call exactMatch the switch case 3:
statement.
Save, compile and run your program and test if this option works
correctly.When you run your code make sure to select option 1
before you select other options since you need to first read in
the array of strings from the file.
The last function is calculateMeanLength. This function calculates the average length of all the words in the file shelterfromthestorm.txt .
Declare a local double variable total , which will calculate the sum of lengths. Initialize it to 0.0 . Note that it should be declared as double (not integer.)
Write a for loop to calculate the sum of of word lengths.
For each interation, add the length of each string. To get
the length of a string, use strlen
function declared in string.h. The strlen
function returns the length of the string.
For example,
for(i=0;i<count;++i) total += strlen(words[i]);
After the for loop, use the printf function to print the average. (Uncomment the printf statement and complete it).To calculate the average length, which equals the total divided by the variable count, which stores the number of entered strings.
Update main to call calculateMeanLength in the switch case 4:
statement.
4. Implementation --- Write the "Program" (Code)
Use the pseudo-code above and open gedit to type your code in
the appropriate function in the file named lab8.c that you
copied from the directory
/class/cs101ta/public_html/labs/lab8 .
5. Run the code
Save, compile and run your program and test if this option
works correctly.
Now it's time to fill in the blanks on your answer sheet.
Answer questions #6, #7, #8 on the
answer sheet.
To store the words ( strings), we have declared (in main) a 2-dimensional array of characters named words. The code in lab8.h shows that the user must not enter more than 1000 words, and each word is at most 64 letters. For your convenience, we defined MAX_COUNT and MAX_LENGTH in lab8.h. (Why is MAX_LENGTH defined as (64+1) instead of 64?)
We are using a variation of the scanf function called fscanf (file scanf) to read data from a file named "shelterfromthestorm.txt". We will NOT have to use Unix redirection (<) .