Professional Documents
Culture Documents
th
Function Review
Program Design and Decomposition
Typecasting
If we have some time, well start ASCII
Function Review
The foo function
computes the average
of two numbers and:
VOID
int
int foo(int
foo(int
foo(int n1,
n1,
n1, n2,
int
int int
n2,
n2, avg)
int
int avg)
&avg)
({
int sum;= 0;
sum += num1;
sum += num2;
avg
= sum / 2;
return(sum);
avg
= sum / 2;
return(sum);
}]
int main(void)
{
int mean, sum;
sum = func(5,9,mean);
cout << sum << << mean;
}
Function Review
float credit;
void charge(float amount)
{
if (amount <= credit)
{
cout << Approved.\n;
credit -= amount;
}
else
cout << Rejected!\n;
}
int main(void)
{
credit = 100.00;
charge(52);
charge(10);
charge(40);
}
// starting
// line 1
// line 2
// line 3
cur
s1
s2
s3
(0+0+3)/3
(0+3+6)/3
(3+6+1)/3
return((s1+s2+s3)/3);
61
3
0
3
0
3
6
3
0
16
}
int main(void)
{
float avg;
avg=moving_avg(3);
avg=moving_avg(6);
avg=moving_avg(1);
avg=moving_avg(2);
}
3
avg 3.3
1
cout
cout
cout
cout
<<
<<
<<
<<
avg
avg
avg
avg
<<
<<
<<
<<
\n;
\n;
\n;
\n;
What does it
print?
1
3
3.3
Etc
Reference Review
void Silly(int byValue, int &byReference)
{
byValue = 10;
byReference = 20;
}
void main()
{
int a = 5, b = 6;
Silly(a,b);
cout << a << << b;
When we pass by
value, the program
makes a copy of
the actual
parameter and
sends it to the
function.
Reference Review
void Silly(int byValue, int &byReference)
{
byValue = 10;
byReference = 20;
}
void main()
{
int a = 5, b = 6;
Silly(a,b);
cout << a << << b;
When we pass by
reference, the
program
references the
actual parameter
in the calling
function.
Reference Review
void Silly(int byValue, int &byReference)
{
byValue 10
5
byValue = 10;
byReference = 20;
byReference
}
void main()
{
int a = 5, b = 6;
Silly(a,b);
cout << a << << b;
}
Output:
5 20
a 5
b 20
6
What does it
5print?
5
Programming Challenge
void main(void)
{
int eyes, ears;
cin >> eyes >> ears;
switch (eyes + ears)
{
case 0:
cout << I think ;
case 1:
cout << you are a
goblin;
break;
case 2:
case 3:
cout << You ghoul ;
case 4:
cout << or a human;
break;
default:
cout << not a ghost;
break;
}
}
21 Pickup Requirements?
Will the computer:
be one of the players or
simply run a game between two human players?
And which player goes first?
Our Requirements
The computer will play against a human player
The human player will always go first
21
Pickup
Run
The
Game
Display
Instructions
Get
Users
Move
Get
Computers
Move
How to Decompose?
Decomposition can work well
example: designing a restaurant menu
idea: decompose by menu courses
Choose style
and theme
Assemble
pieces
Merge the
story
Pseudocode
Pseudocode is an outline of a program that can easily
be understood and converted into valid programming
statements.
Pseudocode enables you to concentrate on the
algorithms without worrying about all the syntactic
details of the programming language.
Pseudocode can vary from general to very detailed.
It should always be descriptive enough to explain
your programs approach to another programmer.
Example Pseudocode
Pseudocode: Converting Fahrenheit to Celsius
Input: A Fahrenheit value
Output: A Celsius value
Subtract 32 from the Fahrenheit value
Multiply the result of the previous step by 5
Divide the result of the previous step by 9
Print out the result
21 Pickup Decomposition
#4 Run
The
Game
#1 Display
Instructions
#3 Get
Users
Move
#2 Get
Computers
Move
#1 Display Instructions
The purpose of this component is to
display instructions to the user.
Inputs:
None
Output:
None
Pseudo-code:
1. Print out the instructions for the user
21 Pickup Decomposition
#4 Run
The
Game
#1 Display
Instructions
#3 Get
Users
Move
#2 Get
Computers
Move
21 Pickup Decomposition
#4 Run
The
Game
#1 Display
Instructions
#3 Get
Users
Move
#2 Get
Computers
Move
21 Pickup Decomposition
#4 Run
The
Game
#1 Display
Instructions
#3 Get
Users
Move
#2 Get
Computers
Move
Output:
None
Pseudo-code:
1. Print the game instructions using component #1
2. Create an initial pile of 21 stones
3. Remember that the human starts first
3. While there are stones left
A. Print out the current board status
B. If its the humans turn, ask component #3 for a move
C. Otherwise, ask component #2 for the computers move
D. Remove the specified number of stones from the pile
E. If the game was won, print the outcome and stop
F. Otherwise, its the other players turn
If we
pass in
1
2
3
>3
<= 0
We Expect
this Result
1
2
3
1 <= rand <= 3
<= 0
}
(This
enables you to test the parts of the
void RunTheGame(void)
program
that you have finished.)
{
PrintInstructions();
int numStones = 21, curplayer = 0;
while (numStones != 0)
{
cout << There are << numStones << stones left.\n;
if (curplayer == 0)
numStones -= GetHumanMove(numStones);
else
...
}
main()
{
RunTheGame();
}
Refactoring
So youve completed your version of 21 Pickup and you
have a few spare hours to waste
Old
computer
player:
New computer
player:
You
decide to
improve (refactor)
your computer
Inputs:
player so it playsInputs:
a better game.
The number of remaining rocks
Output:
Pseudo-code:
1. If 3 or fewer stones remain, then
pick up all remaining stones.
2. Otherwise, pick a random number
between 1 and 3
3. Return the chosen value
Pseudo-code:
1. If 3 or fewer stones remain, then
pick up all remaining stones.
2. Otherwise try to take enough
stones to leave the remaining pile
with a multiple of four stones
3. Return the chosen value
Refactoring
Old computer player:
Inputs:
Inputs:
same
Output:
The number of rocks the computer
wants to pick up (1-3)
Pseudo-code:
1. If 3 or fewer stones remain, then
pick up all remaining stones.
2. Otherwise try to take enough
stones to leave the remaining pile
with a multiple of four stones
3. Return the chosen value
Notice that our new component still accepts the same inputs
and returns the same outputs.
We can use our updated component without having to
make any changes to the rest of our program!
This is a good thing! Our smart design has isolated
the impact of small changes to our program.
Type Casting
Question: What happens
if you try to put an 8x10
photo in a 4x6 frame?
Answer: You end up losing
some of the information in
the picture.
Type Casting
Storing a floating point value into an integer
int main(void)
// example #1
ding 1.235
{
dong 1
double ding = 1.235;
Integer variables can only hold whole
int dong;
numbers.
dong = ding;
}
int main(void)
{
double ding;
int dong = 55;
ding = dong;
}
// Any
Theyre
lessproblem
precise here?
than doubles or1.235
floats
(which can hold fractional numbers too).
// example #2
dong 55
This can sometimes result in bugs!
// Any problem here?
Type Casting
When we assign a variable of one type (e.g. int)
to a variable or expression of another type (e.g.
float), this is called type casting.
We have to be careful when doing this because
we may lose precision during the conversion.
There are two ways to deal with type casting in C++:
1. Let the compiler do it for you automatically.
2. You do it yourself with a special C++ command.
i=1;
u=4000000000;
s=2;
f=3.5;
d=4.2;
// cubic inches
// cubic inches
howMany;
// bad!
howMany = bagVolume/peachVolume;
howMany = static_cast<int>(bagVolume/peachVolume);// good!
}
// cubic inches
// cubic inches
howMany;
= f; ?
= s; ?
= s; ?
= i; ?
= 3.0;?
?
=3.0*s;
= i; ?
= u; ?
i=1;
u=4000000000;
s=2;
f=3.5;
d=4.2;
shhhh;
shhhh = 5/2;
cout << shhhh << endl;
shhhh = 5/2.0;
cout << shhhh << endl;
shh = 1.0*(5/2);
cout << shhhh << endl;
shhhh;
a=5, b=2;
shhhh = a/b;
cout << shhhh << endl;
shhhh = static_cast<float>(a/b);
cout << shhhh << endl;
shhhh = static_cast<float>(a)/b;
cout << shhhh << endl; // what will print?
}
1. First the integer division happens (5/2 -> 2), and then
the result is implicitly converted into a double. Result: 2.0
2. Again, first the integer division happens (5/2 -> 2), and then
the result is explicitly coverted into a double. Result: 2.0
3. In this case, we convert as integer value 5 into 5.0, and then
divide the floating point 5.0 by 2. Result: 2.5
Traditional C Typecasts
You may also see this alternative
typecasting notation in C or C++ programs:
variable1 = (newtype) variable2;
variable1 = (newtype) (expression2);
int main(void)
{
float
double
int
short
f = (float)us;
i = (int)(f/2);
s = (int)f /(int)d;
}
f
d
i
s
=
=
=
=
2.718;
3.14159;
-2;
32767;
// f = static_cast<float>(us);
// i = static_cast<int>(f/2);
// s = static_cast<int>(f) /
static_cast<int>(d);