Thread: Having trouble with Easter Sunday program

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    28

    Unhappy Having trouble with Easter Sunday program

    Hello,

    I am brand new to programming and am having a lot of trouble in the class I had to take this semester. I'm a Biology major, so obviously have no idea when it comes to programming (but this class is a requirement, for some reason). Anyway, I have barely been keeping up with the concepts, and am still getting very confused. Some programs I have written up just fine (like a calculator), but this one I am having a lot of trouble with.

    Here are the exact words from the HW:

    1. Have the user enter a year between 1982 and 2048. You can compute the date for any Easter Sunday using the following formulas (all values are integers):


    • “a” is a year % 19
    • “b” is year % 4
    • “c” is year % 7
    • “d” is (19 * a + 24) % 30
    • “e” is (2 * b + 4 * c + 6 * d + 5) % 7

    Easter Sunday is March (22 + d + e)

    Write a program that inputs the year and outputs the date (month and day) of Easter Sunday for that year. Print the input as part of the output. For example:

    Enter the year (for example, 1999):
    1985
    Easter is Sunday, the 7, in 1985



    Here is what I have so far:
    Code:
    #include <stdio.h>
    
    int main()
    {
        printf("name here" \n");
        int year;
        int a ;
        int b ;
        int c ;
        int d ;
        int e ;
        int easter_sunday ;
    
        a = year % 19 ;
        b = year % 4 ;
        c = year % 7 ;
        d = (19 * a + 24) %30 ;
        e = (2 * b + 4 * c + 6 * d + 5) % 7 ;
        easter_sunday = (22 + d + e) ;
    
        /* Get year from user*/
        printf ("Enter year between 1982 and 2048: ") ;
        scanf ("%d", year) ;
        printf ("\n Easter Sunday is March %d", easter_sunday) ;
    
    return 0;
    }
    Now, I realize it isn't complete (I'm not sure how to differentiate between March and April, and not sure how to get the output into the input), but from what I am able to understand, it should at least be working and giving me 'March - day'. But, I keep getting two warnings. These warnings are '25: format ^ayear^a expects type ^aint *^a, but argument 2 has type ^aint^a', and '16: ^ayear^a is used uninitialized in this function'.

    I'm sorry if any of this doesn't make any sense. As I said, I am having a lot of trouble understanding this as it's so unlike anything I've ever done before. I think I'm sort of on the right track, but something isn't quite working right.

    Any help would be very much appreciated!
    Thank you!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    > a = year % 19 ;
    So what value is in year, when you do this LATER

    > scanf ("%d", year) ;
    C works in strict order. You can't just say year%19 and expect the result to update automatically every time year changes.

    Plus, the other error message tells you that you forgot the & in scanf, like
    scanf ("%d", &year) ;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Before you start doing calculations which require the year, don't you want to get the year from the user? < facepalm >

    Your IDE might be giving you a warning like:
    "possible use of an uninitialized variable: year"

  4. #4
    Here we go again...
    Join Date
    Sep 2011
    Location
    San Diego
    Posts
    102
    To find the month, check if easter_sunday is greater than 31. If it is, you know the month is April and the day is easter_sunday - 31. If it is not greater than 31 the month is March and the day is easter_sunday.

    Try working with that and the other comments above and see if you can get it going.

  5. #5
    Registered User
    Join Date
    Sep 2011
    Posts
    28
    Thank you everyone! I fixed the warnings and now it's compiled and executing. I am still trying to figure out how to get both March and April, though. Thank you for the advice, rmatze! I am a bit too tired to figure it out now, but I'm going to try and figure the rest of this out tomorrow. Luckily, it's not due until tomorrow night, so I've got a little bit of time. On the bright side, I have all 4 of my other programs working perfectly. So I'm not a completely lost cause, I guess.

    Thank you again! I will update tomorrow if I get this thing running properly or need any more help figuring it out.

  6. #6
    Registered User
    Join Date
    Sep 2011
    Posts
    28
    I got it! It seems so easy now that it is complete, lol.

    Thank you again!

    Code:
    #include <stdio.h>
    
    int main()
    {
        printf("name here" \n");
        int year;
        int a ;
        int b ;
        int c ;
        int d ;
        int e ;
        int easter_sunday ;
    
        /* Get year from user*/
        printf ("Enter year between 1982 and 2048: ") ;
        scanf ("%d", &year) ;
        
        a = year % 19 ;
        b = year % 4 ;
        c = year % 7 ;
        d = (19 * a + 24) %30 ;
        e = (2 * b + 4 * c + 6 * d + 5) % 7 ;
        easter_sunday = (22 + d + e) ;
    
        if (easter_sunday > 31) {
        printf("Easter Sunday in %d", year);
        printf(" is April %d\n", easter_sunday - 31);
        }
        else {
        printf (" Easter Sunday in %d", year);
        printf (" is March %d\n", easter_sunday);
    }   
    return 0;
    }

  7. #7
    Here we go again...
    Join Date
    Sep 2011
    Location
    San Diego
    Posts
    102
    Nice work!

    It's easier to read and a better coding habit when you indent...something like this:

    Code:
    #include <stdio.h>
     
    int main()
    {
        printf("name here" \n");
        int year;
        int a ;
        int b ;
        int c ;
        int d ;
        int e ;
        int easter_sunday ;
     
        /* Get year from user*/
        printf ("Enter year between 1982 and 2048: ") ;
        scanf ("%d", &year) ;
         
        a = year % 19 ;
        b = year % 4 ;
        c = year % 7 ;
        d = (19 * a + 24) %30 ;
        e = (2 * b + 4 * c + 6 * d + 5) % 7 ;
        easter_sunday = (22 + d + e) ;
     
        if (easter_sunday > 31) 
        {
    
            printf("Easter Sunday in %d", year);
            printf(" is April %d\n", easter_sunday - 31);
    
        }
        else 
        {
    
            printf (" Easter Sunday in %d", year);
            printf (" is March %d\n", easter_sunday);
    
        }   
    
    
        return 0;
    
    }

  8. #8
    Registered User
    Join Date
    Sep 2011
    Posts
    28
    Thanks! I'll remember that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with Easter Day Calculator
    By chrandbar in forum C Programming
    Replies: 10
    Last Post: 03-26-2009, 12:40 PM
  2. This browser demo probably owns you 9 ways from sunday
    By Jeremy G in forum A Brief History of Cprogramming.com
    Replies: 19
    Last Post: 08-22-2004, 12:04 AM
  3. Sunday Smut
    By Fordy in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 07-21-2002, 09:22 PM
  4. Easter Day?
    By Unregistered in forum C Programming
    Replies: 0
    Last Post: 03-30-2002, 06:25 PM
  5. taking suggestions for easter eggs.
    By doubleanti in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 03-15-2002, 01:07 PM