Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Greetings, I just began taking C++ again and I seem to be totally lost at anothe

ID: 3666412 • Letter: G

Question

Greetings,

I just began taking C++ again and I seem to be totally lost at another level. I need to be able to answer this and explain how it's done. Can anyone help code these solutions and explain how it's done (not just post solution)? Much appreciated. -Mike

Problem 1 (Displaying pyramid) Write a program that prompts the user to enter an integer from 1 to 9 and display a pyramid, as shown in the following sample output:

Enter the number of lines: 5

                                    1

                                2 1 2

                            3 2 1 2 3

                        4 3 2 1 2 3 4

                    5 4 3 2 1 2 3 4 5

Problem 2 (Displaying calendar) Write a program that prompts the user to enter the year and first day of the year and displays on the console the calendar table for the year. For example, if the user entered the year 2005, and 6 for Saturday, January 1, 2005, your program should display the calendar for each month in the year, as follows:

                 January 2005

---------------------------------------------------

Sun      Mon     Tue      Wed    Thu      Fri        Sat

                                                                        1

2          3          4          5          6          7          8

9          10        11        12        13        14        15

16        17        18        19        20        21        22

23        24        25        26        27        28        29

30        31

…….

                December 2005

---------------------------------------------------

Sun      Mon     Tue      Wed    Thu      Fri        Sat

                                                1          2          3

4          5          6          7          8          9          10

11        12        13        14        15        16        17

18        19        20        21        22        23        24

25        26        27        28        29        30        31

(Hint: the number of days in February for different year may be different, which depends on whether it is a leap year or plain year. A year is leap year if either (1) or (2) holds: (1) it is divisible by 400. (2) it is divisible by 4 but not divisible by 100.

Explanation / Answer

I am giving the code of both the problem along the comments that will make you easy for understanding AND KNOW ABOUT THE working of the code:

PROBLEM:

#include <iostream.h>

int main()

{// variable declaration//

   int i,space,rows,k=0,count=0,count1=0;

   cout << "Enter the number of rows: ";

   cin >> rows;     

   for(i=1;i<=rows;++i)   // outer loop fpr checking the row to print//

   {

       for(space=1;space<=rows-i;++space)

       {

          cout << " "; // for spacing//

          ++count;

        }

        while(k!=2*i-1) // loop that check value and print the pattern //

        {

           if (count<=rows-1)

           {

             cout << " " << (i-k);

             ++count;

           }

           else

           {

             ++count1;

              cout << " " << (i-k+2*count1);

           }

           ++k;

        }

        count1=count=k=0;

        cout << endl;

    }

    return 0;

}

PROBLEM 2:

#include <iostream.h>

#include <iomanip.h>

using namespace std;

int main()

{// variable declaration//

int monthdayss[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

int Wday = 0;

int yr;

int days;

int MonthFirstDays = 0;

cout << "Please enter the year: "; //enter the year //

cin >> yr;

if (yr < 0)

{

cout << "The year starting from 0…!" << endl;// here year starting from 0 for jan…//

return 0;}

cout << " enter the days of the week of January 1st starting from 0 Sundays…" << endl;

cin >> days;

if (days < 0 || days > 6)

{

cout << "The days of January 1st must be between 0 and 6!" << endl;

return 0;}

cout << endl << endl;

// year's calendar display//

for (int month = 0; month < 12; ++month) {

{

for (int i = 0; i < Wday; ++i)

{cout << setw(3) << " ";}

if (month == 0)

{cout << "January ";}

if (month == 1)

{cout << "February ";

if (yr % 400 == 0 || (yr % 4 == 0 && yr % 100 != 0))

monthdayss[1] = 29;}

if (month == 2)

{cout << "March ";}

if (month == 3)

{cout << "April ";}

if (month == 4)

{cout << "May ";}

if (month ==5)

{cout << "June ";}

if (month == 6)

{cout << "July ";}

if (month == 7)

{cout << "August ";}

if (month == 8)

{cout << "September ";}

if (month == 9)

{cout << "October ";}

if (month == 10)

{cout << "November ";}

if (month == 11)

{cout << "December ";}

}

for (int i = 0; i < days; i++)//provide spaces

    {cout << " ";

}

//here check the first days //

days = 0;

    for (MonthFirstDays; MonthFirstDays < days; ++MonthFirstDays)

    {  

        cout << setw(3);

    }

// month's calendar display here//

for (int mdays = 0; mdays < monthdayss[month]; ++mdays) {

cout << setw(3) << mdays + 1;

Wday++;

if (Wday == 7) {

cout << " " << setw(3);

Wday = 0;

}

}

//next month//

if (Wday != 7) {

cout << " ";

}

cout << endl;

days = Wday + 1;

}

return 0;

}