Thread: Compute Full Moon

  1. #1
    Registered User
    Join Date
    Nov 2017
    Posts
    12

    Compute Full Moon

    Hi, please help me with algorithm that compute the Phase of the Moon after year 2000. The inputs are y, m, d .. Next Full moon constant is 29.53059027 and first Full Moon was 16.7.2000 in 13:55:12. Output is 1 or 0 if day is Full Moon or not. Thanks a lot.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    What have you done so far? We don't provide complete solutions here, just so you know. We just help you find the solution yourself.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Jun 2017
    Posts
    88
    first Full Moon was 16.7.2000 in 13:55:12.
    Are you saying that the first full moon which occurred in the year 2000 happened in July?

    Or perhaps that is the full moon you chose as your first. Though you did write:
    compute the Phase of the Moon after the year 2000.
    Full moon calendar.
    Last edited by jack jordan; 11-29-2017 at 01:45 PM.

  4. #4
    Banned
    Join Date
    Aug 2017
    Posts
    861
    so you can check your work
    Moon Phase Calculator

    https://stardate.org/nightsky/moon

  5. #5
    Registered User
    Join Date
    Nov 2017
    Posts
    12
    Hi, so I made a programm that works.

    Code:
    int prevFullMoon ( int y, int m, int d,
                       int * prevY, int * prevM, int * prevD )
    {
        int i, j, mx;
        j = 0;
        i = isFullMoon(y, m, d);
        if(i == 0)
        {
            do
            {
                d--;
                j++;
                if(d == 0)
                {
                    if(m == 1)
                    {
                        y = y-1;
                        m = 12;
                    }
                    else m = m-1;
                    mx = max(y, m);
                    d = mx;
                }
    
    
                i = isFullMoon(y, m, d);
    
    
    
    
            }while(i != 1);
    
    
        }
    
    
        *prevY = y;
        *prevM = m;
        *prevD = d;
    
    
    
    
    }
    
    
    
    
    
    
    int main ( int argc, char * argv [] )
    {
      int y, m, d;
    
    
      assert ( prevFullMoon ( 2017, 1, 11, &y, &m, &d ) == 1 && y == 2016 && m == 12 && d == 14 );
      assert ( prevFullMoon ( 2017, 1, 12, &y, &m, &d ) == 1 && y == 2016 && m == 12 && d == 14 );
      assert ( prevFullMoon ( 2017, 1, 13, &y, &m, &d ) == 1 && y == 2017 && m == 1 && d == 12 );
      assert ( prevFullMoon ( 2017, 11, 3, &y, &m, &d ) == 1 && y == 2017 && m == 10 && d == 5 );
      assert ( prevFullMoon ( 2017, 11, 4, &y, &m, &d ) == 1 && y == 2017 && m == 10 && d == 5 );
      assert ( prevFullMoon ( 2017, 11, 5, &y, &m, &d ) == 1 && y == 2017 && m == 11 && d == 4 );
      assert ( prevFullMoon ( 2019, 12, 11, &y, &m, &d ) == 1 && y == 2019 && m == 11 && d == 12 );
      assert ( prevFullMoon ( 2019, 12, 12, &y, &m, &d ) == 1 && y == 2019 && m == 12 && d == 11 );
      assert ( prevFullMoon ( 2019, 12, 13, &y, &m, &d ) == 1 && y == 2019 && m == 12 && d == 11 );
    return 0;
    This is a part of it ... And I dont know, how to output y, m, d from function. Program computes, pervFllMoon correctly I just need to return the y, m and d values.

  6. #6
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Your prevFullMoon function should return something, right now it returns nothing.

    There are many ways to output the results of a function. One is to call printf directly inside the function. Another is to first call the function and then call printf on the results.
    Devoted my life to programming...

  7. #7
    Registered User
    Join Date
    Nov 2017
    Posts
    12
    Okay, I got this code and want to ask if its ok or I can change something and make it better ? I am not sure with the outputs as pointers.

    Code:
    #include <stdio.h>
    #include <assert.h>
    #include <math.h>
    #define INVALID_DATE (-1)
    
    
    
    int leap_y(int y)
    {
    
        int k;
        k = 0;
        if(y % 4 == 0) k = 1;
        if(y % 100 == 0) k = 0;
        if(y % 400 == 0) k = 1;
        if(y % 4000 == 0) k = 0;
        return k;
    
    }
    
    int max(int y, int m)
    {
        int max, k;
        k = leap_y(y);
    
        if(m < 8)
        {
                    if( (m == 2) && (k == 0) ) max = 28;
                    else if( (m == 2) && (k == 1)) max = 29;
                    else if ( (m % 2) == 0) max = 30;
                    else max = 31;
    
        }
    
        if(m >= 8)
            {
    
                    if ( (m % 2) == 0) max = 31;
                    else max = 30;
            }
    
        return max;
    }
    
    int ConvertM(int y, int m)
    {
        int mm, k, d;
        d = 0;
        mm = 1;
    
    
        k = leap_y(y);
    
        if(m <= 8)
        {
            mm = m - 1;
    
            while(mm != 0)
            {
                    if( (mm == 2) && (k == 0) ) {d = d + 28;}
                    else if( (mm == 2) && (k == 1)) {d = d + 29;}
                    else if ( (mm % 2) == 0) {d = d + 30;}
                    else {d = d + 31;}
                    mm--;
            }
        }
    
        if(m > 8){
    
        mm = m - 1;
    
            while(mm >= 8)
            {
                    if ( (mm % 2) == 0) d = d + 31;
                    else d = d + 30;
                    mm--;
            }
            if(k == 1) d = d + 213;
            if(k == 0) d = d + 212;
    
        }
        return d;
    }
    
    int Convert(int y, int m, int d)
    {
        int value, leap, leap1, l, l1, k;
        k = leap_y(y);
    
        y = y - 2000;
        m = ConvertM(y, m);
    
    
        leap1 = 366;
        leap = 365;
        l = 0;
        l1 = 0;
    
        while (y != 0)
        {
            k = leap_y(y);
    
            if( k == 1)
            {
                l1++;
            }
    
            if( k == 0)
            {
                l++;
    
            }
    
            y--;
        }
    
        value = l1*leap1 + l*leap + m + d;
        return value;
    
    }
    
    
    
    
    int isFullMoon ( int y, int m, int d )
    {
    
        int k, fullmoon, vali, mx;
        double val, diff, cons1, cons2, val1;
        cons1 = 29.53059027;
        cons2 = 197.58;
        fullmoon = 1;
        k = leap_y(y);
        mx = max(y, m);
    
        if(y < 2000) return INVALID_DATE;
        if(m < 1 || m >12) return INVALID_DATE;
        if(d < 1 || d > mx) return INVALID_DATE;
    
    
        val = Convert(y, m, d);
    
        val1 = (val - cons2)/cons1;
    
        vali = val1;
    
        diff = fabs(val1 - vali);
    
    
        if(k == 0)
        {
        if(diff > 0.962  && diff <= 1)
        {
            fullmoon = 1;
        }
        else fullmoon = 0;
    
        }
    
    
        if(k == 1)
        {
        if(diff >= 0  && diff < 0.0485)
        {
            fullmoon = 1;
        }
        else fullmoon = 0;
    
    
        }
    
        return fullmoon;
    }
    
    int prevFullMoon ( int y, int m, int d,
                       int * prevY, int * prevM, int * prevD )
    {
        int i, mx;
        mx = max(y, m);
    
        if(y < 2000) return INVALID_DATE;
        if(m < 1 || m >12) return INVALID_DATE;
        if(d < 1 || d > mx) return INVALID_DATE;
    
    
    
        i = isFullMoon(y, m, d);
    
        if(i == 1)
        {
        d = d - 1;
            do
            {
                d--;
                if(d == 0)
                {
                    if(m == 1)
                    {
                        y = y - 1;
                        m = 12;
                    }
                    else m = m - 1;
                    mx = max(y, m);
                    d = mx;
                }
    
                i = isFullMoon(y, m, d);
    
    
            }while(i != 1);
        }
    
        if(i == 0)
        {
            do
            {
                d--;
                if(d == 0)
                {
                    if(m == 1)
                    {
                        y = y - 1;
                        m = 12;
                    }
                    else m = m - 1;
                    mx = max(y, m);
                    d = mx;
                }
    
                i = isFullMoon(y, m, d);
    
    
            }while(i != 1);
    
        }
    
    
    
        *prevY = y;
        *prevM = m;
        *prevD = d;
    
        return 1;
    
    
    }
    
    int nextFullMoon ( int y, int m, int d,
                       int * nextY, int * nextM, int * nextD )
    {
        int i, mx;
        mx = max(y, m);
    
        if(y < 2000) return INVALID_DATE;
        if(m < 1 || m >12) return INVALID_DATE;
        if(d < 1 || d > mx) return INVALID_DATE;
    
    
    
        i = isFullMoon(y, m, d);
        mx = max(y, m);
    
        if(i == 1)
        {
        d = d + 1;
            do
            {
                d++;
                if(d == mx)
                {
                    if(m == 12)
                    {
                        y = y + 1;
                        m = 1;
                    }
                    else m = m + 1;
                    d = 1;
                }
    
                i = isFullMoon(y, m, d);
    
    
            }while(i != 1);
    
        }
    
        if(i == 0)
        {
            do
            {
                d++;
                if(d == mx)
                {
                    if(m == 12)
                    {
                        y = y + 1;
                        m = 1;
                    }
                    else m = m + 1;
                    d = 1;
                }
    
                i = isFullMoon(y, m, d);
    
    
            }while(i != 1);
    
        }
    
    
    
        *nextY = y;
        *nextM = m;
        *nextD = d;
    
        return 1;
    }
    
    int main ( int argc, char * argv [] )
    {
      int y, m, d;
    
      assert ( isFullMoon ( 2017, 1, 11 ) == 0 );
      assert ( isFullMoon ( 2017, 1, 12 ) == 1 );
      assert ( isFullMoon ( 2017, 1, 13 ) == 0 );
      assert ( isFullMoon ( 2017, 11, 3 ) == 0 );
      assert ( isFullMoon ( 2017, 11, 4 ) == 1 );
      assert ( isFullMoon ( 2017, 11, 5 ) == 0 );
      assert ( isFullMoon ( 2019, 12, 11 ) == 1 );
      assert ( isFullMoon ( 2019, 12, 12 ) == 0 );
      assert ( isFullMoon ( 2019, 12, 13 ) == 0 );
      assert ( isFullMoon ( 2000, 7, 16 ) == 1 );
      assert ( isFullMoon ( 2016, 12, 14 ) == 1);
      assert ( prevFullMoon ( 2017, 1, 11, &y, &m, &d ) == 1 && y == 2016 && m == 12 && d == 14 );
      assert ( prevFullMoon ( 2017, 1, 12, &y, &m, &d ) == 1 && y == 2016 && m == 12 && d == 14 );
      assert ( prevFullMoon ( 2017, 1, 13, &y, &m, &d ) == 1 && y == 2017 && m == 1 && d == 12 );
      assert ( prevFullMoon ( 2017, 11, 3, &y, &m, &d ) == 1 && y == 2017 && m == 10 && d == 5 );
      assert ( prevFullMoon ( 2017, 11, 4, &y, &m, &d ) == 1 && y == 2017 && m == 10 && d == 5 );
      assert ( prevFullMoon ( 2017, 11, 5, &y, &m, &d ) == 1 && y == 2017 && m == 11 && d == 4 );
      assert ( prevFullMoon ( 2019, 12, 11, &y, &m, &d ) == 1 && y == 2019 && m == 11 && d == 12 );
      assert ( prevFullMoon ( 2019, 12, 12, &y, &m, &d ) == 1 && y == 2019 && m == 12 && d == 11 );
      assert ( prevFullMoon ( 2019, 12, 13, &y, &m, &d ) == 1 && y == 2019 && m == 12 && d == 11 );
      assert ( nextFullMoon ( 2017, 1, 11, &y, &m, &d ) == 1 && y == 2017 && m == 1 && d == 12 );
      assert ( nextFullMoon ( 2017, 1, 12, &y, &m, &d ) == 1 && y == 2017 && m == 2 && d == 11 );
      assert ( nextFullMoon ( 2017, 1, 13, &y, &m, &d ) == 1 && y == 2017 && m == 2 && d == 11 );
      assert ( nextFullMoon ( 2017, 11, 3, &y, &m, &d ) == 1 && y == 2017 && m == 11 && d == 4 );
      assert ( nextFullMoon ( 2017, 11, 4, &y, &m, &d ) == 1 && y == 2017 && m == 12 && d == 3 );
      assert ( nextFullMoon ( 2017, 11, 5, &y, &m, &d ) == 1 && y == 2017 && m == 12 && d == 3 );
      assert ( nextFullMoon ( 2019, 12, 11, &y, &m, &d ) == 1 && y == 2020 && m == 1 && d == 10 );
      assert ( nextFullMoon ( 2019, 12, 12, &y, &m, &d ) == 1 && y == 2020 && m == 1 && d == 10 );
      assert ( nextFullMoon ( 2019, 12, 13, &y, &m, &d ) == 1 && y == 2020 && m == 1 && d == 10 );
      assert ( prevFullMoon ( 2000, 7, 16, &y, &m, &d ) == 1 && y == 2000 && m == 6 && d == 17);
      assert ( isFullMoon ( 2017, 1, 12 ) == 1 );
      assert ( isFullMoon ( 2017, 2, 11 ) == 1 );
      assert ( isFullMoon ( 2017, 3, 12 ) == 1 );
      assert ( isFullMoon ( 2017, 4, 11 ) == 1 );
      assert ( isFullMoon ( 2017, 5, 10 ) == 1 );
      assert ( isFullMoon ( 2017, 6, 9 ) == 1 );
      assert ( isFullMoon ( 2017, 7, 9 ) == 1 );
      assert ( isFullMoon ( 2017, 8, 7 ) == 1 );
      assert ( isFullMoon ( 2017, 9, 6 ) == 1 );
      assert ( isFullMoon ( 2017, 10, 5 ) == 1 );
      assert ( isFullMoon ( 2017, 11, 4 ) == 1 );
      assert ( isFullMoon ( 2017, 12, 3 ) == 1 );
      assert ( isFullMoon ( 2018, 1, 2 ) == 1 );
      assert ( isFullMoon ( 2018, 1, 31 ) == 1 );
      assert ( isFullMoon ( 2018, 3, 2 ) == 1 );
      assert ( isFullMoon ( 2018, 3, 31 ) == 1 );
      assert ( isFullMoon ( 2018, 4, 30 ) == 1 );
      assert ( isFullMoon ( 2018, 5, 29 ) == 1 );
      assert ( isFullMoon ( 2018, 6, 28 ) == 1 );
      assert ( isFullMoon ( 2018, 7, 27 ) == 1 );
      assert ( isFullMoon ( 2018, 8, 26 ) == 1 );
      assert ( isFullMoon ( 2018, 9, 25 ) == 0 );
      assert ( isFullMoon ( 2018, 10, 24 ) == 1 );
      assert ( isFullMoon ( 2018, 11, 23 ) == 1 );
      assert ( isFullMoon ( 2018, 12, 22 ) == 1 );
      assert ( isFullMoon ( 2019, 1, 21 ) == 1 );
      assert ( isFullMoon ( 2019, 2, 19 ) == 1 );
      assert ( isFullMoon ( 2019, 3, 21 ) == 1 );
      assert ( isFullMoon ( 2019, 4, 19 ) == 1 );
      assert ( isFullMoon ( 2019, 5, 18 ) == 0 );
      assert ( isFullMoon ( 2019, 6, 17 ) == 1 );
      assert ( isFullMoon ( 2019, 7, 16 ) == 0 );
      assert ( isFullMoon ( 2019, 8, 15 ) == 1 );
      assert ( isFullMoon ( 2019, 9, 14 ) == 1 );
      assert ( isFullMoon ( 2019, 10, 13 ) == 1 );
      assert ( isFullMoon ( 2019, 11, 12 ) == 1 );
      assert ( isFullMoon ( 2019, 12, 12 ) == 0 );
      assert ( prevFullMoon ( 2000, 11, 31, &y, &m, &d ) == INVALID_DATE );
      assert ( nextFullMoon ( 2001, 2, 29, &y, &m, &d ) == INVALID_DATE );
      assert ( isFullMoon ( 2004, 2, 29 ) == 0 );
      assert ( prevFullMoon ( 2048, 2, 26, &y, &m, &d ) == 1 && y == 2048 && m == 1 && d == 30);
      assert ( prevFullMoon ( 2100, 2, 29, &y, &m, &d ) == INVALID_DATE );
      assert ( nextFullMoon ( 2000, 2, 29, &y, &m, &d ) == 1 && y == 2000 && m == 3 && d == 20 );
      return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. full size of the window, full screen mode
    By nerio in forum C Programming
    Replies: 4
    Last Post: 07-20-2011, 05:25 PM
  2. 3rd moon?
    By dbaryl in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 10-02-2002, 02:31 PM
  3. Moon shot in 4kb - could you?
    By Ken Fitlike in forum Tech Board
    Replies: 10
    Last Post: 09-04-2002, 02:17 PM
  4. Full moon out tonight?
    By Deckard in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 01-19-2002, 10:38 PM

Tags for this Thread