Você está na página 1de 4

CS161 Homework #5 Functions with Arguments

Refer to the background information for detailed instructions

Writing a Program
For Homework #5, we will be starting a new program. Part of this assignment will be to reinforce the
process of writing algorithms and learning how best to apply loops and functions. We will move away
from using global variables in this assignment and learn how to create locals and pass (and return) the
information we want functions to have access. For your loops you will want to continue to use WHILE
loops.

In this homework, you will be creating a program to play a word guessing game. It is a 2 player game.
Here is how it will work:
1. The first player types in a word. Your program should clear the screen immediately after
2. The second player will try to guess the word with a maximum of 4 chances
3. In the prompts each time through, your program will provide a variety of hints starting with:
a. First prompt provides the number of characters of the word
b. The second prompt provides the first character
c. The third prompt provides both the first and last characters
d. CHALLENGE (Extra Credit!) - The fourth prompt outputs the first character and each vowel
followed by the last character
4. The word typed in doesnt have to be the same case as what the first player typed in to match.
This means funny will match with FUNNY or Funny
5. If they get it right, give the player 4 points on the first try, 3 points for the second try, 2 points for
the 3rd try and 1 point for the last try. Then give the other player a turn
6. Continue this until a player reaches 10 points or they want to quit.
The next page outlines the steps to take to be successful with writing this program!

Follow these steps. Start by planning! Have a technical assistant check off the first three steps before
starting to program!
_____1. Create the algorithm
_____a. List out the major steps
_____b. How do we clear the screen? Output many newlines!
_____c. Decide how you will handle the points. What variables do you need and what
should their initial values be
_____d. Decide how to handle the different prompt hints. Will you be using an if
statement? What variable will you need to decide how many tries the user has gone
through so far
_____e. Decide how to handle alternating between two players
_____f. Where will you need loops in this program?
_____a. To clear the screen
_____b. To have another turn
_____c. The four tries maximum
_____2. Decide what Modules you need
For each module plan what information the module needs to work with as arguments
and what returned value is needed
Function Headers
_____a. Welcome the user
MODULE welcome()
_____b. Get the word to be guessed MODULE String get_word()
_____c. Clear the screen
_____d. Now it is your turn:

_____3.

_____4.

Create Variables Needed in the main program


Think about what variables you will need and plan the data types (NUM, INT, STRING)
_____a. Number of tries
INT tries
_____b. Player 1 points, Player 2 points ______________
________________
_____c. Now it is your turn:

Draw the flow chart

_____5. AFTER Steps 1-4 are checked off by a technical assistant, start to program! Incrementally
create the program, one module at a time. Translate and compile and then continue with the
next. Never implement more than a single module without first testing what you have created!
_____6.

Login, and get set up with quizor1.cs.pdx.edu


_____a. Connect to PSUs network using your ODIN login and password
_____b. Use Host name of quizor1.cs.pdx.edu and login with your CS login and password
_____c. MAC Only: In Applications, navigate to Utilities and select Terminal, type:
ssh <username>@quizor1.cs.pdx.edu

_____7.

Navigate to the directory for the homework, by changing into that directory:
cd CS161/HW5 <enter>

_____8.

Now start modifying the program to add repetition using pico, nano, vi, vim or emacs:
pico prog5.gp <enter>
or
nano prog5.gp <enter>

_____9. Start by having your main program


_____a. Create the START/STOP block
_____b. Define and initialize the variable, as designed
_____10. Implement the first module
_____a. Start with the module header and end with a RETURN
MODULE return_type NAME (data_type argument_name)
//body of the function
RETURN
_____b. Remember there can be only one RETURN in a module SLiK. This is to assist us with
structured programming techniques
_____c. If you have specified a returned_type, then the RETURN must be followed with a
value:
RETURN value
_____d. Once you have implemented the Module, call it from the main program.
_____a. If there are arguments, make sure to pass them in
_____b. If there is a returned type, then make sure to USE that returned value:
For a module such as this:

The call would be:

MODULE INT sample(INT tries)


//body
RETURN value

variable = sample(number_of_tries)

_____11. After implementing each module make sure to translate, compile and run your software:
_____a. Translate and Compile your source code file. The command to do this is:
./slk prog5<enter>
_____b. Run your program by typing:
./prog5.exe
_____c. Make corrections before implementing the next module!
_____12. Submit your program by typing ./submit in the HW5 directory

UPLOAD TO D2L
_____1. Upload to D2L your answers to Steps 1-4, including the flow chart

Você também pode gostar