Data structures and algorithms in C++
Data structures and algorithms in C++
2nd Edition
ISBN: 9780470460443
Author: Goodrich
Publisher: WILEY
bartleby

Videos

Expert Solution & Answer
Book Icon
Chapter 1, Problem 2P

Explanation of Solution

Program code:

//include the required header files

#include <iostream>

#include <string>

#include <iomanip>

#include <fstream>

//use the std namespace

using namespace std;

//create a string array MONTHARRAY

const string MONTHARRAY[12] = { " January", " February", " March", " April", " May", " June", " July", " August", " September", " October", " November", " December"};

//create a string array DAYARRAY

const string DAYARRAY[7] = {"Sun ", " Mon ", " Tue ", " Wed ", " Thu ", " Fri ", " Sat"};

int a,y,day,m,d, x;

//create integer variable month

int month;

//define two functions printMonth() and getFirst

void printMonth(int month, int startDay, int year);

int getFirst(int month, int day, int year);

//define the main() function

int main (void)

{

//prompt the user to enter the year later than 1582

cout<<"Please enter a year later than 1582: ";

//scan for the value

cin>>x;

//if the value of x is greater than 1582

if(x>1582)

{

//iterate a for loop

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

{

//call the methods

getFirst(i, 1, x);

printMonth(i, d, x);

}

}

//if the value of x is less than 1582

else

{

//print the statement

cout<<"That year is before 1582, please enter a better year: ";

//scan for the value

cin>>x;

}

//return 0

return 0;

}

//define the method getFirst()

int getFirst(int month, int day, int year)

{

//set the values

a = (14-month)/12;

y = year-a;

m = month+12*a-2;

d = (day+y+y/4-y/100+y/400+(31*m/12))%7;

//return the value of d

return d;

}

//define the method printMonth()

void printMonth(int month, int startDay, int year)

{

//create integer array to hold the dates

int daysInMonths[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

//create two integer variables

int copy = 1;

int counter = 1;

//print the values of M ONTHARRAY[]

cout<<MONTHARRAY[month]<<endl;

//finding leap year

//if the year is divisible by 4

if(year%4 == 0)

//set the daysInMonths[1] as 29

daysInMonths[1] = 29;

//iterate a for loop

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

{

//print the values of DAYARRAY[]

cout<<DAYARRAY[i];

//if the value i is 6

if(i == 6)

//new line

cout<<endl;

    }

//iterate a while loop

while(copy<=daysInMonths[month])

{

//if the value of counter is less than or equal to startDay

if(counter <= startDay)

//print the value of left and call the method setw()

cout<<left<<setw(5)<<" ";

//if the value of counter is greater than startDay

else

{

//print the value of left and call the method setw()

cout<<left <<setw(5) <<copy;

//increment the value of copy by 1

copy++;

}

//if the counter is divisible by 7

if(counter%7 == 0 )

//print a new line

cout<<endl;

//increment the counter by 1

counter++;

}

//print a new line

cout<<endl;

}

Explanation:

The above snippet of code is used create the calendar of any year later 1582. Check the leap year. In the code,

  • Include the required header files.
  • Use the “std” namespace.
  • Create string array “MONTHARRAY[]” and “DAYARRAY[]”.
  • Create the required integer variables.
  • Declare two functions “printMonth()” and “getFirst()”.
  • Define “main()” function.
    • Prompt the user to enter the year later than 1582.
    • Scan for the value.
    • If the value of “x” is greater than 1582.
      • Iterate a “for” loop.
        • Call the method “getFirst()” and “printMonth()”.
    • If the value of “x” is less than 1582.
      • Prompt the statement.
      • Print the value of “x”.
    • Return “0”.
    • Define a method “printMonth()”.
      • Declare integer array, “daysInMonth[]” and variables “copy”, “counter”.
      • Print the values of the array “MONTHARRAY[]”.
      • If the year is divisible by “4”.
        • Iterate a “for” loop;
          • Print the values of the array “DAYARRAY[]”.
          • If the value of “i” is equal to “6”.
            • Print a new line.
              • Iterate a “while” loop.
                • If the value of “counter” is less than or equal to “startDay”.
                  • Print the value of “left” and “setw(5)”.
                • If the value of “counter” is greater than “startDay”.
                  • Print the value of “left” and “setw(5)”.
                  • Increment the value of “copy” by 1.
                • If the value of “counter” is divisible by “7”.
                  • Print a new line.
                • Increment the “counter” by 1
              • Print a new line.

Output:

Please enter a year later than 1582: 1992

 January

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

 February

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

 March

Sun  Mon  Tue  Wed  Thu  Fri  Sat

          ;&#x...

Blurred answer
Students have asked these similar questions
Payroll Flowchart Analysis Discuss the risks depicted by the following payroll system flowchart. Describe the internal control improvements to the system that are needed to reduce these risks with specific explanations. An illustration of the above flowchart: It is represented in six columns and the column header reads: supervisor; payroll; data processing; accounts payable; cash disbursement; general ledger. In supervisor column, timekeeper enters into time cards and that leads to “Review Time cards and prepares Personnel Action Form” that further leads to two source documents “time cards, per action form.” Per action, form enters into prepare payroll in payroll column. Prepare payroll enters into paychecks and two source documents “payroll register, payroll register.” Payroll register leads to inverted triangle and payroll leads to A. Paycheck enters into review and distribute in supervisor column leads to paycheck and that further leads to employees. In data processing column,…
Using the following execution shown below, explain what is done in each of the ARIES recovery algorithm phases: LSN   LOG 00   begin_checkpoint 10 end_checkpoint 20 update: T1 writes P1 30 update: T2 writes P2 40 update: T3 writes P3 50 T2 commit 60 update: T3 writes P2 70 T2 end 80 update: T1 writes P5 90 T3 abort   CRASH, RESTART In addition to the execution shown here, the system crashes during recovery after writing two log records to stable storage and again after writing another two log records
A new application is being developed and will be using a database that includes a relation about items: Item (item_id:integer, item_name:string, color:string, price:real). Both the purchasing department in charge of obtaining raw material and the manufacturing department in charge of manufacturing the items can change the price of manufactured items according to changes that may happen in the raw material cost or production cost. The two departments use different transactions to update the price of items. The new application uses the following sequences of actions, listed in the order they are submitted to the DBMS: Sequence S1: T1:R(X), T2:W(X), T2:W(Y), T3:W(Y), T1:W(Y), T1:Commit, T2:Commit, T3:Commit Sequence S2: T1:R(X), T2:W(Y), T2:W(X), T3:W(Y), T1:W(Y), T1:Commit, T2:Commit, T3:Commit For each of the following concurrency control mechanisms, describe how they will handle each of the sequences (S1 & S2). Strict 2PL with timestamps used for deadlock prevention. Conservative…

Chapter 1 Solutions

Data structures and algorithms in C++

Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education
Dynamic Programming - Learn to Solve Algorithmic Problems & Coding Challenges; Author: FreecodeCamp.org;https://www.youtube.com/watch?v=oBt53YbR9Kk;License: Standard YouTube License, CC-BY