CS 101  Lab Activity 8

Objectives:


References



Part 1: Nested Loops

Answer questions 1-3 on the answer sheet.





Part 2: Hangman Game

1. Requirements Specification (Problem Definition)

In this lab, you're going to design a Hangman game. The game works as follows:



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 

2. Analysis---Refine, Generalize, Decompose the problem definition

  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:


3. Design---Develop Algorithm

The complete flow-chart for the program is shown below.
lab8 first flow
        chart

second flow chart
third flow chart


4. Implementation --- Write the "Program" (Code)


Use the flow chart above and open gedit to type your code in the file named hang.c that you downloaded.




5. Run the code

Save to compile your program. Note that whenever you save your source file, the compiler is run.
gcc hang.c
./a.out

You can check your work by running the demo that you downloaded in step 1, by typing the following at the Unux prompt,

./hangdemo


After you've completed your program, then answer questions #4 and #5 on the Answer sheet.

 



Part 3: String Matching and Statistics

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.



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 :

  1. 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.

  2. 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.

  3. The first function is inputWords, which reads words from a file into an array named words. 
  4. 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.

  5. The second function displayWords has been coded for you. Update main to call displayWords in the switch statement for case 2: .

  6. Save, compile and run your program now. Select menu option 1 and next option 2. If you get an infinite loop (possibly by forgetting the break in the switch statement) type Control-c at the keyboard. You may have to type Control-c MANY times. You should see a list of words, the last eleven being,

  7. Come
    in
    she
    said
    I'll
    give
    you
    shelter
    from
    the
    storm.


     

  8. 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.



  9. 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.


  10. The last function is calculateMeanLength. This function calculates the average length of all the words in the file shelterfromthestorm.txt .


    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.



    Notes:

    The End!