-1

This is what I have so far but I am not getting the right outputs.

#include <iostream>
using namespace std;

int main(void) {

    int year_number, month_number, day_number;

    cout << "What year were you born in?\n";
    cin >> year_number;

    cout << "What month were you born in?\n";
    cin >> month_number;

    cout << "What day were you born on?\n";
    cin >> day_number;

    month_number -= 2;

    if (month_number < 0) {

        month_number += 12;
        year_number -= 1;

    }

    month_number *= 83 / 32;
    month_number += day_number;
    month_number += year_number;
    month_number += (year_number / 4);
    month_number -= (year_number / 100);
    month_number += (year_number / 400);

    day_number = month_number % 7;

    cout << "The weekday number you were born on is " << day_number << endl;

    return 0;

}

Here are the instructions:

  1. Decrease month number by 2;
  2. if month number becomes less than 0, increment it by 12 and decrement year by 1;
  3. take month number and multiply it by 83 and divide it by 32;
  4. add day number to month;
  5. add year number to month;
  6. add year/4 to month;
  7. subtract year/100 from month;
  8. add year/400 to month;
  9. find the remainder of dividing month by 7;
walnut
  • 21,629
  • 4
  • 23
  • 59
  • 2
    83 / 32 is not an integer, so it will be truncated before being multiplied by the month_number. Use 83.0 / 32.0 instead. Or do the multiplication first and then divide by 32. – L. Scott Johnson Jan 22 '20 at 13:33
  • 1
    Please have a look at how a *debugger* works to help you step through your program and watch the variables' values while the program is running. This will help you figure out where exactly your program logic is wrong and you can then ask precise questions if any particular behavior is unexpected to you. See also https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems. – walnut Jan 22 '20 at 13:37
  • 2
    `month_number *= 83 / 32` is equivalent to `month_number *= 2`, not to `month_number = month_number * 83 / 32`. – molbdnilo Jan 22 '20 at 13:38
  • breaking up the * and the / of 83 and 32 did not solve the problem... – Austin Edger Jan 22 '20 at 13:51
  • Someone care to explain why this algorithm (the math) works or has a link or for what I can search? – RoQuOTriX Jan 22 '20 at 13:54
  • @RoQuOTrix here is an implentation of the [Zeller congruence as explained on Wikipedia](https://en.wikipedia.org/wiki/Zeller%27s_congruence): https://onlinegdb.com/S1xRUCrb8 The idea is to calculate the total number of days – Taron Jan 22 '20 at 14:02
  • Please don't (try to) fix the code in your question if that code is relevant to the described problem. I have reverted your edit. If you have found a solution to your problem, you can write an answer and post that using the answer box on the bottom of the page (even on your own questions) or you can flag your question as a duplicate of another one if that answered your question or you can delete the question if you don't think anymore that it is going to be useful for others. – walnut Jan 22 '20 at 14:10
  • When you perform those instructions by hand, do you get the correct answer? If so, at what point does the algorithm differ from the calculations you are doing by hand (use a debugger or print statements to find this point of error). – L. Scott Johnson Jan 22 '20 at 14:20

1 Answers1

1

I am not sure where you got those instructions for Zellers congruence. But here is an implementation that follows the formula of wikipedia.

#include <iostream>
#include <math.h>  

int main(void) {

    std::string days[7] = { "Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" };

    int year_number = 2020;
    int month_number = 1;
    int day_number = 22;

    if( month_number < 3 ){
        month_number += 12;
        year_number -= 1;
    }

    int century_number = year_number / 100;
    year_number = year_number % 100;

    int day = day_number + floor( (month_number + 1) * 13.0 / 5.0  ) + 
              year_number + floor( year_number / 4.0 ) +
              floor( century_number / 4.0 ) -
              2 * century_number;

    day = day % 7;

    std::cout << "The weekday you were born on is " << days[day] << std::endl;

    return 0;
}

Live Demo

Another of your problems is probably that you don't floor your intermediate results where you should.

Taron
  • 1,205
  • 2
  • 13
  • 29