Help translating the given algorithm into code

Hey guys!I'm having issues being able to translate this given algorithm and putting it into code. I would appreciate some help at least understanding how to start it or an example that i could follow! Below is the given algorithm

Your program must prompt the user to input a single year (any integer
value from 1583 through the maximum possible value for a C++ int)
and will print out the date of Easter (as determined by the algorithm)
for that year in the format dd mmmmm yyyy .
† Your program must
produce a single line of output in exactly this format; however, the
day number may be only a single digit and the year may be more
than 4 digits (1 April 12345, for example). If the year is before
1583, your program must print “INVALID YEAR”. Your program must
use cin for input and cout for output

Let Y be the year for which the date of Easter is desired (in the following, ‘÷’
indicates integer division, and ‘mod’ the remainder after integer division).

E1 [Golden number.] Set G ← (Y mod 19) + 1. (G is the so-called
“golden number” of the year in the 19-year Metonic cycle.)

E2 [Century.] Set C ← (Y ÷ 100) + 1. (When Y is not a multiple
of 100, C is the century number; i.e., 1993 is in the twentieth
century.)

E3 [Corrections.] Set X ← (3C÷4)−12, Z ← (8C+5)÷25−5. (X is
the number of years, such as 1900, in which leap year was dropped
in order to keep in step with the sun. Z is a special correction
designed to synchronize Easter with the moon’s orbit.)

E4 [Find Sunday.] Set D ← (5Y ÷4)−X −10. (March ((−D) mod 7)
actually will be a Sunday.)

E5 [Epact.] Set E ← (11G + 20 + Z − X) mod 30. If E = 25 and the
golden number G is greater than 11, or if E = 24, then increase
E by 1. (E is the so-called “epact,” which specifies when a full
moon occurs.)

E6 [Find full moon.] Set N ← 44 − E. If N < 21 then add 30 to
N. (Easter is supposedly the “first Sunday following the first full
moon which occurs on or after March 21.” Actually, perturbations
in the moon’s orbit do not make this strictly true, but we are
concerned here with the “calendar moon” rather than the actual
moon. The Nth of March is a calendar full moon.)

E7 [Advance to Sunday.] Set N ← N + 7 − ((D + N) mod 7).

E8 [Get month.] If N > 31, the date is (N − 31) April; otherwise the
date is N March.


This is the information i am i given to translate, if you have any questions let me know and i will try to clarify, as of right now i am only able to solve it out in pen and pencil,but am having trouble putting it into code correctly. Thanks!
Best to take it piece by piece. Learning to turn words into programs is one of the most important skills you can learn — and you can’t by having others do it for you.

Here is a help.
Your program must prompt the user to input a single year

