# Need Help on Interpreting this Code

Page 1 of 1

## 6 Replies - 615 Views - Last Post: 15 January 2013 - 08:30 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=307308&amp;s=1364c7c74feef2ff0a053ec5fc0ef32b&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 trial123

Reputation: -1
• Posts: 5
• Joined: 14-January 13

# Need Help on Interpreting this Code

Posted 14 January 2013 - 11:16 PM

```int date_value(int day, int month, int year) {
year -= 1900;
int totval = year * 365;
totval += (year-1)/4;
totval -= (year-1)/100;
totval += (year+900)/1000;
switch(month) {
case(12): totval += 30;
case(11): totval += 31;
case(10): totval += 30;
case(9) : totval += 31;
case(8) : totval += 31;
case(7) : totval += 30;
case(6) : totval += 31;
case(5) : totval += 30;
case(4) : totval += 31;
case(3) : totval += 28;
if ( ((year%4)==0)&&(((year%100)!=0)||((year-100)%1000)==0) )
totval++;
case(2) : totval += 31;
}
totval +=day;
}

```

Here's my take on it:

// take
int date_value(int day, int month, int year) {
year -= 1900; //not sure
int totval = year * 365; // calculating the year total
totval += (year-1)/4; //not sure
totval -= (year-1)/100; //not sure
totval += (year+900)/1000; //not sure
switch(month) { // case 1 is the number of days in January, case 2 is the number of
case(12): totval += 30; // days in February, etc.
case(11): totval += 31;
case(10): totval += 30;
case(9) : totval += 31;
case(8) : totval += 31;
case(7) : totval += 30;
case(6) : totval += 31;
case(5) : totval += 30;
case(4) : totval += 31;
case(3) : totval += 28;
if ( ((year%4)==0)&&(((year%100)!=0)||((year-100)%1000)==0) ) //test if its a leap year
totval++; //adds days from previous months to
case(2) : totval += 31; // entered date
}
totval +=day; // not sure
}

[/code]

Is This A Good Question/Topic? 0

## Replies To: Need Help on Interpreting this Code

### #2 raghav.naganathan

• Perfectly Squared ;)

Reputation: 410
• Posts: 1,449
• Joined: 14-September 12

## Re: Need Help on Interpreting this Code

Posted 14 January 2013 - 11:31 PM

About the parts where you are not sure namely

```year- = 1900;
totval+ = day;
```

I shall explain this to you in a very simple way.

year- = 1900 can also be written as

```year= year -1900 ;
```

Consider year to be 2000
What that statement does is first, it calculates the value of year - 1900, in this case 2000-1900, and the result 100 is stored in the variable year.

Similarly for totval+ = day;

It can be written as
```totval= totval+ day;
```

The value of totval + day is stored in the variable totval.

regards,
Raghav

This post has been edited by raghav.naganathan: 14 January 2013 - 11:31 PM

### #3 trial123

Reputation: -1
• Posts: 5
• Joined: 14-January 13

## Re: Need Help on Interpreting this Code

Posted 15 January 2013 - 12:07 AM

Ah I see. So what you're saying is that "1900" is just an example. But why specifically use 1900? Is it because its not a leap year and that the case is only for years that are not leap years?

### #4 raghav.naganathan

• Perfectly Squared ;)

Reputation: 410
• Posts: 1,449
• Joined: 14-September 12

## Re: Need Help on Interpreting this Code

Posted 15 January 2013 - 01:04 AM

Well, you are right about 1900 not being a leap year...but I don't know why 1900 was specifically used in your code.

Also your case statements need to be written like this.

``` switch(month) {
case 12: totval += 30;

case 11: totval += 31;

case 10: totval += 30;
.
.
.

}

```

regards,
Raghav

This post has been edited by raghav.naganathan: 15 January 2013 - 01:05 AM

### #5 trial123

Reputation: -1
• Posts: 5
• Joined: 14-January 13

## Re: Need Help on Interpreting this Code

Posted 15 January 2013 - 01:30 AM

Alright. Well I tried compiling the code even without:

```year -= 1900;
```

and
```totval += (year+900)/1000;
```

Still works. And thanks I'll change the switch statements then.

### #6 raghav.naganathan

• Perfectly Squared ;)

Reputation: 410
• Posts: 1,449
• Joined: 14-September 12

## Re: Need Help on Interpreting this Code

Posted 15 January 2013 - 01:51 AM

Well, yes that would work as you have a variable called year as your formal parameter in your date_value() method.

regards,
Raghav

### #7 jimblumberg

Reputation: 5487
• Posts: 17,063
• Joined: 25-December 09

## Re: Need Help on Interpreting this Code

Posted 15 January 2013 - 08:30 AM

The reason for the 1900 is probably because this code was only designed to work for dates within the 20th century (1900 - 1999).

I suggest you start doing your own work. Copying code from someone else that you don't understand will not help you learn to program.

Jim

This post has been edited by jimblumberg: 15 January 2013 - 08:34 AM