Starting from:
$35

$29

COMP 2012H0 Assignment 1 Calendar Solution

Honor Code

We value academic integrity very highly. Please read the Honor Code section on our course webpage to make sure you understand what is considered as plagiarism and what the penalties are.0The following are some of the highlights:

Do NOT try your "luck" - we use sophisticated plagiarism detection software to find cheaters.0We also review codes for potential cases manually.

The penalty (for BOTH the copier and the copiee) is not just getting a zero in your assignment.0Please read the Honor Code thoroughly.

Serious offenders will fail the course immediately, and there will be additional disciplinary actions0from the department and university, upto and including expulsion.

End of Honor Code

Objectives & Intended Learning Outcomes

The objective of this assignment is to provide you with practice on C++ basic variables and control flows.0Upon completion of this assignment, you should be able to:

1. Use basic variables in C++, like int, char, bool, etc.

2. Use if, if-else, switch statements to control program flow

3. Use relational operators and logical operators to control program flow.

4. Use for, while, and do-while to write loops, as well as use break and continue to0exit loops.

End of Objectives & Intended Learning Outcomes

Source:0https://siliconvalleyyouthbridge.org/calendar/

Introduction

https://course.cse.ust.hk/comp2012h/assignments/PA1/Description/ 1/10

11/25/22, 9:46 PM COMP 2012H Assignment 1: Calendar

The Gregorian calendar0is the de facto international standard and is used almost everywhere in the world for civil purposes.0The widely used solar aspect is a cycle of leap days in a 400-year cycle designed to0keep the duration of the year aligned with the solar year.

The Gregorian calendar has also been implemented in Linux/Unix command: cal0and ncal. If you are using Mac or Linux computers, you may try these0commands in your terminal:

• cal -m 9 20220 September 20220

Su

Mo Tu

We Th

Fr Sa0

0

0

0

1

2

30

4

5

6

7

8

9

100

11

12

13

14

15

16

170

18

19

20

21

22

23

240

25

26

27

28

29

300

• ncal -m 9 20220 September 20220

Mo

5 12 19 260

Tu

6 13 20 270

We

7 14 21 280

Th

1

8 15 22 290

Fr

2

9 16 23 300

Sa

3

10 17 240

Su

4

11 18 250

> cal

20230

20230

January

February

March0

Su Mo Tu We Th Fr Sa

Su Mo Tu We Th Fr Sa

Su Mo Tu We Th

Fr Sa0

1

2

3

4

5

6

7

1

2

3

4

1

2

3

40

8

9

10 11 12 13 14

5

6

7

8

91011

5

6

7

8

9

10

110

15

16

17 18 19 20 21

12 13 14 15

16 17 18

12

13

14

15

16

17

180

22

23

24 25 26 27 28

19 20 21 22

23 24 25

19

20

21

22

23

24

250

29

30

31

26 27 28

26

27

28

29

30

310

April

May

June0

Su Mo Tu We Th Fr Sa

Su Mo Tu We Th Fr Sa

Su Mo

Tu We Th

Fr Sa0

1

1

2

3

4

5

6

1

2

30

2

3

4

5

6

7

8

7

8

9

10

11

12 13

4

5

6

7

8

9

100

9101112131415

14 15

16

17

18

19 20

11

12

13

14

15

16

170

16 17 18 19 20 21 22

21 22

23

24

25

26 27

18

19

20

21

22

23

240

23 24 25 26 27 28 29

28 29

30

31

25

26

27

28

29

300

300

July

August

September0

Su Mo Tu We Th Fr Sa

Su Mo Tu We Th Fr Sa

Su Mo

Tu We Th

Fr Sa0

1

1

2

3

4

5

1

20

2

3

4

5

6

7

8

6

7

8

9101112

3

4

5

6

7

8

90

9101112131415

13 14

15

16

17

18 19

10

11

12

13

14

15

160

16 17 18 19 20 21 22

20 21

22

23

24

25 26

17

18

19

20

21

22

230

23 24 25 26 27 28 29

27 28

29

30

31

24

25

26

27

28

29

300

30 310

October

November

December0

11/25/22, 9:46 PM

COMP 2012H Assignment 1: Calendar

Su Mo Tu We Th Fr Sa

Su Mo Tu We Th Fr Sa

