Challenge - How to start


#1

I’ve read through the chapter and I’ve read through everyone’s solutions. But I think I’m missing the point of the chapter based on everyone’s responses. I’m not even sure I know what question to ask.

I thought the point was to build structs and then use them. But it seems everyone built their app inside main.c. Am I overthinking this (cuz I never do that!)?

We have a struct for “now” but should we be building a struct for “later”? Or just building our result based on the structs that Aaron wrote already? And why is time (NULL)?

I think I figured out my question - how do you guys “think” this through? Getting started seems to be the hardest part for me (although I’ve been ok up through this chapter - using examples in the book).

Frustrated…

Pam


#2

I guess I’d ask myself whether any given challenge is phrased in such a way as to express an interest in the architecture of the solution.

Certainly it’s possible to design a modular solution for this challenge, but Apple provides much more flexible (and, admittedly, long-winded) solutions for generating dates than anything Aaron is asking for here, so this exercise seems to be strictly about getting the job done with the tools from the chapter.

Future chapters definitely have challenges that require more thought into designing a modular solution, but this doesn’t feel like one of them.


#3

I just viewed this chapter in particular as a lesson on how structs work and not much else. Remember these early chapters are about building up a layer of C knowledge to help us along later.


#4

I dove in and just tried a bunch of stuff, much of which failed, so I modified it till it worked.

One mini-challenge is, “How do I display the current date?”. It took me two or three tries to get this to work, because I glossed over the fact that tm_year is “years since 1900”, not since 1970.

I created a new struct, but I don’t know if that’s what I was supposed to do. In any case, the stuff I threw together seems to give at least sane results.

After this ran OK, I changed the “4000000” to “4” to see if THOSE results made sense, which they did.

int main (int argc, const char * argv[]) { long secondsSince1970=time(NULL); long someTimeLater=secondsSince1970+4000000; struct tm now; localtime_r(&secondsSince1970, &now); struct tm later; localtime_r(&someTimeLater, &later); printf("The current time was %d:%d:%d.\n",now.tm_hour, now.tm_min, now.tm_sec); printf("The current date was %.2d-%.2d-%.4d.\n",1+(now.tm_mon),now.tm_mday, (now.tm_year)+1900); printf("The date in 4 million seconds will be %.2d-%.2d-%.2d,\n", (later.tm_mon)+1, later.tm_mday, (later.tm_year)+1900); printf("The time in 4 million seconds will be %d:%d:%d.\n", later.tm_hour, later.tm_min, later.tm_sec); return 0; }


#5

just finished mine, it looks much less code than yours - I hope I got it right :question:

#include <stdio.h>
#include <time.h>

int main (int argc, const char * argv[])
{
    long secondsSince1970 = time(NULL);
    long dateAfter4Million = secondsSince1970 + 4000000;
    
    struct tm calDate;
    localtime_r(&dateAfter4Million, &calDate);
    
    printf("The date after 4 million seconds is %d-%d-%d\n", calDate.tm_mon +1 , calDate.tm_mday, calDate.tm_year + 1900);
    
    return 0;
}

#6

If anyone is looking for a quick way to check the correct answer I recommend using Wolfram Alpha.
.


#7

[quote=“untouchableforce”]If anyone is looking for a quick way to check the correct answer I recommend using Wolfram Alpha.
.[/quote]
Nice idea :wink: