Posted on

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

There are 6 questions in it. I got points off from Questions 6. See feedback from TA. I hope it can help anyone.

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

Question #1

## Question #1

Write two versions of a program that reads a positive integer n, and prints the first n even

numbers.

a) In the first, use a while loop.

b) In the second, use a for loop.

Each section should interact with the user exactly as it shows in the following example:

```Please enter a positive integer: 3
2
4
6 ```
```#include <iostream>

using namespace std;

int main() {

int timesToLoop, evenNum;
int counter = 1;

cout << "Enter a positive integer: ";
cin >> timesToLoop;

cout << endl <<  "Section a: "<< endl;

while (counter <= timesToLoop){
evenNum = counter * 2;
cout << evenNum << endl;
counter++;
}

cout << endl <<  "Section b: "<< endl;

for(int i = 1; i <= timesToLoop; i++){
evenNum = i * 2;
cout << evenNum << endl;
}

}

```
Question #2

## Question #2

In this question we will use a simplified version of the Roman Numerals System to represent positive integers.

The digits in this system are I, V, X, L, C, D and M. Each digit corresponds to a decimal value, as shown in the following table:

A number in the simplified Roman numerals system is a sequence of Roman digits, which follow these 2 rules:

1. The digits form a monotonically non-increasing sequence. That is the value of each digit is less than or equal to the value of the digit that came before it.

For example, DLXXVI is a monotonically non-increasing sequence of Roman digits, but XIV is not.

2. There is no limit on the number of times that βMβ can appear in the number.

βDβ, βLβ and βVβ can each appear at most one time in the number.

βCβ, βXβ and βIβ can each appear at most four times in the number.

For example: IIII, XVII and MMMMMMDCCLXXXXVII are legal numbers in our simplified Roman numeral system, but IIIII, XIV, VVI and CCXLIII are not.

Write a program that reads from the user a (decimal) number, and prints itβs representation in the simplified Roman numerals system.

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

```Enter decimal number:
147
147 is CXXXXVII ```
```#include <iostream>
#include <string>

using namespace std;

int main() {

int valueEntered;
string finalRomanString;

cout << "Please enter a decimal number: ";
cin >> valueEntered;

cout << valueEntered;

while (valueEntered > 0){

int romanInNumber;
string romanNumeral;

if(valueEntered >= 1000){
romanInNumber = 1000;
romanNumeral = "M";
}else if (valueEntered < 1000 && valueEntered >= 500){
romanInNumber = 500;
romanNumeral = "D";
}else if (valueEntered < 500 && valueEntered >= 100){
romanInNumber = 100;
romanNumeral = "C";
}else if (valueEntered < 100 && valueEntered >= 50){
romanInNumber = 50;
romanNumeral = "L";
}else if (valueEntered < 50 && valueEntered >= 10){
romanInNumber = 10;
romanNumeral = "X";
}else if (valueEntered < 10 && valueEntered >= 5){
romanInNumber = 5;
romanNumeral = "V";
}else {
romanInNumber = 1;
romanNumeral = "I";
}

int timesToRepeatLetter = valueEntered / romanInNumber;
valueEntered = valueEntered % romanInNumber;

while (timesToRepeatLetter--){
finalRomanString += romanNumeral;
}

}

cout << " is " << finalRomanString << endl;
}
```
Question #3

## Question #3

Write a program that reads from the user a positive integer (in a decimal representation), and prints its binary (base 2) representation.

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

```Enter decimal number:
76
The binary representation of 76 is 1001100 ```
```#include <iostream>

using namespace std;

const int BASE_10 = 10;

int main() {

int enteredNumber, remainingValue, binaryNumber = 0, startingCounter = 1;

cout << "Enter a decimal number: ";
cin >> enteredNumber;

cout << "The binary representation of "<< enteredNumber;

while (enteredNumber != 0){
remainingValue = enteredNumber % 2;
enteredNumber /= 2;
binaryNumber += remainingValue * startingCounter;
startingCounter *= BASE_10;
}

cout <<" is " << binaryNumber << endl;

return 0;
}
```
Question #4

