Professional Documents
Culture Documents
Sas15 Bes043
Sas15 Bes043
Productivity Tip: Try playing some music while doing the exercises on this module.
A. LESSON PREVIEW/REVIEW
1) Introduction (2 mins)
Topic: Random Numbers and Math Library functions
Lesson connection and relevance to the students: One thing that makes programming easy is by
using a ready-made sub-program (C++ call it functions, methods, or objects) already available as part of
a shared library (another term that refers to the header file, such as the iostream.h, iomanip.h, & string.h).
A Header file may contain special functions that will do some special task or processing for your program.
Most of the library is already a part of the package of a programming compiler. Some are shared by other
programmers, which can be downloaded from the internet for free!
If you are into a computer-related courses, another programming subject titled Object-Oriented
offers you to learn to create your own header file with functions necessary for your program that might
not be available in the internet.
Since there are ready-made special sub-programs, all we have to do is to try, understand, and
observe how it works. In programming, there’s no harm in trying, except if you’re creating a computer
virus.
Learning targets: Random number generation, pow(), sqrt()
2) Activity 1: What I Know Chart, part 1 (3 mins)
Write in the first column what you know about the given questions in the second column below.
The third column is left blank at this time.
3. Math computations in
computers are limited, true or
false?
B. MAIN LESSON
1) Activity 2: Content Notes (13 mins)
Random Numbers
Some applications require behavior that appears random. Random numbers are useful
particularly in games and simulations. For example, many board games use a die (one of a pair
of dice) to determine how many places a player is to advance. A die or pair of dice are used in
other games of chance. A die is a cube containing spots on each of its six faces. The number of
spots range from one to six. A player rolls a die or sometimes a pair of dice, and the side(s) that face up have
meaning in the game being played. The value of a face after a roll is determined at random by the complex
tumbling of the die. A software adaptation of a game that involves dice would need a way to simulate the random
roll of a die.
All algorithmic random number generators actually produce pseudorandom numbers, not true random
numbers. A pseudorandom number generator has a particular period, based on the nature of the algorithm used.
If the generator is used long enough, the pattern of numbers produced repeats itself exactly. A sequence of true
random numbers would not contain such a repeating subsequence. The good news is that all practical
algorithmic pseudorandom number generators have periods that are large enough for most applications.
C++ programmers can use two standard C functions for generating pseudorandom numbers: srand and rand:
void srand(unsigned)
int rand()
srand establishes the first value in the sequence of pseudorandom
integer values. Each call to rand returns the next value in the sequence of
pseudorandom values. Listing 8.10 (simplerandom.cpp) shows how a
sequence of 100 pseudorandom numbers can be printed.
The numbers printed by the program appear to be random. The
algorithm is given a seed value to begin, and a formula is used to produce
the next value. The seed value determines the sequence of numbers
generated; identical seed values generate identical sequences. If you run
the program again, the same sequence is displayed because the same seed
value, 23, is used. In order to allow each program run to display different
sequences, the seed value must be different for each run. How can we
establish a different seed value for each run? The best way to make up a “random” seed at run time is to use
the time function which is found in the ctime library. The call time(0) returns the number of seconds since midnight
January 1, 1970.
This value obviously differs between program runs, so each execution will use a different seed value, and
the generated pseudorandom number sequences will be different. Listing 8.11 (betterrandom.cpp) incorporates
the time function to improve its randomness over multiple executions.
Hyperbolic functions
cosh Compute hyperbolic cosine
sinh Compute hyperbolic sine
tanh Compute hyperbolic tangent
acosh Compute arc hyperbolic cosine
asinh Compute arc hyperbolic sine
atanh Compute arc hyperbolic tangent
Power functions
pow Raise to power
sqrt Compute square root
cbrt Compute cubic root
hypot Compute hypotenuse
#include <math.h>
This header file contains the sqrt() function. It is to be included with a .h extension. The C++ compiler
cannot locate the said file during compilation process without the extension. It seems that there’s still
inconsistency with how the compiler deals with header files. For this simple program that involves simple
cout and sqrt() only, all the declarations above the main program can be re-written as
/* sqrt example */
#include <iostream.h> // cout
#include <math.h> // sqrt
void main ()
{
The using namespace std; can still be removed
because there’s no string variables and a string.h is
involved in the program but a .h extension has to be
written with the iostream header file.
result = sqrt (x);
The sqrt() functions the same as the standard math √ (square root). It gives the square root of any number
placed as its parameter (enclosed with a pair of parentheses on the right side of the sgrt name). For this
statement, it gives or returns the square root of any value of the variable x. The sqrt() function takes only
one parameter, it can be an integer, double or any valid C++ math expression, such as x + 2. A negative
x will result to an indefinite value.
pow (3, 2)
Since we cannot write the number with a superscript (32) in the C++ compiler, we may use the ^ character
to represent it as 3^2 for displaying, where the base number 3 is raised to the power 2.
The Listing 5.5 program will display
2) Activity 3: Skill-building Activities (with answer key) (18 mins + 2 mins checking)
Write an algorithm for a program that will let a user guess some magic number. Before the program ask
a number, it will set a number randomly chosen from the range of 1 to 9 and call it the magic number. If
the entered number is lower or higher than the magic number, it will prompt the user if it should be lower
or higher than the entered number. The program will only stop if the magic number has been identified.
2. Area = π(r2)
3.
4.
C. LESSON WRAP-UP
1) Activity 6: Thinking about Learning (5 mins)
Mark the place in the work tracker which is simply a visual to help you track how much work you have
accomplished and how much work there is left to do. This tracker will be part of your activity sheet.
To develop habits on thinking about learning, answer the questions below about your learning
experience.
Three things you learned:
1.
2.
3.
2.) How do we know on how to use all of those Math functions in a program? Answer: We can know how to
use any of those Math functions by referring to a programming book or manual or the easiest way is to
use the internet. You can find a lot of C++ programming tutorial website for free. Example:
www.cplusplus.com