f
i need a program that uses selection but avoids using arrays and structures.
Expert Answer
- View comments (4)
CODE:
#include <stdio.h> const char* dayOfWeek(int day); int getFirstDay(int year, int day); void getFinalDate(int occurence, int day, int year); int main() { int year, day, occurence; printf("Enter year number -> "); scanf("%d", &year); printf("Enter day of week number -> "); scanf("%d", &day); printf("Enter occurence number -> "); scanf("%d", &occurence); getFinalDate(occurence, day, year); return 0; } const char* dayOfWeek(int day) { switch(day) { case 0: return "Sunday"; case 1: return "Monday"; case 2: return "Tuesday"; case 3: return "Wednesday"; case 4: return "Thursday"; case 5: return "Friday"; case 6: return "Saturday"; } } int getFirstDay(int year, int day) { int firstDay = ((year - 1) * 365) + ((year - 1) / 4) - ((year - 1) / 100) + ((year - 1) / 400); firstDay = (firstDay % 7) + 1; for(int i = 0; i < 7; i++) if((firstDay + i) % 7 == day) return i + 1; } void getFinalDate(int occurence, int day, int year) { int requiredDay = getFirstDay(year, day) + ((occurence - 1) * 7), feb = 28; if((!(year % 4) && (year % 100)) || !(year % 400)) feb = 29; switch(1) { case 1: if(requiredDay <= 31) { printf("Desired occurence #%d of %s in %d: January %d", occurence, dayOfWeek(day), year, requiredDay); break; } requiredDay -= 31; case 2: if(requiredDay <= feb) { printf("Desired occurence #%d of %s in %d: February %d", occurence, dayOfWeek(day), year, requiredDay); break; } requiredDay -= feb; case 3: if(requiredDay <= 31) { printf("Desired occurence #%d of %s in %d: March %d", occurence, dayOfWeek(day), year, requiredDay); break; } requiredDay -= 31; case 4: if(requiredDay <= 30) { printf("Desired occurence #%d of %s in %d: April %d", occurence, dayOfWeek(day), year, requiredDay); break; } requiredDay -= 30; case 5: if(requiredDay <= 31) { printf("Desired occurence #%d of %s in %d: May %d", occurence, dayOfWeek(day), year, requiredDay); break; } requiredDay -= 31; case 6: if(requiredDay <= 30) { printf("Desired occurence #%d of %s in %d: June %d", occurence, dayOfWeek(day), year, requiredDay); break; } requiredDay -= 30; case 7: if(requiredDay <= 31) { printf("Desired occurence #%d of %s in %d: July %d", occurence, dayOfWeek(day), year, requiredDay); break; } requiredDay -= 31; case 8: if(requiredDay <= 31) { printf("Desired occurence #%d of %s in %d: August %d", occurence, dayOfWeek(day), year, requiredDay); break; } requiredDay -= 31; case 9: if(requiredDay <= 30) { printf("Desired occurence #%d of %s in %d: September %d", occurence, dayOfWeek(day), year, requiredDay); break; } requiredDay -= 30; case 10: if(requiredDay <= 31) { printf("Desired occurence #%d of %s in %d: October %d", occurence, dayOfWeek(day), year, requiredDay); break; } requiredDay -= 31; case 11:if(requiredDay <= 30) { printf("Desired occurence #%d of %s in %d: November %d", occurence, dayOfWeek(day), year, requiredDay); break; } requiredDay -= 30; case 12: if(requiredDay <= 31) { printf("Desired occurence #%d of %s in %d: Decemeber %d", occurence, dayOfWeek(day), year, requiredDay); break; } requiredDay -= 31; default: printf("Invalid Year"); } }
OUTPUT:
Here, I have written the program as required in the question. It is wokring correctly and I have posted the output also.
THANKS,
PLEASE UPVOTE THE ANSWER.
ليست هناك تعليقات:
إرسال تعليق