Su Mo Tu We Th Fr Sa0

1

2

3

4

5

6

7

1

2

3

4

1

20

8

9

10

11

12

13

14

5

6

7

8

9

10

11

3

4

5

6

7

8

90

15

16

17

18

19

20

21

12

13

14

15

16

17

18

10

11

12

13

14

15

160

22

23

24

25

26

27

28

19

20

21

22

23

24

25

17

18

19

20

21

22

230

29

30

31

26

27

28

29

30

24

25

26

27

28

29

300

310

0

0

0

0

0

0

0

In this assignment, you are going to implement such a tool on your own to0display and format calendars of different styles!0More details can be found below.

End of Introduction

Description

Please read the FAQ section for some common clarifications. You should check that0a day before the deadline to make sure you don't miss any clarification, even if you have already submitted your work by then.

Gregorian calendar

In this assignment, we focus on Gregorian calendar0only, you may need to read the Wikipedia page0https://en.wikipedia.org/wiki/Gregorian_calendar0on what it is, and most importantly, how many days are there in each month,0especially February. The way of calculating leap year might be different0from what you have learnt before.

Program Overview

After the program is started, it will ask user to input a year0(in range 1800-9999), and then input a month (in range 1-12).0If the user input0is not valid, the program will continue asking the user to input until a valid0one is received.

Next, the program will ask the user whether he/she would like to use0Monday or Sunday as the first day of a week. If user chooses Monday,0then the calendar displayed later will start each week on Monday, and0vice versa.0If the user input0is not valid, the program will continue asking the user to input until a valid0one is received.

Then, the program will ask the user if they want the calendar to0print horizontally or vertically. If user chooses horizontally,0then the calendar will look like the one using cal command0on Linux, otherwise it will look like the one using ncal command.0Again, if the user input0is not valid, the program will continue asking the user to input until a valid0one is received.

Note that in bonus tasks, the user0may input -1 or -3 for month. In this situation,0the next two options (start week on Monday/Sunday, and print horizontally/vertically)0will be disabled. The program will directly print calendar after user enters0the month.

How to calculate which day it is for a certain date?

The only thing you have is that the January 1st of year 1800 is a Wednesday.0All other you need is in the Wikipedia link given in0Gregorian calendar0part above.0Actually you only need to figure out how many days are there in each month.

You are expected to calculate which day it is for a certain date when you0display the calendar later.

The calendar rules used before 1800 are different to the rules we use today, due to0some historic reasons. So in this PA, you only need to work on calendars0after year 1800.

Additional Remarks

https://course.cse.ust.hk/comp2012h/assignments/PA1/Description/ 3/10

11/25/22, 9:46 PM COMP 2012H Assignment 1: Calendar

In this assignment, you can feel free to use anything you know, and there is no0need to restrict yourself to only use what we have taught so far. For example,0if you think using a function will make your codes tidy, feel free to do it.0However, you can only use header file #include <iostream>0.

If you would like to try the bonus tasks, it is very likely that you0will find functions useful.

Otherwise, your codes might look quite messy.

End of Description

Tasks

This section describes all you will need to implement.0You may need to check Program Overview0section to have an overview0of this program before reading this part.

Task 1 - Receive and validate user input

As described in Program Overview, ask the user to0input (1) year, (2) month, (3) start each week on Monday or Sunday, and0(4) print horizontally or vertically.

If the user input is not valid, the program will keep asking the user to input until a valid one is received.0You don't need to consider the cases when user input incorrect data types. For example,0if we want an integer, you can assume the user will always input an integer, not0a character, etc.

For students who do bonus part: You should skip (3) and (4)0if user input month -1 or -3, as described in0Bonus Tasks.

Example: (user input is displayed in red)

Enter year (in range 1800-9999): 1230

Enter year (in range 1800-9999): 123450

Enter year (in range 1800-9999): 18000

Enter month (in range 1-12): -1000

Enter month (in range 1-12): 1000

Enter month (in range 1-12): 1 0

Enter the first day of week ('s' for Sunday, 'm' for Monday): k

Enter the first day of week ('s' for Sunday, 'm' for Monday): o

Enter the first day of week ('s' for Sunday, 'm' for Monday): s

Print horizontally (h) or vertically (v): c0

