User Tools

Site Tools


haas:spring2011:cprog:tasks:task3


Corning Community College

C/C++ Programming


Task 3: Selections

~~TOC~~

Objective

To become familiar with selection structures.

Background

In class, we discussed some of the selection structures available to us. They are:

  • if
  • if-else
  • if-else if-else
  • switch-case

The if-style selection structures allow for some conditional value to be met, enabling or disabling the execution of a block of code.

The switch case construct functionally does the same thing, but takes a different aesthetic approach, somewhat representing a switch board or multiplexer.

The if-style statements work based on some conditional value that is checked for, utilizing a number of relational operators:

Relational Operators

Operator Description
== is equal to
!= is not equal to
< is less than
> is greater than
<= is less than or equal to
>= is greater than or equal to

A very common mistake is to use the = sign to denote “is equal to”. This is in error. The singular equal sign denotes set equality, not checking equality. Accidentally placing a single equal where you meant to do double equal will lead to a false positive– the result of the set will ALWAYS be true, creating undesired circumstances in your program logic.

Exploration

What I'd like for you to do is to play with some code to get a better feel for if statements.

I'd like you to write me a program that does the following:

  • prompts the user to enter a random number (this will be used as the seed value), store it in an appropriately sized variable
  • prompts the user to enter a lower bound (positive number), store the user's choice in an appropriately sized variable
  • prompts the user to enter an upper bound (positive number), store the user's choice in an appropriately sized variable
  • using the upper and lower bounds provided by the user, randomly generate a number. This will be the computer's guess
  • giving the user up to 6 chances, prompt them to enter a number (try to guess the value the computer picked).
    • if the user picks a value that is below the computer's guess, display a message telling them their guess is too low (or that the number picked by the computer is higher).
    • if the user picks a value that is above the computer's guess, display a message telling them their guess is too high (or that the number picked by the computer is lower).
    • if the number picked is the correct number picked by the computer, display a message indicating the discovery of the right value.
  • allow the user up to 6 turns (or give them exactly 6 turns) to guess the number. If the user fails to guess the number by the 6th turn, display some sort of “nice try” message and displays the computer's pick before the program ends.

Some additional information you will find useful:

Random Numbers

True Randomness and Computers turns out to be quite a challenging task. Computers cannot “guess”, therefore they select as a result of some algorithm.

In the end, we're actually going settle for “pseudorandom” numbers, which for our purposes will be more than sufficient.

To play with random numbers, we need to do 2 things:

  • seed the random number generator
  • grab a random number out of it

The function srand() is used to seed the random number generator (causing it to generate a sequence of numbers). Be sure to look up its manual page for usage information and requirements.

Once srand() is run, we use the rand() function to obtain a value from the random number generator. We'll make use of our chosen upper and lower bounds to customize the number generated.

Obtaining a random number

You may want to use an equation similar to this:

choice = rand() % upper + lower;

In the above equation, the percent sign % instructs the computer to perform a modulus operation.

Questions

  1. What strategy would you use to most effectively guess the computer's choice?
  2. Are you guaranteed to always find the value in 6 choices?
  3. What happens to the computer's pick if you use the same seed value again and again?
  4. How is that equation for generating a random number of a given range mathematically working?

Review of Compiling/Executing

Just to review the compilation/execution process for working with your source code, if we had a file, hello.c, that we wished to compile to a binary called hello, we'd first want to compile the code, as follows:

lab46:~/src/cprog$ gcc -o hello hello.c
lab46:~/src/cprog$ 

Assuming there are no syntax errors or warnings, and everything compiled correctly, you should just get your prompt back. In the event of problems, the compiler will be sure to tell you about them.

Conceptually, the arrangement is as follows:

gcc -o BINARY_FILE SOURCE_FILE

The BINARY_FILE comes immediately after the -o, and the SOURCE_FILE, must never immediately follow a -o. It can precede, and such is perfectly valid (especially if you feel that way more intuitive).

To execute your binary, we need to specify a path to it, so we use ./, which basically says “in the current directory”:

lab46:~/src/cprog$ ./hello
Hello, World!
lab46:~/src/cprog$ 

Copying files to your submit directory

As you write your code, hopefully you've developed the good habit of storing all your programs in your ~/src/cprog directory (and have added/committed them to your repository).

But, in order to complete your tasks, you've been requested to place it in your ~/src/submit directory instead.

What to do?!

We'll simply make a copy of your code! Assuming we're working with a source file called myprog.c in our ~/src/cprog directory, we'll copy it into ~/src/submit/ and give it a name of: taskX.c

To do that we use the cp command, and run it as follows:

lab46:~/src/cprog$ cp myprog.c ~/src/submit/taskX.c
lab46:~/src/cprog$ 

We can then hop over to our submit directory and add/commit it:

lab46:~/src/cprog$ cd ~/src/submit
lab46:~/src/submit$ ls
contact.info    taskU.c    taskV.c    taskW.c    taskX.c
lab46:~/src/submit$ svn add taskX.c
Added   taskX.c
lab46:~/src/submit$ svn commit -m "added taskX.c to the submit directory"
...

Submission

All questions in this assignment require an action or response. Please organize your responses into an easily readable format and submit the final results to your instructor per the appropriate methods.

Your assignment is expected to be performed and submitted in a clear and organized fashion- messy or unorganized assignments may have points deducted. Be sure to adhere to the submission policy.

When complete, questions requiring a response can be electronically submit using the following form:

<html><center><a href=“http://lab46.corning-cc.edu/haas/content/cprog/submit.php?task3”>http://lab46.corning-cc.edu/haas/content/cprog/submit.php?task3</a></center></html>

Additionally, the successful results of the following actions will be considered for evaluation:

  • placement of the code you created to solve this task in your ~/src/submit directory
  • name that file: task3.c
  • addition/commit of task3.c into your repository

As always, the class mailing list and class IRC channel are available for assistance, but not answers.

haas/spring2011/cprog/tasks/task3.txt · Last modified: 2011/02/07 01:46 by 127.0.0.1