Posted on

This is study resource 5 for the 24-week cohort during the summer of 2019.

There are 5 questions and an extra credit question.

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

Write a program that reads a positive integer n from the user and prints out a nXn multiplication table. The columns should be spaced by a tab.

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

#include <iostream>

using namespace std;

int main() {

    int numEntered;

    cout << "Enter a positive integer: " << endl;
    cin >> numEntered;

    for(int i = 1; i <= numEntered; i++){
        for (int j = 1; j <= numEntered; j++){
            cout << i*j << "\t";
        }
        cout << endl;
    }

    return 0;
}
Question #2

Question #2

Implement a number guessing game. The program should randomly choose an integer between 1 and 100 (inclusive), and have the user try to guess that number.

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

const int ALLOWED_NUMBER_OF_GUESSES = 5;

int main() {

    int guessedNumber, randomNumber, attemptsToGuess = 0, fromRange = 1, toRange = 100;
    bool allowedToContinue = true, isProgramOver = false;

    srand(time(0));

    cout << "I thought of a number between 1 and 100! Try to guess it." << endl;
    randomNumber = (rand() % toRange) + 1;

    while(allowedToContinue && !isProgramOver){

        if(attemptsToGuess < ALLOWED_NUMBER_OF_GUESSES){
            cout << "Range: [" << fromRange <<", " <<  toRange <<  "], Number of guesses left: " << ALLOWED_NUMBER_OF_GUESSES - attemptsToGuess << endl;
            cout << "Your guess:";
            cin >> guessedNumber;

            attemptsToGuess++;

            if(guessedNumber < randomNumber){
                fromRange = guessedNumber + 1;
                cout << "Wrong! My number is bigger."<< endl<< endl;
            }else if(guessedNumber > randomNumber){
                toRange = guessedNumber - 1;
                cout << "Wrong! My number is smaller."<< endl<< endl;
            }else{
                isProgramOver = true;
                cout << "Congrats! You guessed my number in " << attemptsToGuess << " guess(es)." << endl;
            }
        }else{
            isProgramOver = true;
            allowedToContinue = false;
            cout << "Out of guesses! My number is " << randomNumber << endl;
        }

    }
    return 0;
}
Question #3

Question #3

According to TA’s feedback – answer should be:

According to TA’s feedback, the answer should be the following:

Question #4

Question #4

Question #5

Question #5

Extra Credit

Photo by Fotis Fotopoulos on Unsplash

Leave a Reply

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