Posted on

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

Question #1

The Fibonacci numbers sequence, Fn, is defined as follows: F1 is 1, F2 is 1, and Fn = Fn-1 + Fn-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

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

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

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

Question #5

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

a. 5n3 + 2n2 + 3n = Θ(𝑛3)

b. √7n2+ 2n βˆ’ 8 = Θ(𝑛)

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

Photo by Kevin Ku on Unsplash

13 Replies to “Study Resource 6 – NYU Bridge to Tandon Program”

  1. 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

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.