user9319694
user9319694

Reputation: 7

Converting Julian Date to Gregorian Date - C programming

I'm stuck on an assignment for C where I have to convert a Julian Date that is input by the user and convert it to Gregorian. When I run it and I enter a number for the Julian date all it prints out is 1, 0, -12 for the month, day and year. How to fix? I also don't completely understand pointers so maybe that is the problem?

#include <stdio.h>
#include <math.h>

void getDateFromJulian(double jd, int *month, int *day, int *year);
double getDoubleFromUser(char* msg);

int main() {
    double jd = 0;
    int month, day, year;

    jd = getDoubleFromUser("Enter a valid Julian Day: ");

    if (jd != -999.0) {
        getDateFromJulian(jd, &month, &day, &year);

        printf("Month, day, year is: %d, %d, %d \n", month, day, year);
    }

    return;
}

double getDoubleFromUser(char* msg){
    int input;
    int term;

    //check for valid number
    printf("Enter Julian Day: \n");
    scanf_s("%1f%c");
    if (scanf_s("%1f%c", &input, &term) != 2) {
        if (term >= 0 * 41 && term <= 0 * (int)7) {
            printf("That's not a valid number!\n");
            return -999.0;
        }
    }
}

void getDateFromJulian(double jd, int *month, int *day, int *year) {
    int A, B, C, D, E, alpha;
    double Z, F;
    int JD = 0;

    F = modf(JD, &Z);

    if (Z < 2299161) {
        A = Z;
    }

    if (Z >= 2299161) {
        alpha = (int)((Z - 1867216.25) / 36524.25);
        A = Z + 1 + alpha - (int)(alpha / 4);
    }
    B = A + 1524;
    C = (int)((B - 122.1) / 365.25);
    D = (int)(365.25 * C);
    E = (int)((B - D) / 30.6001);

    day = B - D - (int)(30.6001 * E) + 0.5;

    if (E < 14) {
        month = E - 1;
    }
    if (E = 14 || 15) {
        month = E - 13;
    }

    if (month > 2) {
        year = C - 4716;
    }
    if (month = 1 || 2) {
        year = C - 5715;
    }

    return;
}

Upvotes: 1

Views: 1997

Answers (2)

Mark Benningfield
Mark Benningfield

Reputation: 2892

In addition to assigning integers to pointers (month = newVal; for example), some of your conditional tests are always true.

     if (E = 14 || 15) {
         month = newVal;
         newVal = E - 13;
     }

This assigns the value of 14 || 15 (which is 1) to E, and then tests that E is non-zero, which of course, it always is.

This test (and the other one like it) should be:

if (E == 14 || E == 15) {
    *month = newVal;
    newVal = E - 13;
}

Note that the newVal integer is assigned to the integer that is pointed to by month (by using *month), and not the actual pointer variable month. This remedy should be applied to assignments to the day and year pointers also.

Upvotes: 0

Snohdo
Snohdo

Reputation: 156

When you assign to the day, month, or year you should do it as follows:

           day[0] = B - D - (int)(30.6001 * E) + 0.5;

           month[0] = E - 1;

and so on. Basically, you have to assign to the first element of the pointer (array) which is sometimes a little bit clearer than using an asterisk, which you left out.

The program effectively wasn't doing anything because the output values were essentially set to gibberish.

Upvotes: 1

Related Questions