Print horizontally (h) or vertically (v): o0

Print horizontally (h) or vertically (v): m0

Print horizontally (h) or vertically (v): p0

Print horizontally (h) or vertically (v): h0

[Start printing the calendar, remaining parts are ignored. See examples in other tasks]

Here are some code snippets that you can copy and paste to your program,0if you find it convenient.

cout << "Enter year (in range 1800-9999): ";0

cout << "Enter month (in range 1-12): ";0

cout << "Enter the first day of week ('s' for Sunday, 'm' for Monday): ";0

cout << "Print horizontally (h) or vertically (v): ";

Task 2 - Horizontal calendars with week starts on Sunday

When user chooses to start each week on Sunday and print the calendar horizontally,0display the calendar in such style.

https://course.cse.ust.hk/comp2012h/assignments/PA1/Description/ 4/10

11/25/22, 9:46 PM COMP 2012H Assignment 1: Calendar

First a title should be displayed, with format "[Month Name] [Year]", for example, "January 1800". To make it easier, please make the title left-aligned.

Then, display the names of weekdays, from "Su" to "Sa", words should be0separated by one space.

Finally, format the calendar like the example below. Dates should be0separated by one space, and for dates with only one digit, the digit0should be right-aligned. After printing the calendar, the program finishes.

It's ok to have extra spaces at the end of each line.0See details in Grading sectio

Note: This should be the same as Linux command0cal -m [month] [year], except that you don't need to0align the title or ouput additional blank line at the end.

Example: (user input is displayed in red)

Enter

year (in

range 1800-9999): 18000

Enter

month (in range 1-12): 10

Enter

the first day of week ('s' for Sunday, 'm' for Monday): s

Print horizontally (h) or vertically (v): h0

January 18000

Su

Mo Tu

We Th

Fr Sa0

1

2

3

40

5

6

7

8

9

10

110

12

13

14

15

16

17

180

19

20

21

22

23

24

250

26

27

28

29

30

31

Task 3 - Horizontal calendars with week starts on Monday

When user chooses to start each week on Monday and print the calendar horizontally,0display the calendar in such style.

First a title should be displayed, with format "[Month Name] [Year]", for example, "January 1800". To make it easier, please make the title left-aligned.

Then, display the names of weekdays, from "Mo" to "Su", words should be0separated by one space.

Finally, format the calendar like the example below. Dates should be0separated by one space, and for dates with only one digit, the digit0should be right-aligned. After printing the calendar, the program finishes.

It's ok to have extra spaces at the end of each line.0See details in Grading section

Example: (user input is displayed in red)

Enter year (in range 1800-9999): 18000

Enter month (in range 1-12): 10

Enter the first day of week ('s' for Sunday, 'm' for Monday): m

Print horizontally (h) or vertically (v): h0

January 18000

Mo Tu We Th Fr Sa Su0

1 2 3 4 50

• 7 8 91011120

13 14 15 16 17 18 190

20 21 22 23 24 25 260

27 28 29 30 31

Task 4 - Vertical calendars with week starts on Sunday

When user chooses to start each week on Sunday and print the calendar vertically,0display the calendar in such style.

First a title should be displayed, with format "[Month Name] [Year]", for example, "January 1800". To make it easier, please make the title left-aligned.

Then, display the names of weekdays, from "Su" to "Sa",0as the first column on the left. Those names0and dates you will print later (remaining columns) should be0separated

https://course.cse.ust.hk/comp2012h/assignments/PA1/Description/ 5/10

11/25/22, 9:46 PM COMP 2012H Assignment 1: Calendar

by one space.

Finally, format the calendar vertically, like the example below.0Dates should be separated by one space, and for dates with only one digit, the digit0should be right-aligned.

After printing the calendar, the program finishes.

It's ok to have extra spaces at the end of each line.0See details in Grading section

Example: (user input is displayed in red)

Enter

year (in

range 1800-9999): 18000

Enter

month (in range 1-12): 10

Enter

the first day of week ('s' for Sunday, 'm' for Monday): s

Print horizontally (h) or vertically (v): v0

January 18000

Su

5

12 19

260

Mo

6

13 20

270

Tu

7

14 21

280

We

1

8

15 22

290

Th

2

9

16 23

300

Fr

3

