Question

In: Computer Science

Write a C++ program that prints a calendar for a given year. ONLY USING "#include<iostream>" and...

Write a C++ program that prints a calendar for a given year.

ONLY USING "#include<iostream>" and "#include<cmath>" The program prompts the user for two inputs:

      1) The year for which you are generating the calendar.
      2) The day of the week that January first is on, you will use the following notation to set the day of the week:

      0 Sunday                     1 Monday                   2 Tuesday                   3 Wednesday
      4 Thursday                 5 Friday                      6 Saturday

Your program should generate a calendar similar to the one shown in the example output below. The calendar should be printed on the screen. Your program should be able to handle leap years. A leap year is a year in which we have 466 days. That extra day comes at the end of February. Thus, a leap year has 466 days with 29 days in February. A century year is a leap year if it is divisible by 400. Other years divisible by 4 but not by 100 are also leap years.

Example: Year 2000 is a leap year because it is divisible by 400.  Year 2004 is a leap year because it is divisible by 4 but not by 100.

Your program should clearly describe the functionality of each function and should display the instructions on how to run the program.

Sample Input:

Enter the year for which you wish to generate the calendar: 2018
Enter the day of the week that January first is on: 1

Sample output:

Calendar for year 2018

January
Sun      Mon     Tue      Wed     Thu      Fri        Sat
            1          2          4           4          5          6         

7          8          9          10        11        12        14       

14       15        16        17         18        19        20       

21       22        24        24         25        26        27       

28       29        40        41

February
Sun      Mon     Tue      Wed     Thu      Fri        Sat
                                                1          2          4         

4          5          6          7          8          9          10       

11        12        14        ..         ..          ..          ..         

..          ..          ..          ..          ..          ..          ..

..

..

..

Solutions

Expert Solution

code:

#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
using namespace std;

string getMonthName(int monthNumber)
{
   string months[] = {"January", "February", "March",
                   "April", "May", "June",
                   "July", "August", "September",
                   "October", "November", "December"
                   };

   return (months[monthNumber]);
}

int numberOfDays (int monthNumber, int year)
{
   // January
   if (monthNumber == 0)
       return (31);

   // February
   if (monthNumber == 1)
   {
       // If the year is leap then February has
       // 29 days
       if (year % 400 == 0 ||
               (year % 4 == 0 && year % 100 != 0))
           return (29);
       else
           return (28);
   }

   // March
   if (monthNumber == 2)
       return (31);

   // April
   if (monthNumber == 3)
       return (30);

   // May
   if (monthNumber == 4)
       return (31);

   // June
   if (monthNumber == 5)
       return (30);

   // July
   if (monthNumber == 6)
       return (31);

   // August
   if (monthNumber == 7)
       return (31);

   // September
   if (monthNumber == 8)
       return (30);

   // October
   if (monthNumber == 9)
       return (31);

   // November
   if (monthNumber == 10)
       return (30);

   // December
   if (monthNumber == 11)
       return (31);
}

// Function to print the calendar
void printCalendar(int year,int d)
{
   printf ("       Calendar - %d\n\n", year);
   int days;

   // Index of the day from 0 to 6
   int current = d;
   for (int i = 0; i < 12; i++)
   {
       days = numberOfDays (i, year);

       // Print the current month name
       printf("\n ------------%s-------------\n",
           getMonthName (i).c_str());

       // Print the columns
       printf(" Sun\tMon\tTue\tWed\tThu\tFri\tSat\n");

       // printing spaces for days
       int k;
       for (k = 0; k < current; k++)
           printf("\t");

       for (int j = 1; j <= days; j++)
       {
           printf("%5d\t", j);

           if (++k >6)
           {
               k = 0;
               printf("\n");
           }
       }

       if (k)
           printf("\n");

       current = k;
   }

   return;
}

// Driver Program to print calendar
int main()
{
   int year,b;
   cout<<"enter year:";
   cin>>year;
   cout<<"enter day of january first in numbers:";
   cin>>b;
   cout<<"\n";
   printCalendar(year,b);

   return (0);
}
/*
Index   Day
0       Sunday
1       Monday
2       Tuesday
3       Wednesday
4       Thursday
5       Friday
6       Saturday*/

/*
Month Number   Name
0           January
1           February
2           March
3           April
4           May
5           June
6           July
7           August
8           September
9           October
10           November
11           December */


output:

code screenshot:


Related Solutions

