Skip to content

Commit ae852e1

Browse files
committed
added exercise01 to chapter13
1 parent 21e4db2 commit ae852e1

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

Chapter13/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,5 @@ Why would you format your input rather than just having the file contain "the nu
110110

111111
## [Review 14](review/14.txt)
112112
How do you plan the general layout of a graph? How do you reflect that layout in your code?
113+
114+
## [Exercise 1](exercise/01/01.cpp)

Chapter13/exercises/01/01.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include <iostream>
2+
3+
// I think this is more readable
4+
int iterative_fac(int n) {
5+
int j = 1;
6+
while (n > 1)
7+
j *= n--;
8+
return j;
9+
}
10+
11+
// This is cooler though
12+
int recursive_fac(int n) {
13+
return n > 1 ? n * recursive_fac(n-1) : 1;
14+
}
15+
16+
int main() {
17+
for (int i = 0; i < 21; ++i) {
18+
std::cout << i
19+
<< "! == "
20+
<< recursive_fac(i)
21+
<< " (recursive) == "
22+
<< iterative_fac(i)
23+
<< " (iterative)\n";
24+
}
25+
}

0 commit comments

Comments
 (0)