Corning Community College
C/C++ Programming
Task 3: Selections
~~TOC~~
To become familiar with selection structures.
In class, we discussed some of the selection structures available to us. They are:
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:
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.
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:
Some additional information you will find useful:
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:
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.
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.
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$
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" ...
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:
As always, the class mailing list and class IRC channel are available for assistance, but not answers.