Writing a squareroot program in C++ using only: #include <iostream> using namespace std; The program must...
Writing a squareroot program in C++ using only: #include <iostream> using namespace std; The program must be very basic. Please don't use math sqrt. Assume that the user does not input anything less than 0. For example: the integer square root of 16 is 4 because 4 squared is 16. The integer square root of 18 is 5 because 4 squared is 16 and 5 squared is 25, so 18 is bigger than 16 but less than 25.  
Write a menu driven C++ program that prints the day number of the year , given...
Write a menu driven C++ program that prints the day number of the year , given the date in the form of month-day-year. For example , if the input is 1-1-2006 , then the day number is 1. If the input is 12-25- 2006 , the day number is 359. The program should check for a leap year. A year is leap if it is divisible by 4 but not divisible by 100. For example , 1992 , and 2008...
C++ CODE ONLY Using the following code. #include <iostream> #include <string> #include <climits> #include <algorithm> using...
C++ CODE ONLY Using the following code. #include <iostream> #include <string> #include <climits> #include <algorithm> using namespace std; // M x N matrix #define M 5 #define N 5 // Naive recursive function to find the minimum cost to reach // cell (m, n) from cell (0, 0) int findMinCost(int cost[M][N], int m, int n) {    // base case    if (n == 0 || m == 0)        return INT_MAX;    // if we're at first cell...
write a program on c++ that outputs a calendar for a given month in a given...
write a program on c++ that outputs a calendar for a given month in a given year, given the day of the week on which the 1st of the month was. The information in numeric format (months are: 1=Januay, 2=February 3=March, 4= April, 5= May, 6= June, 7= July... etc days are 1=Sunday, 2=Monday, 3= Tuesday, 4= Wednesday, 5= Thursday, 6= Friday and 7= Saturday ). The program output that month’s calendar, followed by a sentence indicating on which day...
A C++ question: I want to indent the code of this C++ program: #include<iostream> #include<cstring> using...
A C++ question: I want to indent the code of this C++ program: #include<iostream> #include<cstring> using namespace std; int lastIndexOf(char *s, char target) { int n=strlen(s); for(int i=n-1;i>=0;i--) { if(s[i]==target) { return i; } } return -1; } void reverse(char *s) { int n=strlen(s); int i=0,j=n-1; while(i<=j) { char temp=s[i]; s[i]=s[j]; s[j]=temp; i++; j--; } return; } int replace(char *s, char target, char replacementChar) { int len=strlen(s); int total=0; for(int i=0;i<len;i++) { if(s[i]==target) { s[i]=replacementChar; total+=1; } } return total;...
only using C (not C++) Implement a program that counts the words and prints their frequencies....
only using C (not C++) Implement a program that counts the words and prints their frequencies. In particular, the program extracts “words” from one or more text files, from a pipe, or from the console, and prints to the console the list of words found and their associated frequencies. Note that your project submission is restricted to only using the following system calls: open(), close(), read(), write(), and lseek() for performing I/O. You are allowed to use other C library...
Please write variables and program plan(pseudocode) of this C++ programming code: #include <iostream> using namespace std;...
Please write variables and program plan(pseudocode) of this C++ programming code: #include <iostream> using namespace std; void leapYear(int x); int main() { int x; cout << "Enter a year: "; cin >> x; leapYear (x);   return 0; } void leapYear(int x ) {    if (x % 400 == 0)    {    cout << "This is a leap Year";}    else if    ((x % 4 == 0) && (x % 100 != 0))    {    cout <<...
In C++ Please, using only the libraries given in this homework prompt, Write a program that...
In C++ Please, using only the libraries given in this homework prompt, Write a program that (1) prompts for two integers, (2) prints out their sum, (3) prints out the first divided by the second, and (4) prints out the natural log of the first number raised to the power of the second number. #include <iostream> #include <iomanip> using namespace std; int main() { <your answer goes here> return 0; }
(C++) Write a program that prints a table in the following format given a "x" read...
(C++) Write a program that prints a table in the following format given a "x" read from the keyboard. For example, if x is 4, it prints out 0 0 1 1 2 4 3 9 4 16 To avoid mismatch, put 8 white spaces between two numbers.
Using the pseudocode found below, write only the actual (C++) code for this program. Include all...
Using the pseudocode found below, write only the actual (C++) code for this program. Include all libraries. Specification: Write a program that will repeatedly ask the user for quiz grades in the range from 0 to 20. Any negative value will act as a sentinel. When the sentinel is entered compute the average of the grades entered. Design: Constants None Variables float grade float total = 0 int count = 0 float average ------- Inital Algorithm Repeatedly ask user for...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT