d - ahmad alhayek

d

 f

Problem: Given as input the year, day of the week number (0 [Sunday] through 6 [Saturday]), and occurrence number, determine

i need a program that uses selection but avoids using arrays and structures.

((year – 1)365+ [year -]- year - 17 [year - 1 + 100 400 -- % 7 The formula determines the day based on the values as shown be

Show transcribed image text
View comments (1) 

Expert Answerinformation icon

  • Vishal Singh's Avatar

    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:

    Enter year number -> 2021 Enter day of week number -> 1 Enter occurence number -> 11 Desired occurence #11 of Monday in 2021:

    Enter year number -> 2020 Enter day of week number -> 6 Enter occurence number -> 9 Desired occurence #9 of Saturday in 2020:

    Enter year number -> 2021 Enter day of week number -> 5 Enter occurence number -> 1 Desired occurence #1 of Friday in 2021: J

    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.

    View comments (4) 

ليست هناك تعليقات:

إرسال تعليق

ahmad alhayek تصميم ahmad alhayek جميع الحقوق محفوظة 2016

صور المظاهر بواسطة sndr. يتم التشغيل بواسطة Blogger.