This is study resource 7 for the 24-week cohort during the summer of 2019. There are 7 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

a. Implement a function: *int printMonthCalender(int numOfDays, int startingDay)*

This function is given two parameters:

• numOfDays – The number of days in the month

• startingDay – a number 1-7 that represents the day in the week of the first day

in that month (1 for Monday, 2 for Tuesday, 3 for Wednesday, etc.).

The function should:

• Print a formatted monthly calendar of that month

• Return a number 1-7 that represents the day in the week of the last day in that month.

Formatting Notes:

• The output should include a header line with the days’ names.

• Columns should be spaced by a Tab.

```
#include <iostream>
using namespace std;
int printMonthCalendar(int numOfDays, int startingDay);
bool leapYear (int year);
void printYearCalendar(int year, int startingDay);
int main() {
int yearEntered, startingDayEntered;
cout << "Enter a year and the starting day: ";
cin >> yearEntered >> startingDayEntered;
printYearCalendar(yearEntered,startingDayEntered);
return 0;
}
int printMonthCalendar(int numOfDays, int startingDay){
cout << "Mon\tTue\tWed\tThr\tFri\tSat\tSun" << endl;
int currentDay, lastDay;
int counter = 1;
for(int cell = 1; currentDay < numOfDays; cell++){
currentDay = cell - startingDay + 1;
if(currentDay >= 1){
cout << currentDay;
}
if(cell % 7 == 0){
cout << endl;
}else{
cout << "\t";
}
counter++;
}
lastDay = (counter - 1) % 7;
return lastDay;
}
bool leapYear (int year){
if(year % 400 == 0 || (year % 100 != 0 && year % 4 == 0 ))
return true;
return false;
}
void printYearCalendar(int year, int startingDay){
bool isLeapYear;
int daysInMonth = 0;
int lastDay;
isLeapYear = leapYear(year);
for(int month = 1; month <= 12; month++){
if(month == 1){
cout << "January " << year << endl;
daysInMonth = 31;
}else if(month == 2){
cout << "February " << year << endl;
if(isLeapYear){
daysInMonth = 29;
}else{
daysInMonth = 28;
}
}else if(month == 3){
cout << "March " << year << endl;
daysInMonth = 31;
}else if(month == 4){
cout << "April " << year << endl;
daysInMonth = 30;
}else if(month == 5){
cout << "May " << year << endl;
daysInMonth = 31;
}else if(month == 6){
cout << "June " << year << endl;
daysInMonth = 30;
}else if(month == 7){
cout << "July " << year << endl;
daysInMonth = 31;
}else if(month == 8){
cout << "August " << year << endl;
daysInMonth = 31;
}else if(month == 9){
cout << "September " << year << endl;
daysInMonth = 30;
}else if(month == 10){
cout << "October " << year << endl;
daysInMonth = 31;
}else if(month == 11){
cout << "November " << year << endl;
daysInMonth = 30;
}else{
cout << "December " << year << endl;
daysInMonth = 31;
}
lastDay = printMonthCalendar(daysInMonth, startingDay);
startingDay = lastDay + 1;
cout << endl << endl;
}
}
```

## Question #2

Consider the following definitions:

a. A proper divisors of a positive integer (≥ 2) is any of its divisors excluding the number itself. For example, the proper divisors of 10 are: 1, 2 and 5.

b. A perfect number is a positive integer (≥ 2) that is equal to the sum of its proper divisors. For example, 6 and 28 are perfect numbers, since:

6 = 1 + 2 + 3

28 = 1 + 2 + 4 + 7 + 14

Background of perfect numbers: https://en.wikipedia.org/wiki/Perfect_number

********** THIS PROGRAM IS NOT FULLY WORKING – SEE TA’S FEEDBACK AT THE END OF POST ******* **

```
#include <iostream>
#include <cmath>
using namespace std;
void analyzeDivisors( int num, int& outCountDivs, int& outSumDivs);
bool isPerfect(int num);
int main() {
int numEntered;
cout << "Enter a positive integer greater or equal than 2: ";
cin >> numEntered;
if (numEntered > 1){
cout << "Perfect numbers between 2 and " << numEntered << ": ";
for(int i = 2; i <= numEntered; i++){
if(isPerfect(i)){
cout << i << " ";
}
}
cout << endl;
for(int i = 2; i <= numEntered; i++){
for(int j = i; j <= numEntered; j++){
int countDivsOne = 0, countDivsTwo = 0;
int sumDivsOne = 0, sumDivsTwo = 0;
analyzeDivisors(i, countDivsOne, sumDivsOne);
analyzeDivisors(j, countDivsTwo, sumDivsTwo);
if(i == sumDivsTwo && j == sumDivsOne && i != j){
cout << i << " and " << j << " are amicable. " << endl;
}
}
}
}else{
cout << "Invalid input. Please try again.";
}
return 0;
}
void analyzeDivisors( int num, int& outCountDivs, int& outSumDivs){
int sumDivs = 0, numberOfDivs = 0;
for(int i = 1; i < sqrt(num); i++ ){
if(num % i == 0){
numberOfDivs++;
sumDivs += i;
}
}
for(int i = sqrt(num); i > 1; i--){
if(num % i == 0){
if(num / i != 1){
numberOfDivs++;
sumDivs += num/i;
}
}
}
outCountDivs = numberOfDivs;
outSumDivs = sumDivs;
}
bool isPerfect(int num){
int sumOfDiv = 0;
int count = 0;
analyzeDivisors(num, count, sumOfDiv);
return (sumOfDiv == num);
}
```

********** THIS PROGRAM IS NOT FULLY WORKING ******* **

See the TA’s feedback

## Question #3 to #7

Unfortunately, I do not have the answers to these questions. I really hope this gives you a head start. Good luck!

Photo by Fabian Grohs on Unsplash