Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem with code in C :(
#1
Code:
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>

#define START_GREGORIAN_CALENDAR 1582
#define THURSDAY (0)
#define FRIDAY (1)
#define SATURDAY (2)
#define SUNDAY (3)
#define MONDAY (4)
#define TUESDAY (5)
#define WEDNESDAY (6)


//Function prototypes
int dayOfWeek(int doomsday, int leapYear, int month, int day);
int isLeapYear(int year);
int yearToDoomsday(int year);
int daysToNextThursday(int year, int month, int day);
int isValidYear(int year);

//Main function
int main(int argc, char* argv[]){

    int dayName = 0;
    int day = 0;
    int month = 0;
    int year = 0;
    int doomsday = 0;
    int leapYear = 0;

    printf("Enter a date after 1582 in dd mm yyyy form: \n");
    scanf("%d", &day);
    scanf("%d", &month);
    scanf("%d", &year);

assert(year >= START_GREGORIAN_CALENDAR);

    //Checking for leap year
    if(isValidYear(year)) {
        leapYear = isLeapYear(year);
        doomsday = yearToDoomsday(year);
        dayName = dayOfWeek(doomsday, leapYear, month, day);

        printf("\nThe day of the week is ");
        if(dayName%7 == 0){
            printf("Thurs");
        }
        if(dayName%7 == 1){
            printf("Fri");
        }
        if(dayName%7 == 2){
            printf("Satur");
        }
        if(dayName%7 == 3){
            printf("Sun");
        }
        if(dayName%7 == 4){
            printf("Mon");
        }
        if(dayName%7 == 5){
            printf("Tues");
        }
        if(dayName%7 == 6){
            printf("Wednes");
        }
        printf("day!\n");

        daysToNextThursday(year, month, day);

    }
    return EXIT_SUCCESS;
}

//Function to determine leap year

int isValidYear(int year) {

    int yearIsValid = 0;

    if (year >= START_GREGORIAN_CALENDAR){
        yearIsValid = 1;

        if(isLeapYear(year)){
            printf("\nThe year %d is a leap year.\n", year);
         } else {
            printf("\nThe year %d is not a leap year.\n", year);
         }
    } else {
         printf("Year is before 1582.\n");
    }

    return yearIsValid;
}

int isLeapYear(int year){
    int isLeap = 0;
    if(year%400 == 0){
        isLeap = 1;
    } else if(year%100 == 0){
        isLeap = 0;
    } else if(year%4 == 0){
        isLeap = 1;
    } else if(year%4 != 0){
        isLeap = 0;
    }

    return isLeap;
}

//Doomsday function

int yearToDoomsday(int year){
    int whatday  = 0;
    whatday = (TUESDAY) + (year) + (year/4) - (year/100) + (year/400);
    int doomsday;
    doomsday = (whatday%7);
    printf("\nThe doomsday is %d days from Thursday.\n", doomsday);

    return doomsday;
}

//Day of week function

int dayOfWeek(int doomsday, int leapYear, int month, int day){
    int doomsdate = 0;
    int dayofweek = 0;

    //Setting doomsday date
    if(month == 2){
        if(leapYear){
            doomsdate = 29;
        } else {
            doomsdate = 28;
        }
    } else if(month%2 == 0){
        doomsdate = month;
    } else if(month%2 != 0){
            if(month == 7){
                doomsdate = 11;
            } else if(month == 11){
                doomsdate = 7;
            } else if(month == 9){
                doomsdate = 5;
            } else if(month == 5){
                doomsdate = 9;
            } else if(month == 3){
                doomsdate = 7;
            } else if(month == 1){
                if(leapYear){
                    doomsdate = 4;
                } else {
                    doomsdate = 3;
                }
            }
    }

    if (day > doomsdate){
        dayofweek = 7 - doomsday + ((day-doomsdate)%7);
    } else if (day < doomsdate){
        dayofweek = 7 - doomsday + 7 - ((doomsdate - day)%7);
    } else {
        dayofweek = 7 - doomsday;
    }

    dayofweek = dayofweek%7 + 1;
    return dayofweek;
}

//Function to determine number of days till next Thursday

int daysToNextThursday (int year, int month, int day){
    int dayofweek;
    int daysToGo;
    daysToGo = (7 - dayofweek);
    printf("\nThere are %d days until next Thursday.\n", daysToGo);

    return daysToGo;
}
Hi intelligent Southperrians!
I'm currently taking a computing course and we've just been learning how to do basic things in C.
I'm supposed to write a program which determines the day of the week given a certain input, after working out whether or not it is a leap year and the doomsday for that year.
I've run into a problem with my code with the last part, it always prints out "There are 7 days until next Thursday" when it shouldn't ._.
Would anyone care to point out my mistake?
Thanks in advance Biggrin
Reply


Messages In This Thread
Problem with code in C :( - by Kashimiya - 2011-03-26, 06:32 AM
Problem with code in C :( - by SaptaZapta - 2011-03-26, 06:37 AM
Problem with code in C :( - by Kashimiya - 2011-03-26, 06:46 AM
Problem with code in C :( - by SaptaZapta - 2011-03-26, 06:59 AM
Problem with code in C :( - by Fiel - 2011-03-26, 08:08 PM
Problem with code in C :( - by Kashimiya - 2011-03-27, 02:01 AM

Forum Jump:


Users browsing this thread: 1 Guest(s)