-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
81 lines (64 loc) · 1.89 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <assert.h>
#include <iostream>
using namespace std;
int getIthFibNumberIter(int fibNumber) {
// Fibonacci numbers 1 and 2 are 1
if ( fibNumber <= 2) {
return 1;
}
int nMinus1 = 1;
int currentFib = 1;
for ( int i=3; i<=fibNumber; i++) {
int temp = currentFib;
currentFib += nMinus1;
nMinus1 = temp;
}
return currentFib;
}
int getIthFibNumberRecur(int fibNumber) {
// Fibonacci numbers 1 and 2 are 1
if ( fibNumber <= 2) {
return 1;
}
return getIthFibNumberRecur(fibNumber-1) +
getIthFibNumberRecur(fibNumber-2);
}
int& fallOffTheStack(const int value) {
int myVal = value;
cout << "Returning reference to " << value << endl;
return myVal;
}
void swapWithoutTemp(int first, int second) {
int myArray[2];
myArray[0] = first;
myArray[1] = second;
myArray[0] += myArray[1];
myArray[1] = myArray[0] - myArray[1];
myArray[0] -= myArray[1];
assert(myArray[0]==second);
assert(myArray[1]==first);
}
int main() {
// swapWithoutTemp
char myCharArray[5] = {'a', 'b', 'c', 'd', '\0'};
char* myOtherCharArray = "abcd";
cout << myCharArray << endl << myOtherCharArray << endl;
swapWithoutTemp(4000000000, 1999999999);
swapWithoutTemp(50, 51);
swapWithoutTemp(-1, -4);
int& willChange = fallOffTheStack(5);
cout << "Was " << willChange << endl;
fallOffTheStack(10);
cout << "Now " << willChange << endl;
assert(getIthFibNumberIter(2) == 1);
assert(getIthFibNumberRecur(2) == 1);
assert(getIthFibNumberIter(3) == 2);
assert(getIthFibNumberRecur(3) == 2);
assert(getIthFibNumberIter(4) == 3);
assert(getIthFibNumberRecur(4) == 3);
assert(getIthFibNumberIter(5) == 5);
assert(getIthFibNumberRecur(5) == 5);
assert(getIthFibNumberIter(20) == 6765);
assert(getIthFibNumberRecur(20) == 6765);
return 0;
}