Title: Computer Science 1620
1Computer Science 1620
2- Given a number n, the factorial of n, written n!,
is computed as follows - note 0! 1
- examples
n! n x (n-1) x (n-2) x x 1
3! 3 x 2 x 1 6 5! 5 x 4 x 3 x 2 x
1 120 0! 1
3- Write a program that takes a number n from the
user, and calculates the factorial of that number.
4- Write a program that takes a number from the
user, and calculates the factorial of that
number.
include ltiostreamgt include ltiomanipgt using
namespace std int main() int n cout ltlt
"Please enter a number" cin gtgt n double
factn 1.0 for (int i 1 i lt n i)
factn i cout ltlt fixed ltlt
setprecision(0) cout ltlt n ltlt "! " ltlt factn ltlt
endl return 0
5- Write a program that takes a number from the
user, and calculates the factorial of that
number.
include ltiostreamgt include ltiomanipgt using
namespace std int main() int n cout ltlt
"Please enter a number" cin gtgt n double
factn 1.0 for (int i 1 i lt n i)
factn i cout ltlt fixed ltlt
setprecision(0) cout ltlt n ltlt "! " ltlt factn ltlt
endl return 0
6- Write a program that takes a number from the
user, and calculates the factorial of that
number.
include ltiostreamgt include ltiomanipgt using
namespace std int main() int n cout ltlt
"Please enter a number" cin gtgt n double
factn 1.0 for (int i 1 i lt n i)
factn i cout ltlt fixed ltlt
setprecision(0) cout ltlt n ltlt "! " ltlt factn ltlt
endl return 0
We'll use a double to store the factorial, since
factorials are typically very big numbers.
7- Write a program that takes a number from the
user, and calculates the factorial of that
number.
include ltiostreamgt include ltiomanipgt using
namespace std int main() int n cout ltlt
"Please enter a number" cin gtgt n double
factn 1.0 for (int i 1 i lt n i)
factn i cout ltlt fixed ltlt
setprecision(0) cout ltlt n ltlt "! " ltlt factn ltlt
endl return 0
8- Write a program that takes a number from the
user, and calculates the factorial of that
number.
include ltiostreamgt include ltiomanipgt using
namespace std int main() int n cout ltlt
"Please enter a number" cin gtgt n double
factn 1.0 for (int i 1 i lt n i)
factn i cout ltlt fixed ltlt
setprecision(0) cout ltlt n ltlt "! " ltlt factn ltlt
endl return 0
9- Write a program that takes a number from the
user, and calculates the factorial of that
number.
include ltiostreamgt include ltiomanipgt using
namespace std int main() int n cout ltlt
"Please enter a number" cin gtgt n double
factn 1.0 for (int i 1 i lt n i)
factn i cout ltlt fixed ltlt
setprecision(0) cout ltlt n ltlt "! " ltlt factn ltlt
endl return 0
10- Write a program that takes a number from the
user, and calculates the factorial of that
number.
include ltiostreamgt include ltiomanipgt using
namespace std int main() int n cout ltlt
"Please enter a number" cin gtgt n double
factn 1.0 for (int i 1 i lt n i)
factn i cout ltlt fixed ltlt
setprecision(0) cout ltlt n ltlt "! " ltlt factn ltlt
endl return 0
11(No Transcript)
12- Example Given a set of n objects, the number of
combinations of those objects taken r at a time
can be calculated as - ie. Given a class of 112 students, the number of
possible groups of 5 students is
13- Write a program that takes two integers n and r
from the user, and calculates Cnr for those two
numbers, and displays the results.
14- Write a program that takes two integers n and r
from the user, and calculates Cnr for those two
numbers, and displays the results.
include ltiostreamgt include ltiomanipgt using
namespace std int main() int n, r cout ltlt
"Please enter n and r" cin gtgt n gtgt r
double factn 1.0 for (int i 1 i lt n
i) factn i double factr 1.0
for (int i 1 i lt r i) factr
i double factnr 1.0 for
(int i 1 i lt (n-r) i) factnr
i double result factn / (factr
factnr) cout ltlt fixed ltlt setprecision(0) cout
ltlt n ltlt " choose " ltlt r ltlt " " ltlt result ltlt
endl return 0
15- Write a program that takes two integers n and r
from the user, and calculates Cnr for those two
numbers, and displays the results.
include ltiostreamgt include ltiomanipgt using
namespace std int main() int n, r cout ltlt
"Please enter n and r" cin gtgt n gtgt r
double factn 1.0 for (int i 1 i lt n
i) factn i double factr 1.0
for (int i 1 i lt r i) factr
i double factnr 1.0 for
(int i 1 i lt (n-r) i) factnr
i double result factn / (factr
factnr) cout ltlt fixed ltlt setprecision(0) cout
ltlt n ltlt " choose " ltlt r ltlt " " ltlt result ltlt
endl return 0
16- Write a program that takes two integers n and r
from the user, and calculates Cnr for those two
numbers, and displays the results.
include ltiostreamgt include ltiomanipgt using
namespace std int main() int n, r cout ltlt
"Please enter n and r" cin gtgt n gtgt r
double factn 1.0 for (int i 1 i lt n
i) factn i double factr 1.0
for (int i 1 i lt r i) factr
i double factnr 1.0 for
(int i 1 i lt (n-r) i) factnr
i double result factn / (factr
factnr) cout ltlt fixed ltlt setprecision(0) cout
ltlt n ltlt " choose " ltlt r ltlt " " ltlt result ltlt
endl return 0
17- Write a program that takes two integers n and r
from the user, and calculates Cnr for those two
numbers, and displays the results.
include ltiostreamgt include ltiomanipgt using
namespace std int main() int n, r cout ltlt
"Please enter n and r" cin gtgt n gtgt r
double factn 1.0 for (int i 1 i lt n
i) factn i double factr 1.0
for (int i 1 i lt r i) factr
i double factnr 1.0 for
(int i 1 i lt (n-r) i) factnr
i double result factn / (factr
factnr) cout ltlt fixed ltlt setprecision(0) cout
ltlt n ltlt " choose " ltlt r ltlt " " ltlt result ltlt
endl return 0
Here's where we need to calculate Cnr
18 double factn 1.0 for (int i 1 i lt n
i) factn i double factn 1.0
for (int i 1 i lt n i) factn i
double factn 1.0 for (int i 1 i lt n
i) factn i double result factn /
(factr factnr)
Calculate n!
Calculate r!
Calculate (n-r)!
Combine results into Cnr
19 double factn 1.0 for (int i 1 i lt n
i) factn i double factn 1.0
for (int i 1 i lt n i) factn i
double factn 1.0 for (int i 1 i lt n
i) factn i double result factn /
(factr factnr)
Calculate n!
Calculate r!
Calculate (n-r)!
Combine results into Cnr
20 double factn 1.0 for (int i 1 i lt n
i) factn i double factr 1.0
for (int i 1 i lt r i) factr i
double factn 1.0 for (int i 1 i lt n
i) factn i double result factn /
(factr factnr)
Calculate n!
Calculate r!
Calculate (n-r)!
Combine results into Cnr
21 double factn 1.0 for (int i 1 i lt n
i) factn i double factr 1.0
for (int i 1 i lt r i) factr i
double factnr 1.0 for (int i 1 i lt n-r
i) factnr i double result factn
/ (factr factnr)
Calculate n!
Calculate r!
Calculate (n-r)!
Combine results into Cnr
22 double factn 1.0 for (int i 1 i lt n
i) factn i double factr 1.0
for (int i 1 i lt r i) factr i
double factnr 1.0 for (int i 1 i lt n-r
i) factnr i double result factn
/ (factr factnr)
Calculate n!
Calculate r!
Calculate (n-r)!
Combine results into Cnr
23- Write a program that takes two integers n and r
from the user, and calculates Cnr for those two
numbers.
include ltiostreamgt include ltiomanipgt using
namespace std int main() int n, r cout ltlt
"Please enter n and r" cin gtgt n gtgt r
double factn 1.0 for (int i 1 i lt n
i) factn i double factr
1.0 for (int i 1 i lt r i)
factr i double factnr 1.0 for
(int i 1 i lt n-r i) factnr i
double result factn / (factr
factnr) cout ltlt fixed ltlt setprecision(0) cout
ltlt n ltlt " choose " ltlt r ltlt " " ltlt result ltlt
endl return 0
24- Does this solve our problem?
- Yes
- Any problems with previous code?
- duplication!!!
- the calculation for factorial is the same, but
the code to calculate is repeated 3 times - the only change is the stopping condition value,
and the variable that we set.
25- Write a program that takes two integers n and r
from the user, and calculates Cnr for those two
numbers.
include ltiostreamgt include ltiomanipgt using
namespace std int main() int n, r cout ltlt
"Please enter n and r" cin gtgt n gtgt r
double factn 1.0 for (int i 1 i lt n
i) factn i double factr
1.0 for (int i 1 i lt r i)
factr i double factnr 1.0 for
(int i 1 i lt n-r i) factnr i
double result factn / (factr
factnr) cout ltlt fixed ltlt setprecision(0) cout
ltlt n ltlt " choose " ltlt r ltlt " " ltlt result ltlt
endl return 0
double ltresultgt 1.0 for (int i 1 i lt
ltinputgt i) factn i
26- Imagine a "factorial machine"
- one input, for a number n
- one output, for the computed number n!
factorial
5
120
27- Imagine a "factorial machine"
- one input, for a number n
- one output, for the computed number n!
factorial
0
1
28- Imagine a "factorial machine"
- one input, for a number n
- one output, for the computed number n!
double result 1.0 for (int i 1 i lt x i)
result i
x
5
result
120
- 3 questions
- where does this code go?
- how do we set the input (x)?
- how do we get the output (result)?
29- Function
- a subprogram
- a piece of code that can be re-used
- called from other places in the program
- example code to write out my name and location
cout ltlt Robert" ltlt endl cout ltlt "Lethbridge" ltlt
endl
30- Function
- written outside of main
- every function has a
- return type
- name
- parameter list
- you call a function by typing its name, followed
by args
include ltiostreamgt using namespace std void
whoAmI() cout ltlt Robert" ltlt endl cout ltlt
"Lethbridge" ltlt endl int main()
whoAmI() whoAmI() return 0
31Output
include ltiostreamgt using namespace std void
whoAmI() cout ltlt Robert" ltlt endl cout ltlt
"Lethbridge" ltlt endl int main()
whoAmI() whoAmI() return 0
Robert Lethbridge Robert Lethbridge
Start Here
32Output
include ltiostreamgt using namespace std void
whoAmI() cout ltlt Robert" ltlt endl cout ltlt
"Lethbridge" ltlt endl int main()
whoAmI() whoAmI() return 0
Robert Lethbridge Robert Lethbridge
At this point, we move to the first line in the
function whoAmI
33Output
include ltiostreamgt using namespace std void
whoAmI() cout ltlt "Robert" ltlt endl cout ltlt
"Lethbridge" ltlt endl int main()
whoAmI() whoAmI() return 0
Robert Lethbridge Robert Lethbridge
Perform output
34Output
include ltiostreamgt using namespace std void
whoAmI() cout ltlt "Robert" ltlt endl cout ltlt
"Lethbridge" ltlt endl int main()
whoAmI() whoAmI() return 0
Robert Lethbridge Robert Lethbridge
Perform output
35Output
include ltiostreamgt using namespace std void
whoAmI() cout ltlt "Robert" ltlt endl cout ltlt
"Lethbridge" ltlt endl int main()
whoAmI() whoAmI() return 0
Robert Lethbridge Robert Lethbridge
Function ended, start again right after function
call
36Output
include ltiostreamgt using namespace std void
whoAmI() cout ltlt "Robert" ltlt endl cout ltlt
"Lethbridge" ltlt endl int main()
whoAmI() whoAmI() return 0
Robert Lethbridge Robert Lethbridge
At this point, we move to the first line in the
function whoAmI
37Output
include ltiostreamgt using namespace std void
whoAmI() cout ltlt "Robert" ltlt endl cout ltlt
"Lethbridge" ltlt endl int main()
whoAmI() whoAmI() return 0
Robert Lethbridge Robert Lethbridge
Perform output
38Output
include ltiostreamgt using namespace std void
whoAmI() cout ltlt "Robert" ltlt endl cout ltlt
"Lethbridge" ltlt endl int main()
whoAmI() whoAmI() return 0
Robert Lethbridge Robert Lethbridge
Perform output
39Output
include ltiostreamgt using namespace std void
whoAmI() cout ltlt "Robert" ltlt endl cout ltlt
"Lethbridge" ltlt endl int main()
whoAmI() whoAmI() return 0
Robert Lethbridge Robert Lethbridge
Function ended, start again right after function
call
40Output
include ltiostreamgt using namespace std void
whoAmI() cout ltlt "Robert" ltlt endl cout ltlt
"Lethbridge" ltlt endl int main()
whoAmI() whoAmI() return 0
Robert Lethbridge Robert Lethbridge ...
At this point, program terminates
41- Example Write a program with a function called
factorial that computes the factorial of 5 and
writes the value to output. Call that function
from the main function.
42- Write a program with a function called factorial
that computes the factorial of 5 and writes the
value to output. Call that function from the
main function.
include ltiostreamgt include ltiomanipgt using
namespace std void factorial() double
result 1.0 for (int i 1 i lt 5 i)
result i cout ltlt fixed ltlt
setprecision(0) cout ltlt result int main()
cout ltlt "The factorial of 5 is
" factorial() cout ltlt endl return 0
43- Write a program with a function called factorial
that computes the factorial of 5 and writes the
value to output. Call that function from the
main function.
include ltiostreamgt include ltiomanipgt using
namespace std void factorial() double
result 1.0 for (int i 1 i lt 5 i)
result i cout ltlt fixed ltlt
setprecision(0) cout ltlt result int main()
cout ltlt "The factorial of 5 is
" factorial() cout ltlt endl return 0
44- Write a program with a function called factorial
that computes the factorial of 5 and writes the
value to output. Call that function from the
main function.
include ltiostreamgt include ltiomanipgt using
namespace std void factorial() double
result 1.0 for (int i 1 i lt 5 i)
result i cout ltlt fixed ltlt
setprecision(0) cout ltlt result int main()
cout ltlt "The factorial of 5 is
" factorial() cout ltlt endl return 0
45- Write a program with a function called factorial
that computes the factorial of 5 and writes the
value to output. Call that function from the
main function.
include ltiostreamgt include ltiomanipgt using
namespace std void factorial() double
result 1.0 for (int i 1 i lt 5 i)
result i cout ltlt fixed ltlt
setprecision(0) cout ltlt result int main()
cout ltlt "The factorial of 5 is
" factorial() cout ltlt endl return 0
46- Write a program with a function called factorial
that computes the factorial of 5 and writes the
value to output. Call that function from the
main function.
include ltiostreamgt include ltiomanipgt using
namespace std void factorial() double
result 1.0 for (int i 1 i lt 5 i)
result i cout ltlt fixed ltlt
setprecision(0) cout ltlt result int main()
cout ltlt "The factorial of 5 is
" factorial() cout ltlt endl return 0
47- Write a program with a function called factorial
that computes the factorial of 5 and writes the
value to output. Call that function from the
main function.
include ltiostreamgt include ltiomanipgt using
namespace std void factorial() double
result 1.0 for (int i 1 i lt 5 i)
result i cout ltlt fixed ltlt
setprecision(0) cout ltlt result int main()
cout ltlt "The factorial of 5 is
" factorial() cout ltlt endl return 0
48- Imagine a "factorial machine"
- one input, for a number n
- one output, for the computed number n!
double result 1.0 for (int i 1 i lt x i)
result i
x
5
result
120
- 3 questions
- where does this code go?
- how do we set the input (x)?
- how do we get the output (result)?
49- Function Input
- sending data to the function
- accomplished through a list of parameters
- a list of variables
- contained in parameter list
- we set these variables in the call using
arguments - a list of expressions
50- Function Inputs
- declared as parameters
- function can have 0 or more parameters
- specify the type and the variable name
- Function arguments
- the value of each argument is assigned to
corresponding parameter variable
void square(int x) cout ltlt xx int main()
cout "2 x 2 " square(2) cout "(21)
x (21) " square(21) return 0
51Output
include ltiostreamgt using namespace std void
square(int x) cout ltlt xx int main()
cout "2 x 2 " square(2) cout "\n(21)
(21) " square(21) return 0
2 x 2 4 (21) x (21) 9
Start Here
52Output
include ltiostreamgt using namespace std void
square(int x) cout ltlt xx int main()
cout "2 x 2 " square(2) cout "\n(21)
(21) " square(21) return 0
2 x 2 4 (21) x (21) 9
Perform Output
53Output
include ltiostreamgt using namespace std void
square(int x) cout ltlt xx int main()
cout "2 x 2 " square(2) cout "\n(21)
(21) " square(21) return 0
2 x 2 4 (21) x (21) 9
At this point, we move to the first line in the
function square The parameter x gets the value of
the expression in the call, which is 2
54Output
include ltiostreamgt using namespace std void
square(int x) cout ltlt xx int main()
cout "2 x 2 " square(2) cout "\n(21)
(21) " square(21) return 0
2 x 2 4 (21) x (21) 9
Perform Output. Note that x has value 2.
55Output
include ltiostreamgt using namespace std void
square(int x) cout ltlt xx int main()
cout "2 x 2 " square(2) cout "\n(21)
(21) " square(21) return 0
2 x 2 4 (21) x (21) 9
Function ended, start right after function call.
56Output
include ltiostreamgt using namespace std void
square(int x) cout ltlt xx int main()
cout "2 x 2 " square(2) cout "\n(21)
(21) " square(21) return 0
2 x 2 4 (21) x (21) 9
Perform Output
57Output
include ltiostreamgt using namespace std void
square(int x) cout ltlt xx int main()
cout "2 x 2 " square(2) cout "\n(21)
(21) " square(21) return 0
2 x 2 4 (21) x (21) 9
At this point, we move to the first line in the
function square The parameter x gets the value of
the expression in the call, which is 21 3
58Output
include ltiostreamgt using namespace std void
square(int x) cout ltlt xx int main()
cout "2 x 2 " square(2) cout "\n(21)
(21) " square(21) return 0
2 x 2 4 (21) x (21) 9
Perform Output. Note that x has value 3.
59Output
include ltiostreamgt using namespace std void
square(int x) cout ltlt xx int main()
cout "2 x 2 " square(2) cout "\n(21)
(21) " square(21) return 0
2 x 2 4 (21) x (21) 9
Function ended, start right after function call.
60Output
include ltiostreamgt using namespace std void
square(int x) cout ltlt xx int main()
cout "2 x 2 " square(2) cout "\n(21)
(21) " square(21) return 0
2 x 2 4 (21) x (21) 9
Program terminates.
61- Example Write a program with a function called
factorial that computes the factorial of x (x is
an inputted number) and writes the value to
output. Call that function from the main
function with the value 5.
62- Write a program with a function called factorial
that computes the factorial of x and writes the
value to output. Call that function from the
main function with the value 5.
include ltiostreamgt include ltiomanipgt using
namespace std void factorial(int x) double
result 1.0 for (int i 1 i lt x i)
result i cout ltlt fixed ltlt
setprecision(0) cout ltlt result int main()
cout ltlt "The factorial of 5 is
" factorial(5) cout ltlt endl return 0
63- Write a program with a function called factorial
that computes the factorial of x and writes the
value to output. Call that function from the
main function with the value 5.
include ltiostreamgt include ltiomanipgt using
namespace std void factorial(int x) double
result 1.0 for (int i 1 i lt x i)
result i cout ltlt fixed ltlt
setprecision(0) cout ltlt result int main()
cout ltlt "The factorial of 5 is
" factorial(5) cout ltlt endl return 0
64- Write a program with a function called factorial
that computes the factorial of x and writes the
value to output. Call that function from the
main function with the value 5.
include ltiostreamgt include ltiomanipgt using
namespace std void factorial(int x) double
result 1.0 for (int i 1 i lt x i)
result i cout ltlt fixed ltlt
setprecision(0) cout ltlt result int main()
cout ltlt "The factorial of 5 is
" factorial(5) cout ltlt endl return 0
65- Write a program with a function called factorial
that computes the factorial of x and writes the
value to output. Call that function from the
main function with the value 5.
include ltiostreamgt include ltiomanipgt using
namespace std void factorial(int x) double
result 1.0 for (int i 1 i lt x i)
result i cout ltlt fixed ltlt
setprecision(0) cout ltlt result int main()
cout ltlt "The factorial of 5 is
" factorial(5) cout ltlt endl return 0
66- Write a program with a function called factorial
that computes the factorial of x and writes the
value to output. Call that function from the
main function with the value 5.
include ltiostreamgt include ltiomanipgt using
namespace std void factorial(int x) double
result 1.0 for (int i 1 i lt x i)
result i cout ltlt fixed ltlt
setprecision(0) cout ltlt result int main()
cout ltlt "The factorial of 5 is
" factorial(5) cout ltlt endl return 0
67- Write a program with a function called factorial
that computes the factorial of x and writes the
value to output. Call that function from the
main function with the value 5.
include ltiostreamgt include ltiomanipgt using
namespace std void factorial(int x) double
result 1.0 for (int i 1 i lt x i)
result i cout ltlt fixed ltlt
setprecision(0) cout ltlt result int main()
cout ltlt "The factorial of 5 is
" factorial(5) cout ltlt endl return 0
68- Write a program with a function called factorial
that computes the factorial of x and writes the
value to output. Call that function from the
main function with the value 5.
include ltiostreamgt include ltiomanipgt using
namespace std void factorial(int x) double
result 1.0 for (int i 1 i lt x i)
result i cout ltlt fixed ltlt
setprecision(0) cout ltlt result int main()
cout ltlt "The factorial of 5 is
" factorial(5) cout ltlt endl return 0
69- Imagine a "factorial machine"
- one input, for a number n
- one output, for the computed number n!
double result 1.0 for (int i 1 i lt x i)
result i
x
5
result
120
- 3 questions
- where does this code go?
- how do we set the input (x)?
- how do we get the output (result)?
70- Function Output
- receiving data from a function
- accomplished through a return statement
- recall that an expression in C has a value
- 2 // the value of this expression is 2
- 2 3 // the value of this expression is 5
- 2 gt 3 // the value of this expression is false
(0) - i 2 // the value of this expression is 2
- a function call is an expression with a value
- square(2)
- square(21)
what is the value of these expressions?
71- Function Output
- given by the return statement
- Function call
- the value of the function call is the value that
is returned - the type of the value returned by a function call
is given by its return type
int square(int x) int result x x
return result int main() cout "2 x 2
" cout ltlt square(2) cout "(21) x (21)
" cout ltlt square(21) return 0
72Output
include ltiostreamgt using namespace std int
square(int x) int result x x return
result int main() cout "2 x 2 "
cout ltlt square(2) cout "(21) x (21) "
cout ltlt square(21) return 0
2 x 2 4 (21) x (21) 9
Start here
73Output
include ltiostreamgt using namespace std int
square(int x) int result x x return
result int main() cout "2 x 2 "
cout ltlt square(2) cout "(21) x (21) "
cout ltlt square(21) return 0
2 x 2 4 (21) x (21) 9
Perform Output
74Output
include ltiostreamgt using namespace std int
square(int x) int result x x return
result int main() cout "2 x 2 "
cout ltlt square(2) cout "(21) x (21) "
cout ltlt square(21) return 0
2 x 2 4 (21) x (21) 9
At this point, we move to the first line in the
function square The parameter x gets the value of
the expression in the call, which is 2
75Output
include ltiostreamgt using namespace std int
square(int x) int result x x return
result int main() cout "2 x 2 "
cout ltlt square(2) cout "(21) x (21) "
cout ltlt square(21) return 0
2 x 2 4 (21) x (21) 9
Do calculation. Note that x has value 2.
76Output
include ltiostreamgt using namespace std int
square(int x) int result x x return
result int main() cout "2 x 2 "
cout ltlt square(2) cout "(21) x (21) "
cout ltlt square(21) return 0
2 x 2 4 (21) x (21) 9
Returns value stored in result
The value of the expression square(2) is the
return value (4).
77Output
include ltiostreamgt using namespace std int
square(int x) int result x x return
result int main() cout "2 x 2 "
cout ltlt square(2) cout "(21) x (21) "
cout ltlt square(21) return 0
2 x 2 4 (21) x (21) 9
Perform output.
78Output
include ltiostreamgt using namespace std int
square(int x) int result x x return
result int main() cout "2 x 2 "
cout ltlt square(2) cout "(21) x (21) "
cout ltlt square(21) return 0
2 x 2 4 (21) x (21) 9
Perform output.
79Output
include ltiostreamgt using namespace std int
square(int x) int result x x return
result int main() cout "2 x 2 "
cout ltlt square(2) cout "(21) x (21) "
cout ltlt square(21) return 0
2 x 2 4 (21) x (21) 9
At this point, we move to the first line in the
function square The parameter x gets the value of
the expression in the call, which is (21) 3
80Output
include ltiostreamgt using namespace std int
square(int x) int result x x return
result int main() cout "2 x 2 "
cout ltlt square(2) cout "(21) x (21) "
cout ltlt square(21) return 0
2 x 2 4 (21) x (21) 9
Do calculation. Note that x has value 3.
81Output
include ltiostreamgt using namespace std int
square(int x) int result x x return
result int main() cout "2 x 2 "
cout ltlt square(2) cout "(21) x (21) "
cout ltlt square(21) return 0
2 x 2 4 (21) x (21) 9
Returns value stored in result
The value of the expression square(3) is the
return value (9).
82Output
include ltiostreamgt using namespace std int
square(int x) int result x x return
result int main() cout "2 x 2 "
cout ltlt square(2) cout "(21) x (21) "
cout ltlt square(21) return 0
2 x 2 4 (21) x (21) 9
Perform Output
83Output
include ltiostreamgt using namespace std int
square(int x) int result x x return
result int main() cout "2 x 2 "
cout ltlt square(2) cout "(21) x (21) "
cout ltlt square(21) return 0
2 x 2 4 (21) x (21) 9
Terminate Program!
84- Example Write a program with a function called
factorial that computes the factorial of x (x is
an inputted number) and returns that value. Call
that function from the main function with the
value 5.
85- Write a program with a function called factorial
that computes the factorial of x and returns that
value. Call that function from the main function
with the value 5.
include ltiostreamgt include ltiomanipgt using
namespace std double factorial(int x)
double result 1.0 for (int i 1 i lt x
i) result i return
result int main() cout ltlt fixed ltlt
setprecision(0) cout ltlt "The factorial of 5 is
" cout ltlt factorial(5) cout ltlt endl return
0
86- Write a program with a function called factorial
that computes the factorial of x and returns that
value. Call that function from the main function
with the value 5.
include ltiostreamgt include ltiomanipgt using
namespace std double factorial(int x)
double result 1.0 for (int i 1 i lt x
i) result i return
result int main() cout ltlt fixed ltlt
setprecision(0) cout ltlt "The factorial of 5 is
" cout ltlt factorial(5) cout ltlt endl return
0
87- Write a program with a function called factorial
that computes the factorial of x and returns that
value. Call that function from the main function
with the value 5.
include ltiostreamgt include ltiomanipgt using
namespace std double factorial(int x)
double result 1.0 for (int i 1 i lt x
i) result i return
result int main() cout ltlt fixed ltlt
setprecision(0) cout ltlt "The factorial of 5 is
" cout ltlt factorial(5) cout ltlt endl return
0
88- Write a program with a function called factorial
that computes the factorial of x and returns that
value. Call that function from the main function
with the value 5.
include ltiostreamgt include ltiomanipgt using
namespace std double factorial(int x)
double result 1.0 for (int i 1 i lt x
i) result i return
result int main() cout ltlt fixed ltlt
setprecision(0) cout ltlt "The factorial of 5 is
" cout ltlt factorial(5) cout ltlt endl return
0
89- Write a program with a function called factorial
that computes the factorial of x and returns that
value. Call that function from the main function
with the value 5.
include ltiostreamgt include ltiomanipgt using
namespace std double factorial(int x)
double result 1.0 for (int i 1 i lt x
i) result i return
result int main() cout ltlt fixed ltlt
setprecision(0) cout ltlt "The factorial of 5 is
" cout ltlt factorial(5) cout ltlt endl return
0
90- Write a program with a function called factorial
that computes the factorial of x and returns that
value. Call that function from the main function
with the value 5.
include ltiostreamgt include ltiomanipgt using
namespace std double factorial(int x)
double result 1.0 for (int i 1 i lt x
i) result i return
result int main() cout ltlt fixed ltlt
setprecision(0) cout ltlt "The factorial of 5 is
" cout ltlt factorial(5) cout ltlt endl return
0
91- Write a program with a function called factorial
that computes the factorial of x and returns that
value. Call that function from the main function
with the value 5.
include ltiostreamgt include ltiomanipgt using
namespace std double factorial(int x)
double result 1.0 for (int i 1 i lt x
i) result i return
result int main() cout ltlt fixed ltlt
setprecision(0) cout ltlt "The factorial of 5 is
" cout ltlt factorial(5) cout ltlt endl return
0
92- Write a program with a function called factorial
that computes the factorial of x and returns that
value. Call that function from the main function
with the value 5.
include ltiostreamgt include ltiomanipgt using
namespace std double factorial(int x)
double result 1.0 for (int i 1 i lt x
i) result i return
result int main() cout ltlt fixed ltlt
setprecision(0) cout ltlt "The factorial of 5 is
" cout ltlt factorial(5) cout ltlt endl return
0
93- Imagine a "factorial machine"
- one input, for a number n
- one output, for the computed number n!
double result 1.0 for (int i 1 i lt x i)
result i cout ltlt result
x
5
result
120
- 3 questions
- where does this code go?
- how do we set the input (x)?
- how do we get the output (result)?
94- Write a program that takes two integers n and r
from the user, and calculates Cnr for those two
numbers, and displays the results.
95- Write a program that takes two integers n and r
from the user, and calculates Cnr for those two
numbers.
include ltiostreamgt include ltiomanipgt using
namespace std int main() int n, r cout ltlt
"Please enter n and r" cin gtgt n gtgt r
double factn 1.0 for (int i 1 i lt n
i) factn i double factr
1.0 for (int i 1 i lt r i)
factr i double factnr 1.0 for
(int i 1 i lt n-r i) factnr i
double result factn / (factr
factnr) cout ltlt fixed ltlt setprecision(0) cout
ltlt n ltlt " choose " ltlt r ltlt " " ltlt result ltlt
endl return 0
Rewrite this program using the factorial function.
96- Write a program that takes two integers n and r
from the user, and calculates Cnr for those two
numbers.
include ltiostreamgt include ltiomanipgt using
namespace std double factorial(int x)
double result 1.0 for (int i 1 i lt x
i) result i return result int
main() int n, r cout ltlt "Please enter n and
r" cin gtgt n gtgt r double factn 1.0
for (int i 1 i lt n i) factn
i double factr 1.0 for (int i 1 i
lt r i) factr i double
factnr 1.0 for (int i 1 i lt n-r i)
factnr i double result factn
/ (factr factnr) cout ltlt fixed ltlt
setprecision(0) cout ltlt n ltlt " choose " ltlt r ltlt
" " ltlt result ltlt endl return 0
97- Write a program that takes two integers n and r
from the user, and calculates Cnr for those two
numbers.
include ltiostreamgt include ltiomanipgt using
namespace std double factorial(int x)
double result 1.0 for (int i 1 i lt x
i) result i return result int
main() int n, r cout ltlt "Please enter n and
r" cin gtgt n gtgt r double factn 1.0
for (int i 1 i lt n i) factn
i double factr 1.0 for (int i 1 i
lt r i) factr i double
factnr 1.0 for (int i 1 i lt n-r i)
factnr i double result factn
/ (factr factnr) cout ltlt fixed ltlt
setprecision(0) cout ltlt n ltlt " choose " ltlt r ltlt
" " ltlt result ltlt endl return 0
98- Write a program that takes two integers n and r
from the user, and calculates Cnr for those two
numbers.
include ltiostreamgt include ltiomanipgt using
namespace std double factorial(int x)
double result 1.0 for (int i 1 i lt x
i) result i return result int
main() int n, r cout ltlt "Please enter n and
r" cin gtgt n gtgt r double factn
factorial(n) double factr 1.0 for (int i
1 i lt r i) factr i
double factnr 1.0 for (int i 1 i lt
n-r i) factnr i double
result factn / (factr factnr) cout ltlt fixed
ltlt setprecision(0) cout ltlt n ltlt " choose " ltlt r
ltlt " " ltlt result ltlt endl return 0
99- Write a program that takes two integers n and r
from the user, and calculates Cnr for those two
numbers.
include ltiostreamgt include ltiomanipgt using
namespace std double factorial(int x)
double result 1.0 for (int i 1 i lt x
i) result i return result int
main() int n, r cout ltlt "Please enter n and
r" cin gtgt n gtgt r double factn
factorial(n) double factr 1.0 for (int i
1 i lt r i) factr i
double factnr 1.0 for (int i 1 i lt
n-r i) factnr i double
result factn / (factr factnr) cout ltlt fixed
ltlt setprecision(0) cout ltlt n ltlt " choose " ltlt r
ltlt " " ltlt result ltlt endl return 0
100- Write a program that takes two integers n and r
from the user, and calculates Cnr for those two
numbers.
include ltiostreamgt include ltiomanipgt using
namespace std double factorial(int x)
double result 1.0 for (int i 1 i lt x
i) result i return result int
main() int n, r cout ltlt "Please enter n and
r" cin gtgt n gtgt r double factn
factorial(n) double factr factorial(r)
double factnr 1.0 for (int i 1 i lt
n-r i) factnr i double
result factn / (factr factnr) cout ltlt fixed
ltlt setprecision(0) cout ltlt n ltlt " choose " ltlt r
ltlt " " ltlt result ltlt endl return 0
101- Write a program that takes two integers n and r
from the user, and calculates Cnr for those two
numbers.
include ltiostreamgt include ltiomanipgt using
namespace std double factorial(int x)
double result 1.0 for (int i 1 i lt x
i) result i return result int
main() int n, r cout ltlt "Please enter n and
r" cin gtgt n gtgt r double factn
factorial(n) double factr factorial(r)
double factnr 1.0 for (int i 1 i lt
n-r i) factnr i double
result factn / (factr factnr) cout ltlt fixed
ltlt setprecision(0) cout ltlt n ltlt " choose " ltlt r
ltlt " " ltlt result ltlt endl return 0
102- Write a program that takes two integers n and r
from the user, and calculates Cnr for those two
numbers.
include ltiostreamgt include ltiomanipgt using
namespace std double factorial(int x)
double result 1.0 for (int i 1 i lt x
i) result i return result int
main() int n, r cout ltlt "Please enter n and
r" cin gtgt n gtgt r double factn
factorial(n) double factr factorial(r)
double factnr factorial(n r) double
result factn / (factr factnr) cout ltlt fixed
ltlt setprecision(0) cout ltlt n ltlt " choose " ltlt r
ltlt " " ltlt result ltlt endl return 0
103- Write a program that takes two integers n and r
from the user, and calculates Cnr for those two
numbers.
include ltiostreamgt include ltiomanipgt using
namespace std double factorial(int x)
double result 1.0 for (int i 1 i lt x
i) result i return result int
main() int n, r cout ltlt "Please enter n and
r" cin gtgt n gtgt r double result
factorial(n) / (factorial(r) factorial(n
r)) cout ltlt fixed ltlt setprecision(0) cout ltlt
n ltlt " choose " ltlt r ltlt " " ltlt result ltlt
endl return 0
104- Write a program that takes two integers n and r
from the user, and calculates Cnr for those two
numbers.
include ltiostreamgt include ltiomanipgt using
namespace std double factorial(int x)
double result 1.0 for (int i 1 i lt x
i) result i return result int
main() int n, r cout ltlt "Please enter n and
r" cin gtgt n gtgt r cout ltlt fixed ltlt
setprecision(0) cout ltlt n ltlt " choose " ltlt r ltlt
" " ltlt factorial(n) /
(factorial(r) factorial(n r)) ltlt
endl return 0
105- The anatomy of a function
return type
function name
parameter list
double factorial(int x) double result
1.0 for (int i 1 i lt x i) result
i return result
function header (declaration)
function body (definition)
106- Functions with no return value
- sometimes, we do not need a value back from our
function - we indicate this with the void return type
- return keyword in void function simply exits
function
void square(int x) int result x x cout
ltlt result
void inverse(int x) if (x 0) return
double result 1.0 / x cout ltlt result
107- Functions as expressions
- the value of a function call is its return value
- return value can be used in other expressions
int square(int x) int result x x
return result int main() cout ltlt
square(2) // outputs 4 cout ltlt square(2)
square(3) // outputs 13 cout ltlt
square(square(2)) // outputs 16 return 0
108- Functions with multiple inputs
- functions can have as many parameters as you like
- first argument aligns with first parameter,
second with second, etc
int sum(int x, int y) return x y int
main() cout ltlt sum(2, 3) // outputs 5
return 0
109- More than one return statement
- a function may have more than one return
statement - function terminates as soon as return statement
is reached
int max(int x, int y) if (x gt y) return
x return y int main() cout ltlt max(2,
3) ltlt endl // outputs 3 cout ltlt max(3, 2)
ltlt endl // outputs 3 return 0
110- Where have we seen functions?
111- The main function
- a function called by operating system
- can have parameters (more on this later)
- has a return value
- 0 program terminated normally
- nonzero program terminated abnormally
int main() return 0
112- Functions can call other functions
- Example write a program that takes an integer
n, and outputs a table of all values Cnr for all
values 0 lt r lt n
113(No Transcript)
114include ltiostreamgt include ltiomanipgt using
namespace std int main() int n cout ltlt
"Please enter n" cin gtgt n cout ltlt " r
n choose r" ltlt endl cout ltlt "---
----------" ltlt endl for (int r 0 r lt n
r) double factn 1.0 for (int i
1 i lt n i) factn i
double factr 1.0 for (int i 1 i lt r
i) factr i double
factnr 1.0 for (int i 1 i lt n-r i)
factnr i double result
factn / (factr factnr) cout ltlt fixed ltlt
setprecision(0) cout ltlt setw(3) ltlt r ltlt
setw(15) ltlt result ltlt endl return 0
115include ltiostreamgt include ltiomanipgt using
namespace std double factorial(int x)
double result 1.0 for (int i 1 i lt x
i) result i return result int
main() int n cout ltlt "Please enter
n" cin gtgt n cout ltlt " r n choose r"
ltlt endl cout ltlt "--- ----------" ltlt
endl for (int r 0 r lt n r) double
factn factorial(n) double factr
factorial(r) double factnr
factorial(n-r) double result factn /
(factr factnr) cout ltlt fixed ltlt
setprecision(0) cout ltlt setw(3) ltlt r ltlt
setw(15) ltlt result ltlt endl return 0
116include ltiostreamgt include ltiomanipgt using
namespace std double factorial(int x)
double result 1.0 for (int i 1 i lt x
i) result i return result int
main() int n cout ltlt "Please enter
n" cin gtgt n cout ltlt " r n choose r"
ltlt endl cout ltlt "--- ----------" ltlt
endl for (int r 0 r lt n r) double
result factorial(n) / (factorial(r)
factorial(n-r)) cout ltlt fixed ltlt
setprecision(0) cout ltlt setw(3) ltlt r ltlt
setw(15) ltlt result ltlt endl return 0
117- With factorial, looks much better
- what is a good candidate for a function in this
code? - calculating n choose r
- write a function called choose that takes two
values n and r, and returns n choose r - inputs n (int), r(int)
- output an integer
118- write a function called choose that takes two
values n and r, and calculates and returns n
choose r - inputs n (int), r(int)
- output an integer
int choose(int n, int r) double result
factorial(n) / (factorial(r) factorial(n-r))
return static_castltintgt(result)
119- write a function called choose that takes two
values n and r, and calculates and returns n
choose r - inputs n (int), r(int)
- output an integer
int choose(int n, int r) double result
factorial(n) / (factorial(r) factorial(n-r))
return static_castltintgt(result)
120- write a function called choose that takes two
values n and r, and calculates and returns n
choose r - inputs n (int), r(int)
- output an integer
int choose(int n, int r) double result
factorial(n) / (factorial(r) factorial(n-r))
return static_castltintgt(result)
121- write a function called choose that takes two
values n and r, and calculates and returns n
choose r - inputs n (int), r(int)
- output an integer
int choose(int n, int r) double result
factorial(n) / (factorial(r) factorial(n-r))
return static_castltintgt(result)
122- write a function called choose that takes two
values n and r, and calculates and returns n
choose r - inputs n (int), r(int)
- output an integer
int choose(int n, int r) double factn
1.0 for (int i 1 i lt n i)
factn i double factr 1.0
for (int i 1 i lt r i) factr
i double factnr 1.0 for (int i
1 i lt n-r i) factnr i
double result factn / (factr factnr)
double result
123- write a function called choose that takes two
values n and r, and calculates and returns n
choose r - inputs n (int), r(int)
- output an integer
- note this assumes that we have the factorial
function defined in our program!!!
int choose(int n, int r) double result
factorial(n) / (factorial(r) factorial(n-r))
return static_castltintgt(result)
124- write a function called choose that takes two
values n and r, and calculates and returns n
choose r - inputs n (int), r(int)
- output an integer
int choose(int n, int r) double result
factorial(n) / (factorial(r) factorial(n-r))
return static_castltintgt(result)
125- write a function called choose that takes two
values n and r, and calculates and returns n
choose r - inputs n (int), r(int)
- output an integer
int choose(int n, int r) double result
factorial(n) / (factorial(r) factorial(n-r))
return static_castltintgt(result)
126- write a function called choose that takes two
values n and r, and calculates and returns n
choose r - inputs n (int), r(int)
- output an integer
int choose(int n, int r) double result
factorial(n) / (factorial(r) factorial(n-r))
return static_castltintgt(result)
127- With function factorial and choose
include ltiostreamgt include ltiomanipgt using
namespace std double factorial(int x)
double result 1.0 for (int i 1 i lt x
i) result i return result int
choose(int n, int r) double result
factorial(n) / (factorial(r) factorial(n-r))
return static_castltintgt(result) int main()
int n cout ltlt "Please enter n" cin gtgt
n cout ltlt " r n choose r" ltlt endl
cout ltlt "--- ----------" ltlt endl for (int
r 0 r lt n r) cout ltlt setw(3) ltlt r ltlt
setw(15) ltlt choose(n, r) ltlt endl return
0
128Function Placement
- 2 key ideas
- The engine of a C program is the main method
- allocates tasks to other functions
- program functionality can usually be inferred
from a well written main method, as opposed to a
poorly written one - ie. suppose someone asks "What does your
program do?" - "program outputs n choose r for all 0 lt r lt n"
- the user is typically interested in what function
does, not how it does it - ex. square root
129- Main function code is more interesting to user
than helper functions - thus, it is typically placed near the top of your
file, before other functions - however, if we place a function after the main
function, we can't call it from main - C must know the parameter types and return type
of a function before it can be called, for type
checking
130- With function factorial and choose
include ltiostreamgt include ltiomanipgt using
namespace std int main() int n cout ltlt
"Please enter n" cin gtgt n cout ltlt " r
n choose r" ltlt endl cout ltlt "---
----------" ltlt endl for (int r 0 r lt n
r) cout ltlt setw(3) ltlt r ltlt setw(15) ltlt
choose(n, r) ltlt endl return 0 double
factorial(int x) double result 1.0 for
(int i 1 i lt x i) result i return
result int choose(int n, int r) double
result factorial(n) / (factorial(r)
factorial(n-r)) return static_castltintgt(result)
131- With function factorial and choose
include ltiostreamgt include ltiomanipgt using
namespace std int main() int n cout ltlt
"Please enter n" cin gtgt n cout ltlt " r
n choose r" ltlt endl cout ltlt "---
----------" ltlt endl for (int r 0 r lt n
r) cout ltlt setw(3) ltlt r