I am using C language UNIX program will only be tested with meaningful integer d
ID: 3927993 • Letter: I
Question
I am using C language UNIX
program will only be tested with meaningful integer data with years only in the range from 1800 to 2016
I am not allowed to use repetition Thank you !
Problem: Given a year and a day number determine how long until the next Tuesday occurs in the specified year. See problem #60 on page 300 of your C programming text for some useful formulas Example Execution #1 : (The date January 10, 2016 occurs on a Sunday so the next Tuesday is in two days) Enter the year 2016 Enter the day number [1 - 366] 10 The day number entered (10) is a Sunday. The next Tuesday will occur in 2 days. Example Execution #2: (No more Tuesdays occur in that year on or after the given day number.) Enter the year 2015 Enter the day number [ - 365]: 365 The day number entered (365) is a Thursday. There are no more Tuesdays remaining in the year. Example Execution #3: Enter the year 2013 Enter the day number [1 - 365] 365 The specified day number (365) is a Tuesday Example Execution #4: Enter the year: 2016 Enter the day number [ - 366]: 300 The day number entered (300) is a Wednesday. The next Tuesday wil1 occur in 6 days Example Execution #5: Enter the year 2016 Enter the day number [1 - 366]: 49 The day number entered (49) is a Thursday The next Tuesday will occur in 5 days Example Execution #6: Enter the year: 2016 Enter the day numbe [1-366]: 1Explanation / Answer
#include <stdio.h>
#include <string.h>
int day_week(int day,int mon,int year)
{
int md[] = { 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4 };
if(mon<3)
{
year = year-1;
}
int ans = ( year + year/4 - year/100 + year/400 + md[mon-1] + day) % 7;
return ans;
}
int fleap(int year)
{
int leap=0;
if(year%400==0)
{
leap=1;
}
else if(year%100==0)
{
leap=0;
}
else if(year%4==0)
{
leap=1;
}
return leap;
}
int find_mon(int yday,int year)
{
int leap=fleap(year);
int m_day[] = {31,29,31,30,31,30,31,31,30,31,30,31};
if(leap==0)
{
m_day[1]=28;
}
int count=0,mon=-1,i;
for(i=0;i<12;i++)
{
count=count+m_day[i];
if(count>=yday)
{
mon=i;
break;
}
}
return mon;
}
int find_mday(int mon,int yday,int year)
{
int leap=fleap(year);
int m_day[] = {31,29,31,30,31,30,31,31,30,31,30,31};
if(leap==0)
{
m_day[1]=28;
}
int count=0,mday=-1,i;
for(i=0;i<12;i++)
{
count=count+m_day[i];
if(count>=yday)
{
mday=yday-(count-m_day[i]);
break;
}
}
return mday;
}
int main()
{
int year,yday,leap;
printf("Enter the year: ");
scanf("%d",&year);
leap = fleap(year);
printf("Enter the day number[1-%d]: ",365+leap);
scanf("%d",&yday);
int mon = find_mon(yday,year);
int mday = find_mday(mon,yday,year);
int d_week = day_week(mday,mon+1,year);
//printf("***%d %d %d**** ",mon+1,mday,d_week);
char *a[] = {"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
int ans = (9-d_week)%7;
if(ans==0)
{
printf("The specificed day number(%d) is a Tuesday. ",yday);
}
else
{
printf("The day number entered(%d) is a %s ",yday,a[d_week]);
}
if(yday+ans>365+fleap(year))
{
printf("There are no more Tuesdays in this year. ");
}
else if(ans!=0)
{
printf("The next Tuesday will occur in %d days ",ans);
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.