I need to write a C++ program that can be used to calculate the date of any Easter Sunday which can be computed as follows;

Let X be the year for which it is desired to compute Easter Sunday.
Let A be the remainder of the division of X by 19.
Let B be the remainder of the division of X by 4.
Let C be the remainder of the division of X by 7.
Let D be the remainder of the division of (19*A+24) by 30.
Let E be the remainder of the division of (2*B+4*C+6*D+5) by 7.

Easter Sunday = March 22+D+E. (Note-This can give a date in April)

The program should prompt the user for a year and then calculates and outputs (properly labeled), the correct month, day and year for that year’s Easter Sunday.

I'm understanding the subject a little bit. I think there it should include 'if' and 'else' statements.

See my code I have thus far: I get an error message that says that 'x' has not been assigned a value...But 'x' is what I want to personally enter as 'cin'- the year.

Thanks.
--------------------------------------------------------------------------------

#include "stdafx.h"
#include <iostream>
#include <string>
#include <cmath>

using namespace std;

int main()
{
	
int x;//The year for which it is desired to compute Easter Sunday.
int a = x % 19;//The remainder of the division of x by 19.
int b = x % 4;//The remainder of the division of x by 4.
int c = x % 7;//The remainder of the division of x by 7.
int d = (19 * a) + (24) % 30;//The remainder of the division of (19*a + 24) by 30.
int e = (2 * b) + (4 * c) + (6 * d) + (5) % 30;//The remainder of the division of (2*b+4*c+6*d+5) by 7.
int sun = (22 + d + e);//The calculation of easter sunday.
cout<< "Enter the year you wish to inquire about"<<endl;
cout<<x<<endl;
cin>> x;//The 4-digit year.
cout<< sun<<endl;
return 0;

You are trying to output the value of x before actually getting the value. IE. You are using cout<<x<< endl; before cin>> x;

Ok...thx for your help-now I see it. But where would and 'if' else statement be necessary in such a program? I'm in my first month of C++ and I'm struggling to catch on to it. I really appreaciate all the assistance. I had tried and example- i had inputted '1999'- and i got a result of '749'. I know it doesn't make any sense, but It's suppose to calculate and show the day, month and year. Thx.

That error message means that when line line 12 is executed variable x has not been assigned a value yet so it just contains some random value. Programs are normally executed in the same order that you code them, so line 12 is executed immediately after line 11, and line 13 after line 12, etc.

To correct the error you received you need to move lines 18, 19 and 20 up between line 11 and 12 so that the value of x is entered at the keyboard before attemtping to execute lines 12 thru 16.

Ok...thx for your help-now I see it. But where would and 'if' else statement be necessary in such a program? I'm in my first month of C++ and I'm struggling to catch on to it. I really appreaciate all the assistance. I had tried and example- i had inputted '1999'- and i got a result of '749'. I know it doesn't make any sense, but It's suppose to calculate and show the day, month and year. Thx.

Well firstly you are trying to do math with x before it actually has a variable so once it gets the others (a, b, c, d, e, sun) aren't actually giving the correct number

You want to run that by me again? If i enter a value from the keyboard-doesn't a program automatically do the calculation? You kinna lost me there.

Also, where do the 'if' and 'else' statements come into play with such a program?

You want to run that by me again? If i enter a value from the keyboard-doesn't a program automatically do the calculation? You kinna lost me there.

Yes it does but you are trying to do the calculations before you have gotten any input. It is trying to do the math on a number that doesn't exist.

#include "stdafx.h"
#include <iostream>
#include <string>
#include <cmath>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <iomanip>


using namespace std;


int main()
{
cout<< "Enter the year you wish to inquire about";


int x;//The year for which it is desired to compute Easter Sunday.
cin>> x;//The 4-digit year.
int a = x % 19;//The remainder of the division of x by 19.
int b = x % 4;//The remainder of the division of x by 4.
int c = x % 7;//The remainder of the division of x by 7.
int d = (19 * a + 24) % 30;//The remainder of the division of (19*a + 24) by 30.
int e = (2 * b + 4 * c + 6 * d + 5) % 30;//The remainder of the division of (2*b+4*c+6*d+5) by 7.
int sun = (22 + d + e);//The calculation of easter sunday.
cout<<setw(4)<< "Day" <<setw(8)<< "Month" <<setw(8)<< "Year"<<endl;
cout<<setw(20)x<<endl;


return 0;
}

But doesn't my position of 'cin' allow for that value to be calculated? I was able to get the year...and how does this program require a 'if' and 'else' statements?

Edited 3 Years Ago by happygeek: fixed formatting

Also, where do the 'if' and 'else' statements come into play with such a program?

The variable sun in that equation is the julian day (1=Jan1, 365=Dec 31) for Easter Sunday. Now you need to add code that will calculate the month, day, and year. The if statement comes in when determining whether the year is a leap-year or not. If its a leap year then February will have 29 days otherwise it will have 28 days.

You should create an array of 12 integers that contains the cumulative number of days from 1 January to the 1st day of the months. For example, Jan = 0, Feb = 31, Mar = 31+28 = 59, April = 59+31 = 90, etc. Then use the sun to determine which of the 12 months contains Easter. If sun = 95 then Easter is in the 3d month (counting at 0 for Jan).

#include "stdafx.h"
#include <iostream>
#include <string>
#include <cmath>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <iomanip>


using namespace std;


int main()
{
cout<< "Enter the year you wish to inquire about";
int x;//The year for which it is desired to compute Easter Sunday.
cin>> x;//The 4-digit year.
if (x<0001 || x>9999);
cout<<"The year must be between 0001 and 9999"<<endl;
int a = x % 19;//The remainder of the division of x by 19.
int b = x % 4;//The remainder of the division of x by 4.
int c = x % 7;//The remainder of the division of x by 7.
int d = (19 * a + 24) % 30;//The remainder of the division of (19*a + 24) by 30.
int e = (2 * b + 4 * c + 6 * d + 5) % 30;//The remainder of the division of (2*b+4*c+6*d+5) by 7.
int sun = (22 + d + e);//The calculation of easter sunday.
cout<<setw(4)<< "Day" <<setw(8)<< "Month" <<setw(8)<< "Year"<<endl;
cout<<setw(20)<<x<<endl;


return 0;

Well i'm going to see if I can read up on arrays-we are in out first month of class, so i don't know what arrays are....this project is due on monday, so i'm going to need all the assistance that i can get. Thx.

Edited 3 Years Ago by happygeek: fixed formatting

Even though for 'x', i inputted a value between 0001 and 9999-why does it still show the 'cout' statement that I have there?

#include "stdafx.h"

if (x<0001 || x>9999);

Well, you put the if statement in there but you didn't really do anything. Look up if-check syntax in your book it should be:

if (var relation value)
{
     statement;
}

Example:

if (userInput == 93)
{
     cout<<"You entered "<<userInput<< endl;
}

That example is completely useless but it's good enough to show syntax. Though a while loop would probably be better for input validation since it would only show the message once then happily let you input an incorrect value.

Ok..got it-it works now: So far I have:

#include "stdafx.h"
#include <iostream>
#include <string>
#include <cmath>
#include <time.h>
#include <stdio.h>
#include <iomanip>


using namespace std;


int main()
{


cout<< "Enter the year you wish to inquire about";
int x;//The year for which it is desired to compute Easter Sunday.
cin>> x;//The 4-digit year.
if (x<0001 || x>9999)
cout<<"The year has to be between 0001 & 9999"<<endl;
int a = x % 19;//The remainder of the division of x by 19.
int b = x % 4;//The remainder of the division of x by 4.
int c = x % 7;//The remainder of the division of x by 7.
int d = (19 * a + 24) % 30;//The remainder of the division of (19*a + 24) by 30.
int e = (2 * b + 4 * c + 6 * d + 5) % 30;//The remainder of the division of (2*b+4*c+6*d+5) by 7.
int sunday = (22 + d + e);//The calculation of easter sunday.
int month = (sunday/31);
cout<<setw(4)<< "Day" <<setw(8)<< "Month" <<setw(8)<< "Year"<<endl;
cout<<setw(4)<<sunday<<setw(6)<<month<<setw(10)<<x<<endl;

I'm still playing around with it...Thx.

Edited 3 Years Ago by happygeek: fixed formatting

The calculation for month is incorrect. You can't just simply divide by 31 because not all months contain 31 days. If you do not know about arrays yet then you can get the month with a series of if/else statements, something like this:

if( sunday > 334)
{
    month = 12; // December
}
else if( sunday > 304)
{
    month = 11; // November
}
<snip>
...

I normally have to make myself a chart that looks like below. The first column is the number of days in each month and the second column is the number of days from 1 Jan to the beginning of the month.

31 0	// Jan
28 31	// Feb
31 59	// Mar
30 90	// Apr
31 120	// May
30 151	// Jun
31 181	// Jul
31 212	// Aug
31 243	// Sep
31 274	// Oct
30 305	// Nov
31 335	// Dec
#include "stdafx.h"
#include <iostream>
#include <string>
#include <cmath>
#include <time.h>
#include <stdio.h>
#include <iomanip>

using namespace std;

int main()
{	
	
	cout<< "Enter the year you wish to inquire about";		
	int x;//The year for which it is desired to compute Easter Sunday.
	int month;//Month of easter sunday.
	cin>> x;//The 4-digit year.	
	if (x<0001 || x>9999)
		cout<<"The year has to be between 0001 & 9999"<<endl;
	int a = x % 19;//The remainder of the division of x by 19.
	int b = x % 4;//The remainder of the division of x by 4.
	int c = x % 7;//The remainder of the division of x by 7.
	int d = (19 * a + 24) % 30;//The remainder of the division of (19*a + 24) by 30.
	int e = (2 * b + 4 * c + 6 * d + 5) % 30;//The remainder of the division of (2*b+4*c+6*d+5) by 7.
	int sunday = (22 + d + e);//The calculation of easter sunday.
		
	if( sunday > 334)
{
    month = 12; // Dec
}
else if( sunday > 304)
{
    month = 11; // Nov
}
else if (sunday > 273)
{
	month = 10; //Oct
}
else if( sunday > 242 )
{
    month = 9; // Sep
}
else if (sunday > 211)
{
	month = 8; //Aug
}
else if( sunday > 180)
{
    month = 7; // Jul
}
else if (sunday > 150)
{
	month = 6; //Jun
}
else if( sunday > 119)
{
    month = 5; // May
}
else if (sunday > 89)
{
	month = 4; //Apr
}
else if( sunday > 58)
{
    month = 3; // Mar
}
else if (sunday > 29)
{
	month = 2; //Feb
}
else if (sunday > 0)
{
	month = 1; //Jan
}

	cout<<setw(4)<< "Day" <<setw(8)<< "Month" <<setw(8)<< "Year"<<endl;
	cout<<setw(4)<<sunday<<setw(6)<<month<<setw(10)<<x<<endl;
	
	
	return 0;

---------------------------------------------------------------------------------
Ok thx for the help...this is where i've reached thus far and i'm almost done.....the program compiles ok-however for some reason for the month- i still get a value of 47=it does print the month of 2-but why isn't the actual day (Easter Sunday is on April 8 th).I'm still messing around on it.

Also, if it's a leap year-it would really make much of a difference..by that i mean, the calculations like how i have it would still be able to calculate a leap year just fine...or would i have to create a program that would inform the program that a specific year is a leap year and tus do a different calculation?

>>Easter Sunday = March 22+D+E. (Note-This can give a date in April)

I probably mis-interpreted the result of the calculation in line 25. According to your instructions, easter is the month of March plus the caluculation in line 25. If the value calculated at line 25 is greater than 31 (because there are 31 days in March) then Easter is in April and the day in April is whatever was calculated on line 25 minus 31.

So, you do not need that huge if statement. But simply this:

<snip>
    int sunday = (22 + d + e);//The calculation of easter sunday.
    month = 3; // March
    if(sunday > 31)
    {
        month = 4;
        sunday -= 31;
    }
<snip>

Also, line 24 contains a bug -- should be % 7, not % 34 (see your original instructions).

Thanks much for your assistance!! I got it-The program works great....You are correct. Easter sunday = March 22+d+e.

This question has already been answered. Start a new discussion instead.