There are only 10 different kinds of people in the world: those who can count in binary and those who can't. --Anon
Iteration: About.com C++ Tutorial - Lesson 7: While statement
Selection: About.com C++ Tutorial - Lesson 5: If statement
Algorithm isPrime . "To understand a program, you must become both the machine and the program." ==> Hand-trace the algorithm/program.
Read about 122 pseudocode
Implement the algorithm as a C++ program: isPrime-v1.cpp
//prompt and read
. . .
while(eof){
//. . .
}
Exercise: modify pizza.cpp to use an EOF-controlled while loop
Exercise: modify glassvol.cpp to use an interactive UI
Exercise: modify sPrime-v1.cppto use an interactive UI: isPrime-eof.cpp
Bottom-test do loop: Syntax
do{
statement(s);
}while(condition);
The do loop is a post-tested or bottom-tested loop (or a leap-before-you-look loop ;-). The statements in the body of the do loop will execute 1 or more times, depending on the value of the condition.
The do loop is a convenient control structure to use when a problem requires code to be executed at least once.
Problem: Count the number of times we must roll a die until we get a particular number (e.g., six). We're not going to hit our target without at least one roll, so this is a good problem for a do-while loop:
//prompt and read n
. . .
//roll the die until we get n
do{
die = roll the die;
++rolls;
}while(die != n);
Refining the code:
The above code segment has an instruction, roll the die
, which is not a C++ statement. To simulate random events like tossing dice,
the C++ standard runtime library includes a rand() function that returns a
pseudorandom integer in the range [0 .. RAND_MAX].
pseu·do·ran·dom (su'do-ran'dom). adj.Of, relating to, or being random numbers generated by a definite, nonrandom computational process.
Since rand() returns a potentially large number, we can reduce it by using
the remainder operator (%). This statement gives us a value in the range 0
.. 5:
die = rand() % 6;
If we just add 1 to this value (adding parens for readability), we've got our die roll, 1 .. 6:
die = (rand() % 6) + 1;
To use rand(), we include the stdlib header:
Implementation: rollDie.cpp
rand() always generates the same sequence of pseudorandom numbers. So, every time the rollDie.cpp executes it gets the same sequence of numbers from its calls to rand(). C++ offers another function, srand(), that addresses this limitation.
int abs(int aNum);
//
return absolute value of integer parameter
int rand();
// returns a value between (inclusive) 0 and and RAND_MAX (defined
by the compiler, often 32767):
#include#include using namespace std; int main(){ cout << "RAND_MAX = " << RAND_MAX //32767 << endl << "Exiting.." << endl; return 0; }
double ceil(double x);
//returns the smallest integer that is greater or equal to
x
double floor(double x);
//returns the greatest integer that is less than or equal to
x
double fabs(double number);
//returns absolute value of floating-point
double log2(double x);
//returns the base 2 logarithm for x > 0.0
double log(double x);
//returns the natural logarithm for x > 0.0
double log10(double x);
//returns the base 10 logarithm for x > 0.0
double pow(double b, double p);
//returns b raised to the p power.
Each standard function is like a mini-program: we
call the function, passing any necessary data to it, the function computes
a value and returns it to the point of call.
Here's how we called rand() to simulate the roll of a die:
die = rand() % 6 + 1;