Hello to those reading. My assignment is as follows:
"Your job is to write a program that will display a calendar for any given month of a given year. The user will need to type the number of the month as in integer from 1 to 12 (1 is for January, etc.), and the year as a 4-digit integer."
My main issue is determining what day of the week the month the user desires to display starts. I was given an equation, D (0 = Sunday, 1 = Monday, 2 = Tuesday, .... 6 = Saturday) which is supposed to give values 0-6 which correspond to days in the week, in which the value of D should be the day of the week the month starts on.
Here is the following equation (please excuse me if it is hard to read at all, please tell me if it is and I will try to make it neater):
D = ([(26 * (M + 1)) / 10] + Y + [Y / 4] + [C / 4] + 5 * C) mod 7
Where
M = the month (3 = March; 4 = April; 5 = May; 6 = June; 7 = July; 8 = August; 9 = September; 10 = October; 11 = November; 12 = December; 13 = January; 14 = February)
C is the "century" (more accurately, the first 2 digits of the user input for year - e.g. if year = 2014, C = 20)
Y is the year within the century (last 2 digits of the user input for year - e.g. if year = 2014, Y = 14)
Square brackets mean that the value inside should be truncate (not rounded), so I suppose simply converting the value to an int would do - e.g. [2.8] -> 2
Below is the bulk of my CURRENT code (it will not be representative of the end goal, which is to display a calendar for a desired month/year). This code's main purpose right now is to display "D". If I can figure this out, I think the rest of the problem will be relatively easy.
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 32 33 34 35 36 37 38 39 40 41 42
|
#include <iostream>
#include <iomanip>
void variablesForD(int, int, int, int, int);
int D(int, int, int);
using namespace std;
int main() {
int year;
int monthNumber;
cout << "Please enter a month as an integer (3 = March; 4 = April; 5 = May; 6 = June; 7 = July; 8 = August; 9 = September; 10 = October; 11 = November; 12 = December; 13 = Janruary; 14 = February): ";
cin >> monthNumber;
cout << "Please enter a year (as a 4-digit integer): ";
cin >> year;
cout << setw(15) << D << endl;
system("pause");
return 0;
}
void variablesForD(int m, int y, int c, int monthNumber, int year) {
m = monthNumber;
c = (year - y) / 100;
y = year - 100 * c;
}
int D(int m, int y, int c) {
int D;
D = (((26 * (m + 1)) / 10) + y + (y / 4) + (c / 4) + (5 * c)) % 7;
return D;
}
|
Basic plug-and-chug aside, to determine variables C and Y, I noticed year = (100 * C) + Y, so I did basic algebraic manipulation and set the equation to solve for C and Y respectively in the "variablesForD" function. I couldn't notice any syntax errors or anything wrong with the equation for D, but my output is rather weird. I noticed that the output has letters in it, though my ineptitude for programming means I have no idea how to interpret that. Here is an example of the code's output (using the case of March 2014) :
1 2 3 4
|
Please enter a month as an integer (3 = March; 4 = April; 5 = May; 6 = June; 7 = July; 8 = August; 9 = September; 10 = October; 11 = November; 12 = December; 13 = Janruary; 14 = February): 3
Please enter a year (as a 4-digit integer): 2014
0099140B
Press any key to continue . . .
|
I can't figure this out on my own (due to my aforementioned ineptitude in programming). May someone give me some insight on how to fix this, or directions for where to look? Thank you in advance for reading this!