6 Replies - 333 Views - Last Post: 15 January 2013 - 08:30 AM Rate Topic: -----

#1 trial123  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • 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;
  return totval;
}






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
return totval; // returns total
}

[/code]

Is This A Good Question/Topic? 0
  • +

Replies To: Need Help on Interpreting this Code

#2 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • 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

Was This Post Helpful? 0
  • +
  • -

#3 trial123  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • 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?
Was This Post Helpful? 0
  • +
  • -

#4 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • 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

Was This Post Helpful? 0
  • +
  • -

#5 trial123  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#6 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • 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
Was This Post Helpful? 0
  • +
  • -

#7 jimblumberg  Icon User is online

  • member icon


Reputation: 4278
  • View blog
  • Posts: 13,433
  • 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

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1