Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Calender program help please

  1. Jun 7, 2012 #1
    Hello, I'm trying to make a calender that will prompt the user to type in a year, and it will show them a calender for that year. I have the formulas that I use to find out what day January will start on for that year, and which years are leap years.

    Here's what I have so far. I've done a lot wrong. What doesn't look right, or doesn't make sense, or what should I do differently?

    Thanks.

    #include<stdio.h>

    printMonth (int startDay, int days)
    int leap (int year)
    int year (void)
    int dayFormula (int year)
    int main (void)
    {
    int year;
    int dayFormula;
    int leap;

    year = year (void);
    dayFormula = dayFormula (year);
    leap = leap (year);
    printMonth (int startDay, int days);
    }
    int year (void)
    {
    int year;
    printf("Type in a year: ");
    scanf("%d", &year);

    return year;
    }
    int dayFormula (year);
    {
    int dayFormula;
    dayFormula = (((year-1)*365)+((year-1)/4)-((year-1)/100)+((year-1)/400)+1)%7;
    return dayFormula;
    }
    int leap (int year)
    {
    if (!(year % 4) && (year % 100)) || !(year % 400))
    days = 29;
    else
    days = 28;
    }
    int printMonth (int startDay, int days)
    {

    }
     
  2. jcsd
  3. Jun 7, 2012 #2

    Mark44

    Staff: Mentor

    Re: Calender

    Please put [noparse]
    Code (Text):
     and
    [/noparse] around your code, as I have done below. This preserves your indenting, and makes your code easier to read.

    I added comments in your code about changes I would make.
     
    Last edited: Jun 7, 2012
  4. Jun 8, 2012 #3
    Re: Calender

    Ok, I changed the things you said. But it's now saying that before my printMonth thing, it says "expected expression before 'int'". I'm not sure what that means. Plus, what should I put in the printMonth function at the bottom?

    Thanks

    Code (Text):
    #include<stdio.h>

    printMonth (int startDay, int days);
    int getLeap (int year);
    int getYear (void);
    int dayFormula (int year);

    int main (void)
    {
        int year;
        int formula;
        int leap;

        year = getYear ();
        formula = dayFormula (year);
        leap = getLeap (year);
        printMonth (int startDay, int days);
    }
    int getYear (void)
    {
        int year;
        printf("Type in a year: ");
        scanf("%d", &year);

        return year;
    }
    int dayFormula (year);
    {
        int formula;
        formula = (((year-1)*365)+((year-1)/4)-((year-1)/100)+((year-1)/400)+1)%7;
        return formula;
    }
    int getLeap (int year)
    {
        if (!(year % 4) && (year % 100)) || !(year % 400))
            days = 29;
        else
            days = 28;

    }
    int printMonth (int startDay, int days)
    {

    }
     
  5. Jun 8, 2012 #4

    Mark44

    Staff: Mentor

    Re: Calender

    The error you're getting is from not using printMonth() correctly. In the definition for printMonth, this function is advertised as returning an int. The prototype for it omits the return type - it should be there.

    Code (Text):

    int main (void)
    {
        int year;
        int formula;
        int leap;

        year = getYear ();
        formula = dayFormula (year);
        leap = getLeap (year);
        printMonth (int startDay, int days);
    }
    In your call to printMonth() -- last line, above -- you are using it as if it were a void function (i.e., one that returns void). You are also putting in types for the parameters in the call to printMonth. They shouldn't be there. The call to printMonth should look something like this.
    Code (Text):
    result = printMonth(startDay, days);
    The result (or whatever you want to call it) needs to be declared in main(). The startDay and days variables need to be declared and initialized to appropriate values before you call printMonth().

    I've been throwing around some terminology, so to make sure you understand the difference between a function prototype (or declaration), function definition, and function call, I'll give some quick definitions.

    Function prototype - The declaration statement in which you list the return type for the function, and the types of all of its parameters. A function prototype typically appears above the definition for main(). If you put the definition for the function above main(), the definition serves as the prototype.
    Here is what your prototype should look like. Note that I have included the return type, int.
    Code (Text):
    int printMonth (int startDay, int days);
    Function definition - The block that includes the body of the function - its code. You haven't written the function body yet, so it's empty.
    Code (Text):

    int printMonth (int startDay, int days)
    {

    }
     
    Function call - The expression or statement in which you call (use) your function. If a function returns a value, the call often appears on the right side of an assignment statement, but can appear anywhere a value of that type (the type returned by the function) can appear.
    Code (Text):
    result = printMonth(startDay, days);
    Your intent for printMonth() is for it to print a month, but which month? The function would be more useful if you passed the month as a parameter. I think I understand the purpose of startDay (the day of the week that the first of the given month is on), but what is the purpose of days? That would be useful only if the month you're printing happens to be February.

    Before I can give any help for printMonth() you need to explain what you want it to do. It's not clear to me that you understand what you want it to do. You have it as returning a value. What should that value represent? What should the function do? Before you write the code you should have an algorithm in mind - the specific steps, in pseudocode, that you want this function to carry out.

    You still have some problems with getLeap():
    1. day is not declared.
    2. the function doesn't return anything.
     
  6. Jun 8, 2012 #5
    Re: Calender

    Thanks for spending the time to help me with this.
    I'm very new to programming. About 4 weeks ago, I didn't even know how programming was done. I knew they typed some stuff somewhere on a computer, but that was the extent of my knowledge.

    Code (Text):
    #include<stdio.h>

    int printMonth (int startDay, int days);
    int getLeap (int year);
    int getYear (void);
    int dayFormula (int year);

    int main (void)
    {
        int year, formula, leap, result, startDay, days;

        year = getYear ();
        formula = dayFormula (year);
        leap = getLeap (year);
        result = printMonth (startDay, days);
    }
    int getYear (void)
    {
        int year;
        printf("Type in a year: ");
        scanf("%d", &year);

        return year;
    }
    int dayFormula (year)
    {
        int formula;
        formula = (((year-1)*365)+((year-1)/4)-((year-1)/100)+((year-1)/400)+1)%7;
        return formula;
    }
    int getLeap (int year)
    {
        if (!(year % 4) && (year % 100) || !(year % 400))
            days = 29;
        else
            days = 28;

    }
    int printMonth (int startDay, int days)
    {

    }
     
    Here's the fixes. Did I fix them correctly? The error now goes down to line 34 where "days" is undeclared. I'm not sure what to do about that other than put "int days" into the function, but that doesn't seem like it would grant me any progress.

    And to answer your questions: printMonth is to print all the months. I think I'm supposed to use it 12 times. At least that's what I understood from the notes I took in class. How do I pass the month as a parameter?

    Yes, startDay is the first day of the month. I wrote down some formula that the teacher wrote on the board: (day + 31)%7. That's supposed to get the first day of the month.
    Days is how many days the month has. It seems like it's only relevant for February though, like you said.

    I know what I want to do, but turning it into a program is hard. The only steps I know of is "prompt user to enter a year" and then "print year". The "print year" step needs to be divided into steps, I guess, since that's the entire program. But I don't know how to divide it, since it does it all at once.

    And with getLeap, I'm not sure what to return. Return is what it causes the program to output, right?

    Thanks again.
     
  7. Jun 9, 2012 #6

    Mark44

    Staff: Mentor

    Re: Calender

    getLeap is still not right because the local variable days is not declared, and you don't return a value. Here's how it should look:
    Code (Text):

    int getLeap (int year)
    {
        int days;
        if (!(year % 4) && (year % 100) || !(year % 400))
            days = 29;
        else
            days = 28;
        return days;
    }
     
    This means that it is supposed to print one month at a time, so its purpose is NOT to print all 12 months.
    You need to define the function so that it has another parameter, like so:

    Code (Text):

    int printMonth(int month, int startDay, int days)
    {
       // body of function
    }
     
    All I've done here is to add another parameter, month. One possible use for the days parameter is that if you happen to be printing the calendar for February, you'll display the right number of days in a leap year. Also I don't know why you have it returning a value. I would be inclined to write it as a void function, one that doesn't return a value.


    This expression will evaluate to a number in the range 0, 1, 2, ..., 5, 6, so your program could use this idea to determine what day of the week the first of the month falls on. It would have to "know" that 0 corresponds to Sunday, 1 to Monday, and so on, although I have seen some calendars that are arranged Monday, ..., Sunday instead of Sunday, ..., Saturday.

    Your printMonth function would also need to know how many days are in each month, and that could be done using an array - have you studied arrays yet?
    The steps are calling printMonth 12 times, once for each month.
    No, return <something> causes the function to send a value (<something>) back to the caller.

    Here's an example of a simple function that returns a number that is twice as large as the number passed to it.
    Code (Text):

    int doubleValue(int num)
    {
       return 2 * num;
    }
    You would call it this way:
    Code (Text):

    int value = 8;
    int result;

    result = doubleValue(value);
     
    After the call to doubleValue(), result will be set to 16.
     
  8. Jun 9, 2012 #7
    Thanks for the response.
    I made all the changes you mentioned.
    So does this look good so far?
    Code (Text):
    #include<stdio.h>

    int printMonth (int startDay, int days);
    int getLeap (int year);
    int getYear (void);
    int dayFormula (int year);

    int main (void)
    {
        int year, formula, leap, result, startDay, days;

        year = getYear ();
        formula = dayFormula (year);
        leap = getLeap (year);
        result = printMonth (startDay, days);
    }
    int getYear (void)
    {
        int year;
        printf("Type in a year: ");
        scanf("%d", &year);

        return year;
    }
    int dayFormula (year)
    {
        int formula;
        formula = (((year-1)*365)+((year-1)/4)-((year-1)/100)+((year-1)/400)+1)%7;
        return formula;
    }
    int getLeap (int year)
    {
        int days;
        if (!(year % 4) && (year % 100) || !(year % 400))
            days = 29;
        else
            days = 28;
        return days;
    }
    void printMonth (int month, int startDay, int days)
    {

    }
     
    At line 15 where it says result = printMonth, it says the void value isn't being ignored like it should be, so I got rid of it.

    So now it prints 28 in 1991 and 29 in 1992, which is correct. How do I implement that into the calender?

    We just started on that. Would I do something like this...
    a[7] = 31
    b[4] = 30
    c[??] = ???
    For how many months have how many days? I'm not sure what to do with February here.
    That's confusing. How does the first function know what "value" is?
     
  9. Jun 9, 2012 #8

    Mark44

    Staff: Mentor

    If you implement printMonth as a void function, then it is an error to have the call to printMonth on the right side of an assignment statement. A void function does not represent a value; it causes something to happen.

    You wouldn't use three separate arrays; you would use one array with 12 elements, like this:

    Code (Text):
    int monthDays[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
     
    Alternatively, you could have an array with 13 elements, with 0 in the first (index 0) element.
    Code (Text):
    int monthDays[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
     
    Using the 2nd array, April is the 4th month, and monthDays[4] == 30. You wouldn't use monthDays[0].

    If the year is a leap year you wouldn't use the value in the array; you would use 29 instead.
    It doesn't. In the call to doubleValue, the actual parameter (value) is evaluated (8) and is passed in. The local variable num is set to 8, and 16 is returned.
     
  10. Jun 9, 2012 #9
    I'm at a point where I don't know what to do. I made all those functions and I don't know how to connect them, I don't know how to use the array, I don't know what else I can add to the program. This is a ridiculous project to expect of us.
     
  11. Jun 10, 2012 #10
    I'm just turning in what I have so far and get, maybe, partial credit. It's glaringly obvious this program is too hard for someone at my level. I'm thoroughly confused about what to do with this program.
     
  12. Jun 10, 2012 #11
    Well I did some research and tried something new. Can someone show me what all is wrong with this?

    Code (Text):
    #include <stdio.h>
    int monthNames (void)
    int main (void)
    {
        int monthDays[12]={31,28,31,30,31,30,31,31,30,31,30,31};
        int monthNames[12]={January,February,March,April,May,June,July,August,September,October,November,December};

    }
    int getYear (void)
    {
        int year;
        printf("Enter a year: ")
        scanf("%d", &year);
        return year;
    }
    int dayFormula (void)
    {
        int day;
        day = (((year-1)*365)+((year-1)/4)-((year-1)/100)+((year-1)/400)+1)%7;
        return day;
    }
    int leapYear (void)
    {
        int days;
        if (!(year % 4) && (year % 100) || !(year % 400))
            days = 29
        else
            days = 28
        return days;
    }
    int printMonth (int month, int startDay, int day)
    {
        for (month=0; month<=11; month++)
        {
            printf("%d", monthNames);
            printf("Sun Mon Tue Wed Thu Fri Sat Sun");

            for (startDay=0; startDay<=day; startday++)
                printf("%d", startDay);
        }
    }
     
  13. Jun 10, 2012 #12

    Mark44

    Staff: Mentor

    Yes, that's really the crux of this problem. It needs to be clear in your mind what needs to happen to display the calendar. To do this, you need to have an algorithm in mind, and from the algorithm you write the code that implements that algorithm.

    Start small - print a calendar for the month of January. It should start at the right day of the week for the year whose calendar you are printing. Starting at whatever is the right day of the week, print the dates for the rest of that week, and then print a new line character. After that, print the dates for the next week, and another new line character. Continue for the remaining weeks of January. Use the array to know when the last date of that month is.

    Next step - print the calendar for the month of February. You will need to start it at the appropriate day of the week so the the 1st of Feb. comes right after the 31st of January. The only tricky part is knowing whether to print 28 days or 29 days for February.

    If you can get the dates right for the first two months, the rest should be pretty easy.

     
  14. Jun 10, 2012 #13

    Mark44

    Staff: Mentor

    Your code here won't compile, since there are syntax errors. Also, even if there were no syntax errors, there wouldn't be any output, since doesn't have any executable statements - just a couple of declarations.

    Here are the syntax errors or errors in logic that I noticed.

    1. int monthNames (void)
    You are declaring monthNames as if it were a function that has no arguments.
    2. int monthNames (void)
    A declaration like this needs to end with a semicolon - ;
    3. int monthDays[12]={31,28,31,30,31,30,31,31,30,31,30,31};
    This statement is syntactically correct, but it's not useful. This array is a local variable whose scope is only inside main(). It is not visible to any other function. You should either make it a global variable (defined outside of main) or make it a local variable inside the function that uses it, printMonth.
    4. int monthNames[12]={January,February,March,April,May,June,July,August,September,October,November,December};
    This definition will generate compiler errors since the names January, February, etc. are not defined.
    The array should really be an array of strings, not int, and each string literal needs to be surrounded by double quotes, like this.
    char * monthNames[12]={"January","February","March", etc.};

    5. Just like the other array, the scope of monthNames is only the main function. The array should be global or else it should be defined in whatever function uses it.

    I gave some tips in my other post, for how you could proceed. Something I didn't mention there is comments - you don't have a single comment in your code. Each function should have a comment that explains what the function does, as well as what values it expects for the parameters, and what the function returns. The compiler doesn't care about comments, but using them helps you be clear on what you're trying to do.
     
  15. Jun 11, 2012 #14
    Thanks for all the help. I'll have to turn it in as is. This is the first time I've taken a class where it seems like I'm just too dumb to understand it.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook