944,196 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 3174
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Jun 20th, 2007
0

Easter Sunday

Expand Post »
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.
--------------------------------------------------------------------------------
C++ Syntax (Toggle Plain Text)
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <string>
  4. #include <cmath>
  5.  
  6. using namespace std;
  7.  
  8. int main()
  9. {
  10.  
  11. int x;//The year for which it is desired to compute Easter Sunday.
  12. int a = x % 19;//The remainder of the division of x by 19.
  13. int b = x % 4;//The remainder of the division of x by 4.
  14. int c = x % 7;//The remainder of the division of x by 7.
  15. int d = (19 * a) + (24) % 30;//The remainder of the division of (19*a + 24) by 30.
  16. 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.
  17. int sun = (22 + d + e);//The calculation of easter sunday.
  18. cout<< "Enter the year you wish to inquire about"<<endl;
  19. cout<<x<<endl;
  20. cin>> x;//The 4-digit year.
  21. cout<< sun<<endl;
  22. return 0;
Last edited by Ancient Dragon; Jun 20th, 2007 at 8:50 pm. Reason: add code tags
Similar Threads
Featured Poster
Reputation Points: 129
Solved Threads: 26
Nearly a Posting Maven
zandiago is offline Offline
2,463 posts
since Jun 2007
Jun 20th, 2007
0

Re: Easter Sunday

You are trying to output the value of x before actually getting the value. IE. You are using cout<<x<< endl; before cin>> x;
Sponsor
Reputation Points: 520
Solved Threads: 268
Code Monkey
ShawnCplus is offline Offline
1,564 posts
since Apr 2005
Jun 20th, 2007
0

Re: Easter Sunday

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.
Featured Poster
Reputation Points: 129
Solved Threads: 26
Nearly a Posting Maven
zandiago is offline Offline
2,463 posts
since Jun 2007
Jun 20th, 2007
0

Re: Easter Sunday

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,963 posts
since Aug 2005
Jun 21st, 2007
0

Re: Easter Sunday

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.
Featured Poster
Reputation Points: 129
Solved Threads: 26
Nearly a Posting Maven
zandiago is offline Offline
2,463 posts
since Jun 2007
Jun 21st, 2007
0

Re: Easter Sunday

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
Last edited by ShawnCplus; Jun 21st, 2007 at 2:31 pm.
Sponsor
Reputation Points: 520
Solved Threads: 268
Code Monkey
ShawnCplus is offline Offline
1,564 posts
since Apr 2005
Jun 21st, 2007
0

Re: Easter Sunday

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.
Featured Poster
Reputation Points: 129
Solved Threads: 26
Nearly a Posting Maven
zandiago is offline Offline
2,463 posts
since Jun 2007
Jun 21st, 2007
0

Re: Easter Sunday

Also, where do the 'if' and 'else' statements come into play with such a program?
Featured Poster
Reputation Points: 129
Solved Threads: 26
Nearly a Posting Maven
zandiago is offline Offline
2,463 posts
since Jun 2007
Jun 21st, 2007
0

Re: Easter Sunday

Click to Expand / Collapse  Quote originally posted by zandiago ...
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.
Sponsor
Reputation Points: 520
Solved Threads: 268
Code Monkey
ShawnCplus is offline Offline
1,564 posts
since Apr 2005
Jun 21st, 2007
0

Re: Easter Sunday

#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?
Featured Poster
Reputation Points: 129
Solved Threads: 26
Nearly a Posting Maven
zandiago is offline Offline
2,463 posts
since Jun 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: C++ help
Next Thread in C++ Forum Timeline: Error messages aplenty!!!!!





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC