Okay well i'm having trouble understanding this program ..in which our professor doesn't even explain..I'm so lost..
I hope somebody can help me please..
here's the problem...

I have to write a program in c++ that computes the number of days between two dates using the Julian day number which is the number of days that have elapsed since noon on jan 1, 4713 B.C , so i have to use the julian day number for each date and subtract one from the other.
This algorithm might help.. So given the year (an integer such as 2001), month (an integer from 1 to 12 ), and day ( an integer from 1 through 31), so if month is 1 or 2, then subtract 1 from year and add 12 to month.

If the date comes from the Gregorian calender (later than Oct 15, 1582), then compute an intermediate result with the following formula (otherwise, let intRes1 equal 0):

intRes1 = 2 - year/100+year/400 (integer division)

compute a second intermediate result with the formula

intRes2 = int(365.25 * year)

compute a third intermediate result with the formula

intRes3 = int(30.6001 * (month + 1))

Finally, the Julian day number is computed with the formula
julianDay = intRes1 + intRes2 + intRes3 + day + 1720994.5

So my program should make appropriate use of value-returning functions in solving this problem. integer type long and floating point type double may have to be used. Also it should prompt appropriately for input (the two dates) if it is to be run interactively. also it has to use proper style and comments.
so i have to make up my own test data and be ready to explain rationale behind my choice.
AND thats the situation..I am so stacked and i hope somebody can help me out :confused: ..Well thanks for your time and hopefully i can get this problem solved .. I really appreciate your help and dedication.. Thanks again.

Dani AI

Generated

Short answer: break the task into small, well-commented functions (parse/validate, calendar-choice, convert-to-JDN, compute-difference). was right about the pseudo-code approach, and is right that the spec already tells you what to do — just isolate the math from I/O and test each piece.

Below is a compact, robust C++ implementation of the conversion and difference using an integer JDN formula (avoids floating truncation and handles the historical Gregorian cutover at 1582-10-15). Keep each function small so you can comment and test them individually.

#include <cstdlib>   // for llabs
#include <cstdint>

struct Date { int y, m, d; };

bool isGregorian(const Date& dt) {
    if (dt.y > 1582) return true;
    if (dt.y < 1582) return false;
    if (dt.m > 10) return true;
    if (dt.m < 10) return false;
    return dt.d >= 15;
}

long long julianDayNumber(const Date& dt) {
    int a = (14 - dt.m) / 12;
    long long y = (long long)dt.y + 4800 - a;
    int m = dt.m + 12*a - 3;
    if (isGregorian(dt)) {
        return dt.d + ( (153LL*m + 2) / 5 ) + 365*y + y/4 - y/100 + y/400 - 32045;
    } else {
        return dt.d + ( (153LL*m + 2) / 5 ) + 365*y + y/4 - 32083;
    }
}

long long daysBetween(const Date& a, const Date& b) {
    long long j1 = julianDayNumber(a);
    long long j2 = julianDayNumber(b);
    return llabs(j1 - j2); // use absolute diff; add 1 if you need inclusive count
}

Test cases to include and why: same-day -> 0 (sanity); 2000-02-28 vs 2000-03-01 -> checks leap-year-2000 (expect 2); 1900-02-28 vs 1900-03-01 -> checks non-leap century (expect 1); 1582-10-04 vs 1582-10-15 -> checks the Gregorian cutover (expect 1 if you treat the cutover historically). Also test reversed input and wide year ranges.

Troubleshooting notes: validate month/day (use the correct leap rule depending on calendar), prefer 64-bit integers for JDN to avoid overflow for extreme years, and be explicit about whether you want exclusive vs inclusive day counts. This function-based layout makes adding input validation, unit tests, and comments simple — exactly what the assignment is asking for.

Recommended Answers

All 4 Replies

I think it's explained pretty well what the program should do. Just read more carefully...

Santors class? :)

Seems like the crux of your problem's the math.
Specifically, how do I convert ANY date to the Julian calendar day.
Google will provide you with a clear, concise formula to apply to your user's input, that's a formulaic issue.. not programming.

pseudo-code might be

do
{
print Program's purpose
print Enter a date (ddmm [+/-] yyyy)
get Input1
print Enter a date
get Input2
if CheckInputValidity IS INVALID skip rest of loop
convert 2 inputs to julian dates
Do the math.. subtract one from the other
print the results
} while (userInput != Quit)

This may be frustratingly vague.. but the point is to get the exact mathematical formula (I don't have it), and apply it at the appropriate point in the code.
Your instructor may dock you for not error checking the user input, that is.. invalid dates. I'll check back.

Wow, can you people not read the date of the post? Dec 1st, 2004 was when the thread was last active! Don't bump old threads randomly!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.