Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
March 31st, 2013, 12:06 AM
 benjy1994
Registered User

Join Date: Mar 2013
Posts: 2
Time spent in forums: 56 m 59 sec
Reputation Power: 0
Code Problem (Days between Dates) Using Loops

I am very new at programming and i lack general skills in understanding how programs work. But i really want to make this program work.

as i said i am not very good at programming, so if you are not willing to help me, then this specific thread is not for you.

My programs has three main functions:

1: to Convert Calendar date to julian Dating
2: to convert julian dating to Calendar Dating

These two i was able to figure out ; but the third one has some very problematic logic on it and it need a lot of work... in my opinion

The Third function is to figure out how many days there are between two calendar dates.

many people tell me it is easier if i just convert the whole date into julian dating but i want to figure out this out using my algorithm since I like loops.
Code:
```/*	Program Name: date.c

Compiler: Dev-C++ 4.9.9.2
OS: Windows 7 Ultimate X64
*/

#include <stdio.h>

int main(void)
{

do

{
int monthc=1, dayc=1, jan=28;
int convyear=0,convmonth=0,convday=0,convjdayc=0,convjday=0, convmonth2=0, convday2=0, convyear2=0;

printf("DATE SELECTION MENU\n\n1) Convert Calendar date into Julian date\n2) Convert Julian date into Calendar date\n3) Compute days between two Calendar dates\n4) Exit program\n\nENTER SELECTION (1 - 4):");

case 1:
printf("\n\n\nConverting from calendar date into Julian date\n\n\nEnter Date(mm/dd/yyyy):");
scanf("%d/%d/%d", &convmonth, &convday, &convyear);

if(convmonth!=1){
if((convyear % 4) != 0){

for(monthc=1;monthc<convmonth;monthc++){

if (monthc<=7){

convjdayc=convjdayc+30;
if(monthc==2){
convjdayc=convjdayc-2;
}
if(monthc%2!=0){
convjdayc++;
}

}
if (monthc>=8){

convjdayc=convjdayc+31;
if(monthc%2!=0){
convjdayc--;
}

}
}
}
else{
for(monthc=1;monthc<convmonth;monthc++){
if (monthc<=7){
convjdayc=convjdayc+30;
if(monthc==2){
convjdayc=convjdayc-1;
}
if(monthc%2!=0){
convjdayc++;
}

}
if (monthc>=8){
convjdayc=convjdayc+31;
if(monthc%2!=0){
convjdayc--;
}

}
}
}
}
convjday=convday+convjdayc;
printf("\nJulian Date: %d %d\n\n\n", convjday, convyear);
break;
/* -------------------------------------CASE 1 END--------------------------------------*/

case 2:

printf("\n\n\nConverting from Julian date into Calendar date\n\n\nEnter Julian Date(ddd/yyyy):");
scanf("%d/%d", &convjday, &convyear);

if((convyear % 4) == 0){
jan=29;
}

for(dayc=1;dayc<=convjday;dayc++){

if (dayc>jan&&monthc==2){
convjday=convjday-jan;
dayc=1;
monthc++;
}
if(dayc>30&&monthc%2==0&&monthc<=7){
convjday=convjday-30;
dayc=1;
monthc++;
}
if(dayc>31&&monthc%2!=0&&monthc<=7){
convjday=convjday-31;
dayc=1;
monthc++;
}
if(dayc>31&&monthc%2==0&&monthc>=8){
convjday=convjday-31;
dayc=1;
monthc++;
}
if(dayc>30&&monthc%2!=0&&monthc>=8){
convjday=convjday-30;
dayc=1;
monthc++;
}
}

dayc--;

printf("Calendar Date:%d/%d/%d\n\n\n", monthc, dayc, convyear);
break;
/* -------------------------------------CASE 2 END--------------------------------------*/
case 3:

printf("\n\n\nCompute days between two Calendar dates\n\n\nEnter First Date(mm/dd/yyyy):");
scanf("%d/%d/%d", &convmonth, &convday, &convyear);
printf("\nEnter First Date(mm/dd/yyyy):");
scanf("%d/%d/%d", &convmonth2, &convday2, &convyear2);

if(convyear2<=convyear){
for(convday2;convday2<=convday||convmonth2<=convmonth||(convmonth2>=convmonth&&convyear2<=convyear);convday2++){

if((convyear2 % 4) == 0){jan=29;}else{jan=28;}

if (convday2>jan&&convmonth2==2){

convday2=1;
convmonth2++;
}
if(convday2>30&&convmonth2%2==0&&convmonth2<=7){

convday2=1;
convmonth2++;
}
if(convday2>31&&convmonth2%2!=0&&convmonth2<=7){

convday2=1;
convmonth2++;
}
if(convday2>31&&convmonth2%2==0&&convmonth2>=8){

convday2=1;
convmonth2++;
}
if(convday2>30&&convmonth2%2!=0&&convmonth2>=8){

convday2=1;
convmonth2++;
}

if(convmonth2==12){ convyear2++; convmonth2=1;}

dayc++;

}
}

if(convyear<convyear2){
for(convday;convday<=convday2||convmonth<=convmonth2||(convmonth>=convmonth2&&convyear<=convyear2);convday++){

if((convyear % 4) == 0){jan=29;}else{jan=28;}

if (convday>jan&&convmonth==2){

convday=1;
convmonth++;

}
if(convday>30&&convmonth%2==0&&convmonth<=7){

convday=1;
convmonth++;
}
if(convday>31&&convmonth%2!=0&&convmonth<=7){

convday=1;
convmonth++;
}
if(convday>31&&convmonth%2==0&&convmonth>=8){

convday=1;
convmonth++;
}
if(convday>30&&convmonth%2!=0&&convmonth>=8){

convday=1;
convmonth++;
}

if(convmonth==12){ convyear++; convmonth=1;}

dayc++;

}
}

printf("Days Between Dates:%d\n\n\n\n", dayc);
break;

/* -------------------------------------CASE 3 END--------------------------------------*/

}

getchar();

}
```

Notice that i have put case statements to separate each function.

the third function finds the number of days between dates to the accuracy of 600 days to 1,000 days proportional to the range, so if you look up days between 2,000 and 3,000 its accuracy would be off by almost 30,000 days, but within the 300,000 and 400,000 days.

Example RUN:

From 3/4/2002 (m/d/y)
to 8/23/2009 (m/d/y)
is: 2729 days

My Programs calculates: 2879

#2
March 31st, 2013, 01:24 AM
 salem
Contributed User

Join Date: Jun 2005
Posts: 4,261
Time spent in forums: 2 Months 4 Weeks 1 Day 15 h 13 m 24 sec
Reputation Power: 1827
Copy again from your code editor so indentation is preserved.
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper

#3
March 31st, 2013, 02:18 AM
 benjy1994
Registered User

Join Date: Mar 2013
Posts: 2
Time spent in forums: 56 m 59 sec
Reputation Power: 0
sorry my bad about the code, i did not know how to do it

 Viewing: Dev Shed Forums > Programming Languages > C Programming > Code Problem (Days between Dates) Using Loops