answersLogoWhite

0


Best Answer

You don't need an array to print a calendar. The following program shows how to print a calendar for any month from January 1900 onwards:

#include<iostream>

#include<iomanip>

#include<string>

#include<sstream>

using std::cout;

using std::cin;

using std::endl;

using std::setw;

using std::string;

using std::stringstream;

bool is_leap_year (size_t year)

{

if (year%4) return false;

if (year%100) return true;

if (year%400) return false;

return true;

}

size_t days_in_month (size_t month, size_t year)

{

switch (month)

{

case 4: case 9: case 6: case 11: return 30;

case 1: case 3: case 5: case 7: case 8: case 10: case 12: return 31;

case 2: return is_leap_year (year) ? 28 : 29;

}

return 0; // error!

}

size_t day_of_year (size_t day, size_t month, size_t year)

{

size_t days = day;

while (--month)

days += days_in_month (month, year);

return days - 1;

}

size_t days_in_previous_years (size_t year)

{

size_t days = 0;

while (1900 < --year)

days += is_leap_year (year) ? 366 : 365;

return days;

};

size_t days_since_epoch (size_t day, size_t month, size_t year)

{

return days_in_previous_years(year) + day_of_year (day, month, year);

}

size_t day_of_week (size_t day, size_t month, size_t year)

{

return days_since_epoch (day, month, year) % 7 + 1;

}

void print_calendar (size_t month, size_t year)

{

switch (month)

{

case 1: cout << "January"; break;

case 2: cout << "February"; break;

case 3: cout << "March"; break;

case 4: cout << "April"; break;

case 5: cout << "May"; break;

case 6: cout << "June"; break;

case 7: cout << "July"; break;

case 8: cout << "August"; break;

case 9: cout << "September"; break;

case 10: cout << "October"; break;

case 11: cout << "November"; break;

case 12: cout << "December"; break;

default: return;

}

cout << ' ' << year << endl;

cout << "\n Su Mo Tu We Th Fr Sa\n";

size_t start_day = day_of_week (1, month, year);

size_t days = days_in_month (month, year);

size_t count = start_day % 7;

for (size_t i = 1; i <= count; i++)

cout << setw (4) << ' ';

for (size_t i = 1; i <= days; ++i)

{

cout << setw (4) << i;

++count;

if (count == 7)

{

cout << endl;

count = 0;

}

}

if (count)

cout << endl;

cout << endl;

}

size_t ask (string prompt)

{

for (;;)

{

cout << prompt;

string input;

cin >> input;

stringstream ss;

ss << input;

size_t reply;

if (ss >> reply)

return reply;

cout << "Invalid input.\n";

}

}

int main (void)

{

for (;;)

{

enter_month:

size_t month = ask ("Enter a month (1 - 12, 0 to exit): ");

if (!month)

break;

if (month > 12)

{

cout << "Invalid month." << endl;

goto enter_month;

}

enter_year:

size_t year = ask ("Enter a year (>= 1900, 0 to exit): ");

if (!year)

break;

if (year < 1900)

{

cout << "Invalid year." << endl;

goto enter_year;

}

print_calendar (month, year);

}

}

User Avatar

Wiki User

9y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Is the calendar in c plus plus programming array use?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What is use of keyword extra in c plus plus programming?

Nothing.


How do you use array in template for two different data types in c plus plus programming language?

You simply instantiate the array for each type. That's the beauty of template (or generic) programming. vector&lt;int&gt; myIntArray; vector&lt;double&gt; myDoubleArray; vector&lt;myClass&gt; myClassArray; myDoubleArray.push_back(3.5); myDoubleArray.push_back(6.7); myDoubleArray.push_back(3.14159); The myDoubleArray (actually, it called a vector, which is the container type most closely like an array) now contains 3.5, 6.7, 3.14159, and you can easily use them naively, as in myDoubleArray[1] is equal to 6.7, etc. Of course, the power of STL containers is that you can use various algorithms on them, and you can create iterators to more easily traverse them, but this is enough to answer the question.


How do you store a string of letters for example abcdeabcde each to an array?

In C programming you would use the following: char a[] = "abcdeabcde"; If you wish to create an array with more than one string, use an array of character pointers instead: char* a[] = {"abcde", "fgh", "ijklm", "nopq", "rstu", "vwxyz"};


C plus plus array-based lists?

If you mean an array where each element is a list, then the STL is your friend. To create an array of lists of any type T, use the following declaration: std::vector&lt;std::list&lt;T&gt;&gt; my_array_of_lists;


How do you convert numeric value into alpha value in c plus plus programming language?

use the _itoa function

Related questions

What is the use of c plus plus in banks?

Programming language.


How do you use character in c plus plus?

If you are referring to the character object 'char,' then here are a couple of uses:To create an object, use this:char object = 'a';To create an array of chars, use this:char array[10];To dynamically allocate an array of chars, use this:char array = new char[10];(Don't forget to delete the object with 'delete [] array')


What is the definition of array length in the Java programming language?

When programming in Java language, you will need to use container objects to hold your specific values and these are called Arrays. The amount of values held in these containers is called the Array Length. This is generated once an Array is created and the length becomes locked.


What is use of keyword extra in c plus plus programming?

Nothing.


What does it mean when use percent 17 or mod any number in Array in C programming?

I mean %17


How do you use array in template for two different data types in c plus plus programming language?

You simply instantiate the array for each type. That's the beauty of template (or generic) programming. vector&lt;int&gt; myIntArray; vector&lt;double&gt; myDoubleArray; vector&lt;myClass&gt; myClassArray; myDoubleArray.push_back(3.5); myDoubleArray.push_back(6.7); myDoubleArray.push_back(3.14159); The myDoubleArray (actually, it called a vector, which is the container type most closely like an array) now contains 3.5, 6.7, 3.14159, and you can easily use them naively, as in myDoubleArray[1] is equal to 6.7, etc. Of course, the power of STL containers is that you can use various algorithms on them, and you can create iterators to more easily traverse them, but this is enough to answer the question.


How do you store a string of letters for example abcdeabcde each to an array?

In C programming you would use the following: char a[] = "abcdeabcde"; If you wish to create an array with more than one string, use an array of character pointers instead: char* a[] = {"abcde", "fgh", "ijklm", "nopq", "rstu", "vwxyz"};


C plus plus array-based lists?

If you mean an array where each element is a list, then the STL is your friend. To create an array of lists of any type T, use the following declaration: std::vector&lt;std::list&lt;T&gt;&gt; my_array_of_lists;


How you use negative index in array?

You cannot do this easily in C programming. Arrays in C always start with index 0. If you must use a negative array index, you can do that by allocating an array, and then pointing to an element within the array. Say you allocate an array 10 ints long, named a; then set b = &amp;a[5]. Then b[-4] = a[1]. However, this is extremely bad programming practice and is almost certain to cause data corruption. Some compilers will treat array indices as unsigned, also, so that when you specify b[-4], the compiler will internally simplify that to b[65532] and your program will crash.


How do you write a program that accepts 50 integer values and sort them in basic programming?

Create an array with 50 elements and input the integers one a time, filling the array. Use an insertion sort on the array for each input except the first. Alternatively, input the values first and then use insertion sort.


How do you convert numeric value into alpha value in c plus plus programming language?

use the _itoa function


Why you use array in c sharp programming?

Any feature in any computer language should be based on the need. In the last 2 years, I have not come across any need to use an array in my job. Yes, I do use collections, a lot of them, just never a fixed-size collection.