Home > Software engineering >  Same math and numbers different answers?
Same math and numbers different answers?

Time:03-16

im writing a simple program that you give a number of days and it gives back the number of years weeks and days that equal to the numbers of days you give. but i noticed that you can get two different answers even tho when i checked the math it makes sense in both cases . can someone please please explain to me why the answers are different and which one is correct

    #include<iostream>

using namespace std;

int main()
{
    int y;
    int d,w;
    int Days;

    cin>>d;

    y=d/365;
    int LessThanAYearDays = d65;
    Days=LessThanAYearDays%7;
    w=LessThanAYearDays/7;
    int SameDays = d%7;
    

    cout<<"answer1 is : "<<y<<" "<<w<<" "<<Days<< "\n";
    cout<<"answer2 is : "<<y<<" "<<w<<" "<<SameDays<< "\n";

    return 0;
}

CodePudding user response:

There are not an exact multiple of weeks in a year, so you are displaying different things. They will be the same in years divisible by 7.

E.g. Assume that a given year starts on a Tuesday.

  • Days corresponds to how many days past the last Tuesday you are.
  • SameDays corresponds to what day you are on.

See also the distinction that std makes between a std::chrono::duration, which is a count of time, and a std::chrono::time_point, which is a count of time since a particular date.

  • Related