I have to write a function which takes in a year between 1900 and 1999 and returns as a number the first day of that year. Days of the week go from 0 - 6 ( Sunday = 0, Saturday = 6).
I am supposed to use a leap year test to make this function work. I am also meant to make use of the fact that 1/1/1900 was a Monday. At the moment the program returns the correct value for the year 1900, but for every other year i enter i get "9" which is an invalid value. Any help??
Here is my code so far:
Code:# include <stdio.h> int new_years_day(int day, int year); int no_of_days(int months, int year); int first_day(int day, int year, int months); int main(void) { int choice, day = 0, months = 0, year = 0; printf("Choose a function\n (1) new_years_day (2) no_of_days (3) first_day >> "); scanf("%d", &choice); if (choice == 1) new_years_day(day, year); else if (choice == 2) no_of_days(months, year); else if (choice == 3) first_day(day, year, months); else { printf("\nPlease enter a number between 1 and 3 >> "); scanf("%d", &choice); } return 0; } int new_years_day(int day, int year) { int i = 0, leap = 0; // ask user for year between 1900 and 1999 printf("Enter a year between 1900 and 1999 >> "); scanf("%d", &year); // check that user enters a valid year while (year < 1900 || year > 1999) { // print error message printf("You can only choose a year between 1900 and 1999!! >> "); scanf("%d", &year); } for ( i = day; day <= 6; day++) /* Problem: need to count no. of years after 1900 and loop between 0 and 6 the right number of times to get the right day */ // leap year check while ((year % 400 == 0) && (year % 4 == 0) && (year % 100 > 0)) leap = 1; // is a leap year // find correct day if (year == 1900) { day = 1; printf("\nThe first day of the year in %d was a %d (0 - Sunday, 6 - Saturday)\n", year, day); } else if (leap = 1) { day = day + 2; printf("\nThe first day of the year in %d was a %d (0 - Sunday, 6 - Saturday)\n", year, day); } else if (leap = 0) { day++; printf("\nThe first day of the year in %d was a %d (0 - Sunday, 6 - Saturday)\n", year, day); } return year, day; } int no_of_days(int months, int year) { int i, current, leap = 0; // ask user for year between 1900 and 1999 printf("Enter a year between 1900 and 1999 >> "); scanf("%d", &year); // check that user enters a valid year while (year < 1900 || year > 1999) { // print error message printf("You can only choose a year between 1900 and 1999!! >> "); scanf("%d", &year); } // ask user for a month between 1 and 12 printf("Enter a month between 1 and 12 >> "); scanf("%d", &months); // check that user enters a valid month while (months < 1 || months > 12) { // print error message printf("You can only choose a month between 1 and 12!! >> "); scanf("%d", &months); } return months, year; } int first_day(int day, int year, int months) { int i, current, leap = 0; // ask user for year between 1900 and 1999 printf("Enter a year between 1900 and 1999 >> "); scanf("%d", &year); // check that user enters a valid year while (year < 1900 || year > 1999) { // print error message printf("You can only choose a year between 1900 and 1999!! >> "); scanf("%d", &year); } // ask user for a month between 1 and 12 printf("Enter a month between 1 and 12 >> "); scanf("%d", &months); // check that user enters a valid month while (months <1 || months >12) { // print error message printf("You can only choose a month between 1 and 12!! >> "); scanf("%d", &months); } return day, year, months; }



LinkBack URL
About LinkBacks