10

17 24

310

Sa

4

11

18 25

Task 5 - Vertical calendars with week starts on Monday

When user chooses to start each week on Monday and print the calendar vertically,0display the calendar in such style.

First a title should be displayed, with format "[Month Name] [Year]", for example, "January 1800". To make it easier, please make the title left-aligned.

Then, display the names of weekdays, from "Mo" to "Su",0as the first column on the left. Those names0and dates you will print later (remaining columns) should be0separated by one space.

Finally, format the calendar vertically, like the example below.0Dates should be separated by one space, and for dates with only one digit, the digit0should be right-aligned.

After printing the calendar, the program finishes.

It's ok to have extra spaces at the end of each line.0See details in Grading section

Note: This should be the same as Linux command0ncal -M [month] [year], except that you don't need to0align the title or ouput additional blank line at the end.

Example: (user input is displayed in red)

Enter

year (in

range 1800-9999): 18000

Enter

month (in range 1-12): 10

Enter

the first day of week ('s' for Sunday, 'm' for Monday): m

Print horizontally (h) or vertically (v): v0

January 18000

Mo

6

13 20

270

Tu

7

14 21

280

We

1

8

15 22

290

Th

2

9

16 23

300

Fr

3

10

17 24

310

Sa

4

11

18 25

Su

5

12

19 26

Bonus Task 1 - Print calendar of a whole year

This is a bonus task, please see grading details in0Grading section

When user chooses to print the calendar of a whole year, indicating by0entering -1 as month, we will start each week on Sunday and print the calendar horizontally.

https://course.cse.ust.hk/comp2012h/assignments/PA1/Description/ 6/10

11/25/22, 9:46 PM COMP 2012H Assignment 1: Calendar

First a title should be displayed, with format "Year [Year no.]", for example,0"Year 1800".

To make it easier, please make the title left-aligned.

Next, for each month:

A sub-title should be displayed, with format "[Month Name]", for example,

"January". To make it easier, please make it left-aligned.

Display the names of weekdays, from "Su" to "Sa", words should be0separated by one space.

Finally, format the calendar like the example below. Dates should be0separated by one space, and for dates with only one digit, the digit0should be right-aligned.

You should print three months horizontally. Two adjacent months0in the same row should be separated by two spaces.

If any of the horizontal three months has 6 rows enumerating the date, the remaining horizontal months should also have 6 rows for alignment.

There is one blank line between two set of horizontal three months.

After printing the calendar, the program finishes.

It's ok to have extra spaces at the end of each line.0See details in Grading section

Note: This should be the same as Linux command0cal [year], except that you don't need to align the title and sub-titles or ouput additional blank line at the end.

Example: (user input is displayed in red)

Enter

year (in

range 1800-9999): 18000

Enter

month (in range

1-12): -10

Year 18000

January

February

March

Su

Mo Tu

We Th

Fr Sa

Su

Mo Tu We Th

Fr Sa

Su

Mo Tu

We Th Fr Sa0

1

2

3

4

1

10

5

6

7

8

91011

2

3

4

5

6

7

8

2

3

4

5

6

7

80

12

13

14

15

16

17

18

9

10 11

12 13

14

15

9

10

11

12 13

14 150

19

20

21

22

23

24

25

16

17 18

19 20

21

22

16

17

18

19 20

21 220

26

27

28

29

30

31

23

24 25

26 27

28

23

24

25

26 27

28 290

30

31

April

May

June0

Su

Mo Tu

We Th

Fr Sa

Su

Mo Tu We Th

Fr Sa

Su

Mo Tu

We Th Fr Sa0

1

2

3

4

5

1

2

3

1

2

3

4

5

6

70

6

7

8

9

10

11

12

4

5

6

7

8

9

10

8

9

10

11 12

13 140

13

14

15

16

17

18

19

11

12 13

14 15

16

17

15

16

17

18 19

20 210

20

21

22

23

24

25

26

18

19 20

21 22

23

24

22

23

24

25 26

27 280

27

28

29

30

25

26 27

28 29

30

31

29

30

July

August

September0

Su

Mo Tu

We Th

Fr Sa

Su

Mo Tu We Th

Fr Sa

Su

Mo Tu

We Th Fr Sa0

1

2

3

4

5

1

2

1

2

3

4

5

60

6

7

8

9

10

11

12

3

4

5

6

7

8

9

7

8

9

10 11

12 130

13

14

15

16

17

18

19

10

11 12

13 14

15

16

14

15

16

17 18

19 200

20

21

22

23

24

25

26

17

18 19

20 21

22

23

21

22

23

24 25

26 270

27

28

29

30

31

24

25 26

27 28

29

30

28

29

300

310

October

November

December0

Su

Mo Tu

We Th

Fr Sa

Su

Mo Tu We Th

Fr Sa

Su

Mo Tu

We Th Fr Sa0

1

2

3

4

1

1

2

3

4

5

60

5

6

7

8

9

10

11

2

3

4

5

6

7

8

7

8

9

10 11

12 130

12

13

14

15

16

17

18

9

10 11

12 13

14

15

14

15

16

17 18

19 200

19

20

21

22

23

24

25

16

17 18

19 20

21

22

21

22

23

24 25

26 270

26

27

28

29

30

31

23

24 25

26 27

28

29

28

29

30

310

30

Bonus Task 2 - Print calendar of adjacent 3 months

This is a bonus task, please see grading details in0Grading section

https://course.cse.ust.hk/comp2012h/assignments/PA1/Description/ 7/10

11/25/22, 9:46 PM COMP 2012H Assignment 1: Calendar

When user chooses to print the calendar of adjacent 3 months0(last month, current month, and next month), indicating by0first enter a valid year, and then enter -3 as month.0Then, the program will ask which month do the user want to print as current month0(note that we only have this prompt in this task).0After that, we will start each week on Sunday and print the calendar horizontally.

Note: Since we only process year 1800-9999, so if user want0to print adjacent 3 months, with Jan 1800 or Dec 9999 as current month,0we will consider the case invalid, and your program should ask user to input again.

You should print the three months in the same row. Two adjacent months0in the same row should be separated by two spaces. For each month,

A sub-title should be displayed, with format "[Month Name] [Year]", for example, "August 2022". To make it easier, please make it left-aligned.

Display the names of weekdays, from "Su" to "Sa", words should be0separated by one space.

Finally, format the calendar like the example below. Dates should be0separated by one space, and for dates with only one digit, the digit0should be right-aligned.

After printing the calendar, the program finishes.

It's ok to have extra spaces at the end of each line.0See details in Grading section

Note: This should be the same as Linux command0cal -3 -m [center month] [year], except that you don't need to0align the sub-titles or ouput additional blank line at the end.

Example: (user input is displayed in red)

Enter

year (in

range 1800-9999): 18000

Enter

month (in range

1-12): -30

Enter the month in the center (in range

1-12):

20

January 1800

February

1800

March

18000

Su

Mo Tu

We Th

Fr Sa

Su Mo Tu

We Th Fr

Sa

Su

Mo Tu

We Th Fr Sa0

1

2

3

4

1

10

5

6

7

8

91011

2

3

4

5

6

7

8

2

3

4

5

6

7

80

12

13

14

15

16

17

18

9

10 11

12 13

14

15

9

10

11

12

13

14 150

19

20

21

22

23

24

25

16

17 18

19 20

21

22

16

17

18

19

20

21 220

26

27

28

29

30

31

23

24 25

26 27

28

23

24

25

26

27

28 290

30

31

End of Tasks

Resources & Sample I/O

Skeleton code: No skeleton for this PA, you are expected to start from scratch.

Demo programs: Windows /0MacOS Intel /0Linux (compiled on CS Lab 2 Machine)

Revealed test cases:0Download here.0They are the same as examples in Tasks section. Please note that test cases on ZINC before deadline are the same as those examples.0 Additional note: You may notice that the format of test cases downloaded0is not the same as the format you see in your terminal. This is because when we test your program, we will let your program read input from a file and output to another file.0Please refer to this section0about how you can test your program by yourself.

Sample 1: horizontal, start weeks on Sunday

Sample 2: horizontal, start weeks on Monday

Sample 3: vertical, start weeks on Sunday

Sample 4: vertical, start weeks on Sunday

Sample 5: Bonus 1: print whole year

Sample 6: Bonus 2: print adjacent 3 months

End of Resources & Sample I/O

https://course.cse.ust.hk/comp2012h/assignments/PA1/Description/ 8/10

11/25/22, 9:46 PM COMP 2012H Assignment 1: Calendar

Submission & Grading

Deadline: Sat, 17/9/22 HKT 23:59.0

Best 10 out of 11 PAs will be counted towards your course grade, each with 6.5% at most via Automated Grading on the0ZINC Online Submission System.0Compress the single source code file main.cpp by itself as PA1.zip0for submission to ZINC.

Grading Scheme

The grading process will be performed entirely on0ZINC Online Submission System.0We will input some contents to your program, and0simply compare its output with expected output. To avoid being too demanding, ZINC will consider the results are identical0as long as they "look exactly the same". To be more specific,0trailing spaces at the end of each line and a newline character0at the end of the file will be ignored.

For example, if two lines are0"1 2 3 4" and "1 2 3 4 ",0they will be considered identical.

However,0if two lines are "1 2" and "1 2",0they are not the same, since we only0ignore

spaces at the end of each line/the file.

Before the deadline, the test cases on ZINC will only contain a few cases. If you passed0those tests, it only means your program can successfully run on ZINC and can pass0those test cases. The result is by no means complete and the score is irrelavant to0your actual score.0In the actual grading stage after the deadline, we will use a totally different set of0test cases, which is expected to be more complete and more strict,0to test the correctness of your program.

Please ensure that you submit to ZINC well before the deadline as all late submissions will be automatically rejected.

Note: When testing Task 1-5, we guarantee that there won't be input where month is -1 or -3.

So even if you complete Bonus part, your grade in Task 1-5 will not be affected.

How to test my program

You may compile your code using this command:0(the -Wall flag is optional, it will show all warnings to you)

g++ -o main main.cpp -std=c++11 -Wall

Next, please place main, input.txt, output.txt0(i.e., the sample input/output that we provided)0in the same folder, and0let your program read input from file input.txt and output result0to file my_output.txt by using this command:0(you can change the filename, e.g. input3.txt and output3.txt, etc.)

./main < input.txt > my_output.txt

If you are using Windows PowerShell, you may use the following command instead:

Get-Content input.txt | ./main > my_output.txt

Then you may check if your output in0my_output.txt is the same as the expected output

output.txt0by using this online diff checker tool

https://course.cse.ust.hk/comp2012h/assignments/PA1/Description/ 9/10

11/25/22, 9:46 PM COMP 2012H Assignment 1: Calendar

By default it will not ignore trailing spaces and blank lines, you need to0click "Tools" at the top-right, then "Trim whitespace", as the image shows.

Alternative: If you are using a Linux machine (MacOS is not ok) or using CS Lab 2 machines, you can directly0use this command:

diff -Z -B output.txt my_output.txt

This command will automatically ignore trailing spaces and blank lines.0If you see nothing after executing this command, then the contents in these two files0is considered the same.

End of Submission & Grading

Frequently Asked Questions

Q: My code doesn't work, there is an error/bug, here is the code, can you help me fix it?0

A: As the assignment is a major course assessment, to be fair, you are supposed to work on it on your own and we should not finish the tasks for you. We are happy to help with explanations and0advice, but we shall not directly debug the code for you.

Q: The demo program enters an infinite loop when given unexpected input (e.g. inputting a character when expecting an integer). Is this a bug? 0

A: This is just the behavior of cin >> variable; when given input that is0not type-matched. You don't need to worry about such for PA1, you can assume the user always0input the correct data type. For example, if we want an int, the user will always0input an int, not other weird things. However, the user input can still be0invalid, for example, you want an integer between 1800-9999, and the user may input 1700, or012345, etc

Q: What are the restrictions regarding modifying the header files, writing our own helper functions, including extra header files, etc.? 0

A: The only hard restriction is that you can only submit main.cpp to ZINC, and0include only one header file #include <iostream>. Anything else0are not strictly prohibited, as long as your code can run on ZINC.

End of FAQ

Acknowledgement - The assignment 1 is drafted by LIU Jianmeng.

Maintained by COMP 2012H Teaching Team © 2022 HKUST Computer Science0and Engineering

LIU, Jianmeng

https://course.cse.ust.hk/comp2012h/assignments/PA1/Description/ 10/10

More products