This is study resource 6 for the 24-week cohort during the summer of 2019. There are 5 questions in it.

Do not just copy and paste the answers, you must understand the content in order to do well in the NYU Bridge To Tandon program. Good luck!.

Click here to see all the study resources.

## Question #1

The Fibonacci numbers sequence, Fn, is defined as follows: F_{1} is 1, F_{2} is 1, and Fn = F_{n-1} + F_{n-2} for n = 3, 4, 5, … In other words, each number is the sum of the previous two numbers. The first 10 numbers in Fibonacci sequence are: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 Note: Background of Fibonacci sequence: https://en.wikipedia.org/wiki/Fibonacci_number

1. Write a function int fib(int n) that returns the n-th element of the Fibonacci

sequence.

2. Write a program that prompts the user to enter a positive integer num, and then

prints the numโs elements in the Fibonacci sequence.

Your program should interact with the user exactly as it shows in the following

example:

Please enter a positive integer: 7 13

```
#include <iostream>
using namespace std;
int fib(int n);
int main() {
int numEntered, fibonacciElement;
cout << "Please enter a positive integer: ";
cin >> numEntered;
fibonacciElement = fib(numEntered);
cout << fibonacciElement;
return 0;
}
int fib(int n){
if ( n == 0 ){
return 0;
}
if (n < 3){
return 1;
}
//Since position F1 = F2 = 1, then prevVal1 & 2 are default to 1
int positionVal1 = 1, positionVal2 = 1, currentValue = 0;
for(int position = 3; position <= n; position++){
currentValue = positionVal1 + positionVal2;
positionVal1 = positionVal2;
positionVal2 = currentValue;
}
return currentValue;
}
```

## Question #2

Write a program that, prints a โpine treeโ consisting of triangles of increasing sizes, filled with a character (eg. โ*โ or โ+โ or โ$โ etc). Your program should interact with the user to read the number of triangles in the tree and the character filling the tree.

Your implementation should include the following functions:

a. void printShiftedTriangle(int n, int m, char symbol)

It prints an n-line triangle, filled with symbol characters, shifted m spaces from the left margin.

b. void printPineTree(int n, char symbol)

It prints a sequence of n triangles of increasing sizes (the smallest triangle is a 2-line triangle), which forms the shape of a pine tree. The triangles are filled with the symbol character.

```
#include <iostream>
using namespace std;
void printShiftedTriangle (int n, int m, char symbol);
void printPineTree (int n, char symbol);
int main() {
int numberOfLines, numberOfSpaces, numberOfTriangles;
char characterToPrint;
cout << "Enter the number of lines for the triangle (positive integer): ";
cin >> numberOfLines;
cout << "Enter the number of spaces to the left: ";
cin >> numberOfSpaces;
cout << "Enter the character to use (e.g. *, +, $): ";
cin >> characterToPrint;
cout << endl;
printShiftedTriangle(numberOfLines,numberOfSpaces,characterToPrint);
cout << endl;
cout << endl;
cout << "Enter the number of triangles: ";
cin >> numberOfTriangles;
cout << "Enter the character to use (e.g. *, +, $): ";
cin >> characterToPrint;
printPineTree(numberOfTriangles,characterToPrint);
return 0;
}
void printShiftedTriangle (int n, int m, char symbol){
//n = total # of rows
//m = total # of left spaces
for(int row = 1; row <= n; row++){
for(int colSpaces = 1; colSpaces <= (n-row) + m; colSpaces++){
cout << " ";
}
for(int colStars = 1; colStars <= (2*row) - 1; colStars++){
cout << symbol;
}
cout <<endl;
}
}
void printPineTree (int n, char symbol){
for(int i = 1; i <= n; i++){
printShiftedTriangle(i+1,n-i,symbol);
}
}
```

## Question #3

The number *e* is an important mathematical constant that is the base of the natural logarithm. *e* also arises in the study of compound interest, and in many other applications.

