Full credit for a working answer The total grade for the assignment is 100 point
ID: 3542237 • Letter: F
Question
Full credit for a working answer
The total grade for the assignment is 100 points, with 90 points for correctness and 10 points for documentation. You are required to write a C program that accepts two dates in the year 2013 and prints out the number of days which are strictly between the two specified dates. Each date will be specified by two integers, with the first integer representing the month and the second representing the day. Example: The two integers 2 and 10 represent the date February 10, 2013. Likewise, the two integers 3 and 7 represent March 7, 2013. The number of days that lie strictly between these two dates is 24. (Of these, 18 are in February and the remaining 6 are in March.) As another example, if the first date is specified by the integers 10 and 17 (which represent the date October 17, 2013) and the second date is specified by the integers 10 and 18 (which represent the date October 18, 2013), the answer to be printed is 0. The outline for your program is as follows. Prompt the user for the first date. Read the date and check if it is valid. If it is not valid, print a suitable error message and stop. (Conditions for the validity of a date are given later in this handout.) Prompt the user for the second date. Read the date and check if it is valid. If it is not valid, print a suitable error message and stop. If the second date precedes the first date, print a suitable error message and stop. Compute the number of days which are strictly between the first and the second dates. Print the answer computed in Step 4 and stop.Explanation / Answer
#include<stdio.h>
#include<conio.h>
int check_date(int d,int m)
{
int month[] = {31,28,31,30,31,30,31,31,30,31,30,31};
if(d<0 || d > month[m-1])
return 0;
return 1;
}
int main()
{
int d1,m1,d2,m2;
int month[] = {31,28,31,30,31,30,31,31,30,31,30,31};
int sum =0;
printf("Enter day and month of year ");
scanf("%d %d",&m1,&d1);
if(!check_date(d1,m1))
{
printf("invalid date entered");
getch();
return 0;
}
printf("Enter day and month of year ");
scanf("%d %d",&m2,&d2);
if(!check_date(d2,m2))
{
printf("invalid date entered");
getch();
return 0;
}
if(m1==m2)
{
if(d1>d2)
{
printf("cant calculate days");
getch();
return 0;
} // end day loop
} // end month loop
if(m1>m2)
{
printf("cant calculate days");
getch();
return 0;
}
if(m1<m2)
{
for(int i=m1+1; i<m2-1; i++)
{
sum = sum + month[i-1];
}
printf("No of dates lies between these two dates is %d",sum+month[m1-1]-d1+d2-1);
}
else
{
printf("No of dates lies between these two dates is %d",sum-d1+d2-1);
}
getch();
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.