## Question #4

Write two versions of a program that reads a sequence of positive integers from the user,

calculates their geometric mean, and print the geometric mean.

a) First read the length of the sequence.

For example, an execution would look like:

```Please enter the length of the sequence: 3
1
2
3
The geometric mean is: 1.8171 ```

b) Keep reading the numbers until -1 is entered.

For example, an execution would look like:

```Please enter a non-empty sequence of positive integers, each one in a separate line. End your sequence by typing -1:
1
2
3
-1
The geometric mean is: 1.8171 ```
```#include <iostream>
#include <cmath>

using namespace std;

int main() {

/*
* Section A
*/

int sequenceEntered, numberEntered, sequenceTotal = 1;
double geometricMean;

cout << "Section A: "<< endl << endl;

cout << "Please enter the length of the sequence: ";
cin >> sequenceEntered;

for(int i=0; i < sequenceEntered; i++){
cin >> numberEntered;
sequenceTotal *= numberEntered;
}

geometricMean = pow(sequenceTotal, 1.0/(double)sequenceEntered);

cout << "The geometric mean is: " << geometricMean << endl;

/*
* Section B
*/

cout << endl << "Section B: "<< endl << endl;

int numberEnteredSectionB, sequenceTotalSectionB = 1, counterSectionB = 0;
double geometricMeanSectionB;

cout << "Please enter a non-empty sequence of positive integers, each one in a separate line. End your sequence by typing -1:" <<endl;

while (numberEnteredSectionB != -1){
cin >> numberEnteredSectionB;

if(numberEnteredSectionB == -1){
break;
}

sequenceTotalSectionB *= numberEnteredSectionB;
counterSectionB++;

}

geometricMeanSectionB = pow(sequenceTotalSectionB, 1.0/counterSectionB);

cout << "The geometric mean is: " << geometricMeanSectionB << endl;

return 0;
}
```
Question #5

## Question #5

Write a program that asks the user to input a positive integer n, and prints a textual image of an hourglass made of 2n lines with asterisks.

For example if n=4, the program should print:

``` *******
*****
***
*
*
***
*****
******* ```
```#include <iostream>
using namespace std;

int main() {

int enteredNumber, lineNum, spaceCount, starCount;

cout << "Enter a positive integer: ";
cin >> enteredNumber;

for(lineNum =  1; lineNum <= enteredNumber; lineNum++){
for(spaceCount = 1; spaceCount <= lineNum; spaceCount++){
cout << " ";
}

for(starCount = 0; starCount <= (enteredNumber * 2) - (lineNum * 2); starCount++){
cout << "*";
}
cout << endl;
}

for(lineNum =  1; lineNum <= enteredNumber; lineNum++){
for(spaceCount = 0; spaceCount <= enteredNumber - lineNum; spaceCount++){
cout << " ";
}

for(starCount = 1; starCount <= (lineNum * 2) - 1; starCount++){
cout << "*";
}
cout << endl;
}

return 0;
}
```
Question #6

## Question #6

Write a program that asks the user to input a positive integer n, and print all of the numbers from 1 to n that have more even digits than odd digits.

For example, if n=30, the program should print:

```2
4
6
8
20
22
24
26
28 ```
```#include <iostream>

using namespace std;

int main() {

int numberEntered;

cout << "Please enter a positive integer: ";
cin >> numberEntered;

for(int i = 1; i < numberEntered; i++){

int evenCounter = 0, numDigits = 0;
int num = i, currentDigit = 0;

while (num > 0){

//get current digit
currentDigit = num % 10;

if( currentDigit % 2 == 0 ){
evenCounter++;
numDigits++;
}

num = num / 10;
}

if(evenCounter >= numDigits){
cout << i << endl;
}
}

return 0;
}
```

Feedback from TA:

Make sure to fix the issues listed by the TA.

Photo by Ilya Pavlov on Unsplash

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