# Homework 1

Due: 2019/02/07 at the beginning of class
Homework turned in after class will not be accepted.
Homework turned in during class but after I have collected it will lose 20%.

Do your own work for this assignment; do not work with others. Consult the book and your professor for help if you need it. Please write neatly, or preferably type your answers. Use good grammar, spelling, and complete sentences.

1. (10 points) You have probably heard of the Fibonacci sequence, where each number in the sequence is the sum of the previous two numbers.

Your professor has recently developed the extended Fibonacci sequence, where each number in the sequence is the sum of the previous three numbers. The base cases are efibonacci(1)=1, efibonacci(2)=1, efibonacci(3)=1.

Give a correct recursive (no loops or arrays) C++ function that computes the new extended Fibonacci sequence for any integer n. THINK ABOUT NON-POSITIVE INPUTS; non-positive inputs should continue the sequence. An example of the input (starting from one) and output of this function is:

```...
efibonacci(1) = 1
efibonacci(2) = 1
efibonacci(3) = 1
efibonacci(4) = 3
efibonacci(5) = 5
efibonacci(6) = 9
efibonacci(7) = 17
...
```
Your code must be concise. My solution is 8 lines long. Explain why your code works, and give some examples of the answers it gives for various inputs, including large values.
2. (10 points) Exercise 2.1 in your textbook.
3. (30 points) Exercise 2.7 in your textbook, part (a).

For parts 1-4, label each finite-time statement with its own constant as we have in class (i.e. c1, c2, etc.). For example, code fragment (1) should be labeled as:

```sum = 0;                      // c1
for (int i = 0;               // c2
i < n;         // c3
i++) {  // c4
sum++;                    // c5
}
```

For parts 1-4, show how you assigned constants for each code fragment, give a precise T(n) for each function before giving your big-Oh answer. For parts 5 and 6, just give a big-Oh answer.

4. (30 points) Consider the program below, and then answer these two questions.
1. What will be the output of the program? Explain the reason.
2. What will be the output of the program if you remove the virtual keyword? Explain the reason.
```#include <iostream>

using namespace std;

class Base {
public:
void foo() { cout << "Base::foo()" << endl; }
virtual void bar() { cout << "Base::bar()" << endl; }
};

class Derived : public Base {
public:
void foo() { cout << "Derived::foo()" << endl; }
virtual void bar() { cout << "Derived::bar()" << endl; }
};

int main() {
Derived *dptr = new Derived();
Base *bptr = dptr;

bptr->foo();
bptr->bar();

delete dptr;

return 0;
}
```

valid html and css