# How to figure out my solution approach is the right path?

#1

I started solving chapter 10’s challenge and I ended up using a totally different, time consuming, and unelegant approach though luckily it did produced the correct answer.
When I checked the solution on this website I realized there is a quicker and more elegant solution.
How can I make myself realize I’m going down the wrong path during coding so I don’t waste time and write better efficient code?
I’m a total newbie and I appreciate if someone can provide some tips on improving.
By the way, I agree with everyone that this is a great book - well written, easy to read, and fun to learn with.

int main(int argc, const char * argv[])
{
long secondsSince1970 = time(NULL);
printf(“It has been %ld seconds since 1970\n\n”, secondsSince1970);

``````struct tm now;
localtime_r(&secondsSince1970, &now);
printf("The time is %d month - %d day - %d year, %d hours : %d minutes: %d seconds.\n\n",
now.tm_mon+1, now.tm_mday, now.tm_year+1900 , now.tm_hour, now.tm_min, now.tm_sec);

int leap;
int currentyear;
int nextyear;
currentyear=(now.tm_year+1900);
nextyear=(now.tm_year+ (1900+1));
if (    ((currentyear % 100) == 0) && ((currentyear % 400) == 0)   )
{
leap = 1;
printf ("The current year is a leap year. February will have a 29th day.\n");

} else if (  ((nextyear % 100) == 0) && ((nextyear % 400) == 0)  ) {

printf("The next year is a leap  year. February will have a 29th day.\n");

} else {
leap = 0;
printf("The current year or the next year are not leap years.\n\n");
}

printf("4 mil secs from today is %d days.\n", adddays);

printf("4 mil secs from today is %d hours.\n", addhours);

printf("4 mil secs from today is %d minutes.\n", addmins);

//Thirty days hath September,
//April, June and November
//All the rest have Thirty-one
//Except February, which has Twenty-eight
//or Twenty-nine on leap years

int currentmonth;
currentmonth = (now.tm_mon+1);
int currentday;
currentday = (now.tm_mday);
int stagingnewmonth;
int monthyearcalc;
int newmonth;
int newday;
int newyear;

if ((currentmonth == 4) || (currentmonth == 6) || (currentmonth == 9) || (currentmonth == 11) ) {

if ( stagingnewmonth > 12 ) {
//to count for cases of adding more than 12 months like in several years 24+ months for example
monthyearcalc= stagingnewmonth / 12;
newyear = currentyear + monthyearcalc;
newmonth = stagingnewmonth - (12 * monthyearcalc);
}

} else if ( (currentmonth == 2) && (leap == 0) ){

if ( stagingnewmonth > 12 ) {
//to count for cases of adding more than 12 months like in several years 24+ months for example
monthyearcalc= stagingnewmonth / 12;
newyear = currentyear + monthyearcalc;
newmonth = stagingnewmonth - (12 * monthyearcalc);
}

} else if ( (currentmonth == 2) && (leap == 1) ) {

if ( stagingnewmonth > 12 ) {
//to count for cases of adding more than 12 months like in several years 24+ months for example
monthyearcalc= stagingnewmonth / 12;
newyear = currentyear + monthyearcalc;
newmonth = stagingnewmonth - (12 * monthyearcalc);
}

} else {

if ( stagingnewmonth > 12 ) {
//to count for cases of adding more than 12 months like in several years 24+ months for example
monthyearcalc= stagingnewmonth / 12;
newyear = currentyear + monthyearcalc;
newmonth = stagingnewmonth - (12 * monthyearcalc);
}
}

printf("Current time plus 4 million secs later is %d month - %d day - %d year, %d hr: %d min: %d secs.\n",

return 0;
``````

}

#2

This comes with practice. Some people need more practice, some need less. Also you need to have an open mind, be willing to learn from mistakes, and from the work of others. Last but not least, it pays to learn some computer science: especially, data structures, algorithms, and parsing.

#3

Thanks ibex. I will keep reading more and practicing more to gain more experience. Can you also help me with my solution in NSArray topic? Can you check if I did it correctly? My solution is different from others. Not sure if I approached it correctly.