You are here:

C/C programming

Advertisement


Question
Dear sir,

Actually i am trying to solve this question.


"B. Write a program to enter two dates as shown, and then computes how many days there are in between
the two (including date B). The program should repeat itself until the user enters “0 0” as input for date B.
You may assume that there are 28 days in February (non-leap year). Check that your program produces the
number of days as shown below.
Enter date A: 14 1
Enter date B: 1 5
*** date A to date B: 107 days ***"

  I have an idea how to solve this but there is condition that solve this by only using "while, Do, If and If Else"
i designed my program acorrding to these conditions but it was to long. Like I enter more than 25 conditions which is annoying.
I want guide line how to solve this with out going prom so long.

Thanks
tajud Din

Answer
Hello Taj.
Please post a follow-up question and show me what you have done so far. I will be happy to help you but I will not be able to respond until much later today.

Best regards
Zlatko

Edit 1:

Taj, in this problem is A always before B so that if A is December 31 and B is January 1, the difference is just 1 day, crossing the year boundary, or do we assume both dates are always in the same year so that the difference between A and B is 364 days?


Edit 2:

Well, here is my attempt. I may be off by a day here or there. It's up to you to test it. You should make sure that the program does what you want. I spent very little time testing. Use at your own risk.

I suppose your instructor has nothing against breaking up the program into functions and using arrays.

#include <stdio.h>

/*
30 days hath September,
April, June and November,
All the rest have 31,
Excepting February alone
(And that has 28 days clear,
With 29 in each leap year).
*/

int getDaysInMonth(int month)
{
   /*
   The DaysPerMonth[] array is hidden in this function so that access to the
   array can be checked. Arrays start at index 0, and months start at 1.
   This function adjusts the month to the correct index and also checks that the
   index is in bounds.

   Another way of doing this is to have 13 elements in DaysPerMonth, with element 0
   unused. It depends on your personal preference.
   */

   /*                     J   F   M   A   M   J   J   A   S   O   N   D    */
   int DaysPerMonth[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

   --month;
   if (month < 0 || month > 11)
   {
       printf("Error, invalid month %d\n", month);
       return 0;
   }
   return DaysPerMonth[month];
}

int daysToStartOfThisYear(int month, int day)
{
   int result = 0;
   while(--month) result += getDaysInMonth(month);
   return result + day - 1;
}

int daysToEndOfThisYear(int month, int day)
{
   return 365 - daysToStartOfThisYear(month, day) - 1;
}

int calculate(int monthA, int dayA, int monthB, int dayB)
{
   /*
   Assumption:
   The first date (monthA, dayA) is earlier that the second date.
   A year boundary may be crossed */
   int result;
   int daysToStartA = daysToStartOfThisYear(monthA, dayA);
   int daysToStartB = daysToStartOfThisYear(monthB, dayB);

   if (daysToStartB < daysToStartA)
   {
       /* Based on assumption, the second date is in the next year */
       result = daysToEndOfThisYear(monthA, dayA) + daysToStartB;
   }
   else
   {
       result = daysToStartB - daysToStartA;
   }
   return result;
}

void prompt(const char* text, int* month, int* day)
{
   fputs(text, stdout);
   fflush(stdout);
   fscanf(stdin, "%d %d", day, month);
}

void test(int monthA, int dayA, int monthB, int dayB)
{
   printf("Difference between (D:%d, M:%d) and (D:%d, M:%d) is %d days\n",
       dayA, monthA, dayB, monthB,
       calculate(monthA, dayA, monthB, dayB));
}

int main(void)
{
   printf("Days from start to Jan 1 = %d\n", daysToStartOfThisYear(1, 1));
   printf("Days from start to Dec 31 = %d\n", daysToStartOfThisYear(12, 31));
   printf("Days to end from Jan 1 = %d\n", daysToEndOfThisYear(1, 1));
   printf("Days to end from Dec 31 = %d\n", daysToEndOfThisYear(12, 31));
   test(1, 14, 5, 1);
   test(5, 1, 1, 14);

   while(1)
   {
       int monthA;
       int dayA;

       int monthB;
       int dayB;

       prompt("Enter Date A (Day Month):", &monthA, &dayA);
       if (monthA == 0 || dayA == 0) return 0;

       prompt("Enter Date B (Day Month):", &monthB, &dayB);
       if (monthB == 0 || dayB == 0) return 0;

       test(monthA, dayA, monthB, dayB);
   }

   return 0;
}  

C

All Answers


Answers by Expert:


Ask Experts

Volunteer


Zlatko

Expertise

No longer taking questions.

Experience

No longer taking questions.

Education/Credentials
No longer taking questions.

©2012 About.com, a part of The New York Times Company. All rights reserved.