Oh, I can do that:
1
2
3
4
5
6
int main()
{
  int year;

  std::cout << "Hey, what year? ";
  std::cin >> year;


(any integer value from 1583 through the maximum possible value for a C++ int)

Likewise, I know how to write an if:
1
2
3
4
5
6
7
8
9
10
11
12
int main()
{
  int year;

  std::cout << "Hey, what year? ";             
  std::cin >> year;

  if (year < 1583)
  {
    std::cout << "foo, dude, 1583 or later!\n";
    return 0;
  }

If the year is before 1583, your program must print “INVALID YEAR”.

Oh.
1
2
3
4
5
6
7
8
9
10
11
12
int main()
{
  int year;

  std::cout << "Hey, what year? ";
  std::cin >> year;

  if (year < 1583)
  {
    std::cout << "INVALID YEAR\n";
    return 0;
  }

Let Y be the year for which the date of Easter is desired

Foo. Alright:
1
2
3
4
5
6
7
8
9
10
11
12
int main()
{
  int Y;

  std::cout << "Hey, what year? ";
  std::cin >> Y;

  if (Y < 1583)
  {
    std::cout << "INVALID YEAR\n";
    return 0;
  }


Next, you are given a step-by-step calculation, complete with variable names to use. Follow along:
E1 [Golden number.] Set G ← (Y mod 19) + 1. (G is the so-called “golden number” of the year in the 19-year Metonic cycle.)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main()
{
  int Y;

  std::cout << "Hey, what year? ";
  std::cin >> Y;

  if (Y < 1583)
  {
    std::cout << "INVALID YEAR\n";
    return 0;
  }

  int G = (Y mod 19) + 1;


When done, you have this cool number N, and a final if to process. Printing the full date is then easy enough:

(For March)
std::cout << std::setw(2) << N << " March " << Y << "\n";

(For April)
std::cout << std::setw(2) << (N - 31) << " April " << Y << "\n";

Easy peasy!

Go forth and kick butt, young grasshopper.


[edit]
Changed the code snippets to be more consistent, adding main() and keeping context with code already written.
Fixed a typo.
Last edited on
Thank you so much!!! I know i have to learn on my own, but having an example like the one you gave me really helps!! Especially getting it all set up! Thanks again, the help is very much appreciated.
Exactly. Keep up the good work!
This is the coding i have so far,

#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
int Y;
Y = year;

cout << "Input Year:";
cin >> Y;

If(Y < 1583)
{
cout << "INVALID YEAR" << endl;
return 0;
}
int G = (Y % 19) + 1;
int C = (Y / 100) + 1;
int X = (3 * C / 4) - 12;
int Z = (8 * C + 5) / 25 -5;
int D = (5 * Y / 4) - X - 10;
int E = (11 * G + 20 + Z - X) % 30;
int N = 44 - E;

If( E = 24 or E = 25 and 11 < G)
{
cout << E + 1<< endl:
}

If( N < 21)
{
cout << N + 30 << endl;
}
Else
{
cout << N + 7 - (( D + N) % 7);
}
//For March
cout << setw(2) << N << " March " << Y << endl;
//For April
cout << setw(2) << (N -31) << " April " << Y << endl;
}


i assume i am missing a sections due to the following errors i get when compiling it,

hmwk1.cpp: In function ‘int main()’:
hmwk1.cpp:18:8: error: ‘year’ was not declared in this scope
Y = year;
^~~~
hmwk1.cpp:18:8: note: suggested alternative: ‘char’
Y = year;
^~~~
char
hmwk1.cpp:23:1: error: ‘If’ was not declared in this scope
If(Y < 1583)
^~
hmwk1.cpp:36:31: error: lvalue required as left operand of assignment
If( E = 24 or E = 25 and 11 < G)
^
hmwk1.cpp:45:1: error: ‘Else’ was not declared in this scope
Else
^~~~
hmwk1.cpp:45:1: note: suggested alternative: ‘pclose’
Else
^~~~
pclose

So, maybe i am not declaring things the way they should be??
There is not any variable named “year”. Your year variable is named “Y”. The word “year” should not appear anywhere in the code. (Sorry, I realize my above example left out important context. I’ll edit the post to add it in in a moment.


C++ is a case-sensitive language. “If” is not a valid control construct. if (all lower-case) is.


C++ confuses a lot of people with equals signs. There are TWO operators made of equals signs:

    =  — Assignment operator. x = -7; Sets x’s value to -7.

    == — Equivalency operator. x == -7 Compares x’s value with -7.

An if statement should have a comparison operator between the parentheses, not an assignment operator:

 
  if (x == -7) std::cout << "Yeah! Negative prime baby!\n";                                               
 
  if (x = -7) std::cout << "The compiler complains that I am assigning a value instead of testing one.\n";


You are otherwise very close. Fix those problems and then you will see what you need to modify to print the date in the right spot. Hint: if statements. (You already wrote the if statements, you just need to print the date instead of the day.)


Hope this helps.
Last edited on
Thank you!!! i have gotten it to print a date, but it wont print out right..

i tried 2016 as the year which should print out

27 March 2016

but instead i get

2723 March 2016

any thoughts on what it could be related too? i know it must be in the calculations, but since they are directly as given i don't understand how it could be them.

Thank you again for the help, lt really has helped understand the translating
Be careful not to mix steps. Also,

cout << "something";

that you do earlier still gets printed out and, because it has no newline, combines with stuff you print later

cout << "Amazing!\n";

somethingAmazing!

You’re almost there!
Thank you! i see where i messed up involving the printing issue, i don't know how i could have missed it! Thanks though
Heh, missing stupid stuff is all part of programming. No matter how good you get at it, or how many years you’ve been doing it, part of your time is still spent fixing the stupid things...

Glad you’ve got it. And I hope you have found it both instructive and fun. :O)
It is hard getting used to.. because they really are just dumb mistakes, but yes thank you for the help!!
Topic archived. No new replies allowed.