Background of e: https://en.wikipedia.org/wiki/E_(mathematical_constant) *e* can be calculated as the sum of the infinite series:

The value of e is approximately equal to 2.71828. We can get an approximate value of *e*, by calculating only a partial sum of the infinite sum above (the more addends we add, the better approximation we get).

Implement the function: *double eApprox(int n)*

This function is given a positive integer n, and returns an approximation of *e*, calculated by the sum of the first (n+1) addends of the infinite sum above.

To test your function use the following main:

Notes:

1. Pay attention to the running time of eApprox. An efficient implementation would run in ฮ(๐).

2. Since the values of the factorials will grow to be very large, use a variable of type double to store them.

```
#include <iostream>
using namespace std;
double eApprox( int n );
int main() {
cout.precision(30);
for(int n=1; n <= 15; n++){
cout << "n = " << n << "\t" << eApprox(n) << endl;
}
return 0;
}
double eApprox( int n ){
double result = 1.0;
double currentFactorial = 1.0;
for(int i = 1; i <= n; i++){
currentFactorial *= i;
result += (1/currentFactorial);
}
return result;
}
```

## Question #4

**a.** Implement a function: *void printDivisors(int num)*

This function is given a positive integer num, and prints all of numโs divisors in **ascending** order, separated by a space.

For Example, if we call *printDivisors(100)*, the expected output is:

1 2 4 5 10 20 25 50 100

Implementation requirement: Pay attention to the running time of your function. An efficient implementation would run in ฮ โ๐๐ข๐4.

**b.** Use the function above when implementing a program that reads from the user a positive integer (โฅ2), and prints all itโs divisors.

Your program should interact with the user exactly as it shows in the following example:

Please enter a positive integer >= 2: 100 1 2 4 5 10 20 25 50 100

```
#include <iostream>
#include <cmath>
using namespace std;
void printDivisors(int num);
int main() {
int numberEntered;
cout << "Please enter a positive integer >= 2: ";
cin >> numberEntered;
if(numberEntered > 1){
printDivisors(numberEntered);
}else{
cout << "Invalid input, please try again!";
}
cout << endl;
return 0;
}
void printDivisors(int num){
for(int i=1; i <= sqrt(num); i++){
if(num % i == 0){
cout << i << " ";
}
}
for(int i=sqrt(num); i >= 1; i--){
if(num % i == 0){
if(num / i != i){
cout << num / i << " ";
}
}
}
}
```

## Question #5

Use the definition of ฮ in order to show the following:

**a.** 5n^{3} + 2n^{2} + 3n = ฮ(๐^{3})

**b**. โ7n^{2}+ 2n โ 8 = ฮ(๐)

No answer available. If you have the answer for these 2 questions, share it with everyone in the comment section below. Thanks!

very useful do u have exam samples and/or exam questions.

very useful do u have exam samples

Hi Mellisa, send me an email. I may be able to help. Good luck.

can you send an email of exam samples, thank you.3158944@gmail.com

I want some test samples too, please. 3158944@gmail.com

Kindly tell me the email address so I can receive your message. Thanks!

Hi, may i also give my email for the resources?

Hi Tahsina,

You can now find these resources by visiting – https://csbridgehelp.com/resources/exam-resources/exam-resource/

Good luck.

vconatct@gmail.com

Hi, could you send exam question or practice exam to me if any ? My email is liujinglg@gmail.com. Thanks!

hello are the week 5 questions related to algoritham and data structures.

Hi, xstudent:

My name is Max and I was admitted to the Bridge to Tandon Program for the Spring 2021 session. I have a quick question here: will the exam questions look similar to the questions in the study material? If so, how many questions will be there in a single exam? Thanks for answering my questions and have a wonderful week!

Sincerely,

Max

Hi Max, congratulations on your acceptance to the program. Some of the questions are similar to the homework, but I would recommend practicing as much as possible since this is a tough program. I have added some exam resources to the site. I hope this can help you out. Good Luck! Exam Resources โ NYU Bridge To Tandon Program