function returning days of the week

Discussion in 'C Programming' started by ssylee, Dec 31, 2007.

  1. ssylee

    ssylee Guest

    I need to write a function that would read in a byte that would return
    a number between 1 to 7, 1 being Sunday, 2 being Monday, etc. I want
    to return an actual string that says "Sunday", or "Monday", etc.
    corresponding to the number. I know that the best method to implement
    a lookup conversion table would be using switch(variable ) ... case
    x: .... structure. However, I may need to pass an array as one of the
    parameters in order to access the text itself. Is there anything
    inefficient in passing a character array as a parameter based on
    memory consumption on an embedded microprocessor system? Thanks.
     
    ssylee, Dec 31, 2007
    #1
    1. Advertising

  2. ssylee

    Richard Bos Guest

    ssylee <> wrote:

    > I need to write a function that would read in a byte that would return
    > a number between 1 to 7, 1 being Sunday, 2 being Monday, etc. I want
    > to return an actual string that says "Sunday", or "Monday", etc.
    > corresponding to the number. I know that the best method to implement
    > a lookup conversion table would be using switch(variable ) ... case
    > x: .... structure. However, I may need to pass an array as one of the
    > parameters in order to access the text itself. Is there anything
    > inefficient in passing a character array as a parameter based on
    > memory consumption on an embedded microprocessor system? Thanks.


    You can't pass arrays in C (unless you're being willfully counter-
    productive by stuffing it inside a struct); what you will be passing is
    a pointer to the first element. And no, that is not inefficient.

    Richard
     
    Richard Bos, Dec 31, 2007
    #2
    1. Advertising

  3. ssylee wrote:
    > I need to write a function that would read in a byte that would return
    > a number between 1 to 7, 1 being Sunday, 2 being Monday, etc. I want
    > to return an actual string that says "Sunday", or "Monday", etc.
    > corresponding to the number. I know that the best method to implement
    > a lookup conversion table would be using switch(variable ) ... case
    > x: .... structure.


    How do you "know" this? I think it is just flat wrong. Check the
    following code:

    #include <stdio.h>

    char *day_byte_to_str(int x)
    {
    static char *day[] =
    { "Error", "Sunday", "Monday", "Tuesday", "Wednesday",
    "Thursday", "Friday", "Saturday"
    };
    return (x < 1 || x > 7) ? day[0] : day[x];
    }

    int main(void)
    {
    int which;
    printf("Test of day_byte_to_str()\n");
    for (which = 0; which < 9; which++)
    printf("%d -> %s\n", which, day_byte_to_str(which));
    return 0;
    }

    [Output]
    Test of day_byte_to_str()
    0 -> Error
    1 -> Sunday
    2 -> Monday
    3 -> Tuesday
    4 -> Wednesday
    5 -> Thursday
    6 -> Friday
    7 -> Saturday
    8 -> Error

    > However, I may need to pass an array as one of the
    > parameters in order to access the text itself. Is there anything
    > inefficient in passing a character array as a parameter based on
    > memory consumption on an embedded microprocessor system? Thanks.


    You pass the address of the array, not the array. This is basic stuff.
    That is cheap. Copying a string literal into the array may have some
    small cost, but I can't believe that it is anything significant. If
    this is the greatest inefficiency in your program (and I doubt that
    considering what you "know"), then you have a very tight program indeed.
     
    Martin Ambuhl, Dec 31, 2007
    #3
  4. ssylee

    user923005 Guest

    On Dec 31, 1:10 am, ssylee <> wrote:
    > I need to write a function that would read in a byte that would return
    > a number between 1 to 7, 1 being Sunday, 2 being Monday, etc. I want
    > to return an actual string that says "Sunday", or "Monday", etc.
    > corresponding to the number. I know that the best method to implement
    > a lookup conversion table would be using switch(variable ) ... case
    > x: .... structure. However, I may need to pass an array as one of the
    > parameters in order to access the text itself. Is there anything
    > inefficient in passing a character array as a parameter based on
    > memory consumption on an embedded microprocessor system? Thanks.


    There is a C FAQ on Zeller's congruence.
    It is trivial to add 1 and do a table lookup.

    From the C-FAQ:
    20.31: How can I find the day of the week given the date?

    A: Use mktime() or localtime() (see questions 13.13 and 13.14,
    but
    beware of DST adjustments if tm_hour is 0), or Zeller's
    congruence (see the sci.math FAQ list), or this elegant code
    by
    Tomohiko Sakamoto:

    int dayofweek(int y, int m, int d) /* 0 = Sunday
    */
    {
    static int t[] = {0, 3, 2, 5, 0, 3, 5, 1, 4,
    6, 2, 4};
    y -= m < 3;
    return (y + y/4 - y/100 + y/400 + t[m-1] + d)
    % 7;
    }

    See also questions 13.14 and 20.32.

    References: ISO Sec. 7.12.2.3.

    I'll leave the obvious bit for you.
     
    user923005, Dec 31, 2007
    #4
  5. ssylee

    ssylee Guest

    Thanks for all the replies. I'll reply to each suggestion
    individually:

    Richard Bos: What I meant passing arrays in C is passing by reference.
    Martin Ambuhl: The compiler is complaining about "Too many
    initializer" whenever I tried compiling lines like:
    static char *day[] =
    { "Error", "Sunday", "Monday", "Tuesday", "Wednesday",
    "Thursday", "Friday", "Saturday" };
    user923005: I would love to use mktime() or localtime() but I'm afraid
    that the development suite that I'm using is so stripped down that
    they aren't supported either.
     
    ssylee, Jan 1, 2008
    #5
  6. ssylee wrote:

    > Martin Ambuhl: The compiler is complaining about "Too many
    > initializer" whenever I tried compiling lines like:
    > static char *day[] =
    > { "Error", "Sunday", "Monday", "Tuesday", "Wednesday",
    > "Thursday", "Friday", "Saturday" };


    I'm afraid your compiler is broken. In that case, it is impossible to
    offer any useful advice.
     
    Martin Ambuhl, Jan 1, 2008
    #6
  7. ssylee

    user923005 Guest

    On Dec 31, 10:03 pm, ssylee <> wrote:
    [snip]
    > user923005: I would love to use mktime() or localtime() but I'm afraid
    > that the development suite that I'm using is so stripped down that
    > they aren't supported either.


    If you can't get ahold of mktime() or localtime() how are you going to
    know what the time zone is or if daylight savings time is active or
    what the daylight savings time shift is or any of those other sorts of
    essential things.

    You can use Zeller's congruence on the raw y, m, d but it may not do
    what you want if you are missing information.
     
    user923005, Jan 1, 2008
    #7
  8. ssylee

    ssylee Guest

    I would have to reset the clock one hour ahead or scale back one hour
    manually on the real time clock that I'm programming. I forgot to
    mention that this is on a microcontroller system, so a lot of the
    standard C libraries are not associated with the development suite
    that I'm using. It sucks, but I don't have time to start over again on
    the tasks that I'm doing.
     
    ssylee, Jan 1, 2008
    #8
  9. ssylee

    ssylee Guest

    Below shows the code so far after some thinking and collaborations on
    getting around my problem.

    // This function converts day of the week in number to string
    void access_day(unsigned char number, char dayString[])
    {
    // Declaration and Initialization of Days of the Week string
    char sundayArray[] = { 'S', 'u', 'n', 'd', 'a', 'y'};
    char mondayArray[] = { 'M', 'o', 'n', 'd', 'a', 'y'};
    char tuesdayArray[] = { 'T', 'u', 'e', 's', 'd', 'a', 'y'};
    char wednesdayArray[] = { 'W', 'e', 'd', 'n', 'e', 's', 'd', 'a',
    'y'};
    char thursdayArray[] = { 'T', 'h', 'u', 'r', 's', 'd', 'a', 'y'};
    char fridayArray[] = { 'F', 'r', 'i', 'd', 'a', 'y'};
    char saturdayArray[] = { 'S', 'a', 't', 'u', 'r', 'd', 'a', 'y'};

    // index lookup table of the day strings
    /*uint indexArray[] = {&sundayArray, &mondayArray, &tuesdayArray,
    &wednesdayArray, &thursdayArray,
    &fridayArray,
    &saturdayArray};
    char* pDay = (char*) indexArray[0];*/

    char* pIndexArray[] = {&sundayArray, &mondayArray, &tuesdayArray,
    &wednesdayArray, &thursdayArray,
    &fridayArray,
    &saturdayArray};
    char* pDay = pIndexArray[number-1];


    }

    However, I'm wondering if I should make dayString a pointer instead of
    a char array and do this:

    dayString = pIndexArray[number-1];

    instead of:

    char* pDay = pIndexArray[number-1];

    to return the string of the actual day of the week rather than the
    number. Thanks!
     
    ssylee, Jan 1, 2008
    #9
  10. ssylee

    Default User Guest

    ssylee wrote:

    > I would have to reset the clock one hour ahead


    Please quote the previous message, with the quoted material trimmed
    down to the minimum necessary for reply. See the vast majority of other
    posts in the newsgroup, or the Google Groups help page on the subject.




    Brian
     
    Default User, Jan 1, 2008
    #10
  11. ssylee wrote:
    > Below shows the code so far after some thinking and collaborations on
    > getting around my problem.
    >
    > // This function converts day of the week in number to string
    > void access_day(unsigned char number, char dayString[])
    > {
    > // Declaration and Initialization of Days of the Week string
    > char sundayArray[] = { 'S', 'u', 'n', 'd', 'a', 'y'};

    etc.
    These are not strings.
    If you want strings, just use
    char sunday[] = "Sunday";
    etc.
    If your compiler barfs on this, or if you are correct in your earlier
    claim that
    > The compiler is complaining about "Too many
    > initializer" whenever I tried compiling lines like:
    > static char *day[] =
    > { "Error", "Sunday", "Monday", "Tuesday", "Wednesday",
    > "Thursday", "Friday", "Saturday" };

    then you are not using a C compiler and the language you are writing is
    not C. Perhaps someone in the embedded programming newsgroup or on some
    newsgroup or mailing list for the not-C compiler you are using can help you.
     
    Martin Ambuhl, Jan 1, 2008
    #11
  12. ssylee

    ssylee Guest

    On Jan 1, 1:18 am, Martin Ambuhl <> wrote:
    > ssylee wrote:
    > > Below shows the code so far after some thinking and collaborations on
    > > getting around my problem.

    >
    > > // This function converts day of the week in number to string
    > > void access_day(unsigned char number, char dayString[])
    > > {
    > > // Declaration and Initialization of Days of the Week string
    > > char sundayArray[] = { 'S', 'u', 'n', 'd', 'a', 'y'};

    >
    > etc.
    > These are not strings.
    > If you want strings, just use
    > char sunday[] = "Sunday";
    > etc.
    > If your compiler barfs on this, or if you are correct in your earlier
    > claim that> The compiler is complaining about "Too many
    > > initializer" whenever I tried compiling lines like:
    > > static char *day[] =
    > > { "Error", "Sunday", "Monday", "Tuesday", "Wednesday",
    > > "Thursday", "Friday", "Saturday" };

    >
    > then you are not using a C compiler and the language you are writing is
    > not C. Perhaps someone in the embedded programming newsgroup or on some
    > newsgroup or mailing list for the not-C compiler you are using can help you.


    Sorry for forgetting to quote the lines. I initially thought the
    readers may be reading the previous messages on the thread. In the
    meantime, I have posted a question on mikroC forums and hopefully will
    get some help soon. Thanks for all your help.
     
    ssylee, Jan 1, 2008
    #12
  13. "ssylee" <> wrote in message
    news:...
    > Below shows the code so far after some thinking and collaborations on
    > getting around my problem.
    >
    > // This function converts day of the week in number to string
    > void access_day(unsigned char number, char dayString[])
    > {
    > // Declaration and Initialization of Days of the Week string
    > char sundayArray[] = { 'S', 'u', 'n', 'd', 'a', 'y'};
    > char mondayArray[] = { 'M', 'o', 'n', 'd', 'a', 'y'};
    > char tuesdayArray[] = { 'T', 'u', 'e', 's', 'd', 'a', 'y'};
    > char wednesdayArray[] = { 'W', 'e', 'd', 'n', 'e', 's', 'd', 'a',
    > 'y'};
    > char thursdayArray[] = { 'T', 'h', 'u', 'r', 's', 'd', 'a', 'y'};
    > char fridayArray[] = { 'F', 'r', 'i', 'd', 'a', 'y'};
    > char saturdayArray[] = { 'S', 'a', 't', 'u', 'r', 'd', 'a', 'y'};
    >
    > // index lookup table of the day strings
    > /*uint indexArray[] = {&sundayArray, &mondayArray, &tuesdayArray,
    > &wednesdayArray, &thursdayArray,
    > &fridayArray,
    > &saturdayArray};
    > char* pDay = (char*) indexArray[0];*/
    >
    > char* pIndexArray[] = {&sundayArray, &mondayArray, &tuesdayArray,
    > &wednesdayArray, &thursdayArray,
    > &fridayArray,
    > &saturdayArray};
    > char* pDay = pIndexArray[number-1];
    >
    >
    > }
    >
    > However, I'm wondering if I should make dayString a pointer instead of
    > a char array and do this:
    >
    > dayString = pIndexArray[number-1];
    >
    > instead of:
    >
    > char* pDay = pIndexArray[number-1];
    >
    > to return the string of the actual day of the week rather than the
    > number. Thanks!


    How does your function know how many chars can fit into the passed-in
    dayString buffer?
    How does your function know how many chars to copy from the
    pIndexArray[number-1] arrays?
    How does your function return the number of chars that it filled into the
    dayString?

    I would use the advice that you have already been given about using pointers
    to strings (character arrays that are nul char terminated). This is not
    the only solution, but you have been given good advice.

    Paul
     
    Paul Heininger, Jan 1, 2008
    #13
  14. ssylee <> writes:
    [...]
    > Martin Ambuhl: The compiler is complaining about "Too many
    > initializer" whenever I tried compiling lines like:
    > static char *day[] =
    > { "Error", "Sunday", "Monday", "Tuesday", "Wednesday",
    > "Thursday", "Friday", "Saturday" };

    [...]

    Really? When I compile a source file containing just that exact
    declaration, I don't get any complaints (except "warning: `day'
    defined but not used" with some options). As others have said, if
    your compiler rejects it, then it's not a conforming C compiler.

    Normally the size of the array is inferred from the number of elements
    in the initializer. If your (non-conforming) compiler doesn't do
    this, changing "char *day[]" to "char *day[8]" might be a workaround;
    the resulting code would still be valid C. Consult your compiler's
    documentation.

    --
    Keith Thompson (The_Other_Keith) <>
    [...]
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Jan 1, 2008
    #14
  15. "Martin Ambuhl" <> wrote in message
    news:...
    > ssylee wrote:
    >> I need to write a function that would read in a byte that would return
    >> a number between 1 to 7, 1 being Sunday, 2 being Monday, etc. I want
    >> to return an actual string that says "Sunday", or "Monday", etc.
    >> corresponding to the number. I know that the best method to implement
    >> a lookup conversion table would be using switch(variable ) ... case
    >> x: .... structure.

    >
    > How do you "know" this? I think it is just flat wrong. Check the
    > following code:
    >
    > #include <stdio.h>
    >
    > char *day_byte_to_str(int x)
    > {
    > static char *day[] =
    > { "Error", "Sunday", "Monday", "Tuesday", "Wednesday",
    > "Thursday", "Friday", "Saturday"
    > };
    > return (x < 1 || x > 7) ? day[0] : day[x];
    > }
    >
    > int main(void)
    > {
    > int which;
    > printf("Test of day_byte_to_str()\n");
    > for (which = 0; which < 9; which++)
    > printf("%d -> %s\n", which, day_byte_to_str(which));
    > return 0;
    > }
    >
    > [Output]
    > Test of day_byte_to_str()
    > 0 -> Error
    > 1 -> Sunday
    > 2 -> Monday
    > 3 -> Tuesday
    > 4 -> Wednesday
    > 5 -> Thursday
    > 6 -> Friday
    > 7 -> Saturday
    > 8 -> Error

    snip
    Huh? The kid's asking for the real time.

    Maybe the above might be a way to shoe-horn it into C. C can't find time
    with both hands. If I had to write something sensitive in this respect I
    would pass it to C by virtue of a language that is on a first-name basis
    with computers who function as clocks.

    I am also unschur of what a switch(variable ) ... case
    >> x: .... structure.

    is.
    --
    tja
    and



    ----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
    http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
    ----= East and West-Coast Server Farms - Total Privacy via Encryption =----
     
    merl the perl, Jan 2, 2008
    #15
  16. ssylee

    Army1987 Guest

    ssylee wrote:

    > I need to write a function that would read in a byte that would return
    > a number between 1 to 7, 1 being Sunday, 2 being Monday, etc. I want
    > to return an actual string that says "Sunday", or "Monday", etc.
    > corresponding to the number. I know that the best method to implement
    > a lookup conversion table would be using switch(variable ) ... case
    > x: .... structure. However, I may need to pass an array as one of the
    > parameters in order to access the text itself. Is there anything
    > inefficient in passing a character array as a parameter based on
    > memory consumption on an embedded microprocessor system? Thanks.


    You don't even need a function.
    const char *wdays = { NULL, "Sunday", "Monday" /*etc.*/ }
    and you can use wdays. Or even, throw away the NULL and use wdays[i-1].

    --
    Army1987 (Replace "NOSPAM" with "email")
     
    Army1987, Jan 2, 2008
    #16
  17. merl the perl wrote:
    > "Martin Ambuhl" <> wrote in message
    > news:...
    >> ssylee wrote:
    >>> I need to write a function that would read in a byte that would return
    >>> a number between 1 to 7, 1 being Sunday, 2 being Monday, etc. I want
    >>> to return an actual string that says "Sunday", or "Monday", etc.
    >>> corresponding to the number. I know that the best method to implement
    >>> a lookup conversion table would be using switch(variable ) ... case
    >>> x: .... structure.


    [my reply snipped, being irrelevant to "merl the perl"'s response]

    > snip
    > Huh? The kid's asking for the real time.


    There is nothing in the question about "real time". It is about a
    function that will return a string representing the name of a day of the
    week given a number in the range [1 ... 7]. If you cannot read
    specifications better than that, go back to selling shoes.

    > Maybe the above might be a way to shoe-horn it into C. C can't find time
    > with both hands.


    The above is, of course, uninformed flame-bait. Go back to selling shoes.

    > If I had to write something sensitive in this respect I
    > would pass it to C by virtue of a language that is on a first-name basis
    > with computers who function as clocks.


    And that's not even English. Maybe you can't sell shoes, either.
     
    Martin Ambuhl, Jan 2, 2008
    #17
  18. Army1987 wrote:

    > You don't even need a function.
    > const char *wdays = { NULL, "Sunday", "Monday" /*etc.*/ }
    > and you can use wdays. Or even, throw away the NULL and use wdays[i-1].


    That's wrong. And the correct declaration
    static char *day[] =
    { "Error", "Sunday", "Monday", "Tuesday", "Wednesday",
    "Thursday", "Friday", "Saturday"
    when suggested by me three days ago was met with the reply
    > Martin Ambuhl: The compiler is complaining about "Too many
    > initializer" whenever I tried compiling lines like:
    > static char *day[] =
    > { "Error", "Sunday", "Monday", "Tuesday", "Wednesday",
    > "Thursday", "Friday", "Saturday" };
     
    Martin Ambuhl, Jan 2, 2008
    #18
  19. Martin Ambuhl <> writes:
    > Army1987 wrote:
    >> You don't even need a function.
    >> const char *wdays = { NULL, "Sunday", "Monday" /*etc.*/ }
    >> and you can use wdays. Or even, throw away the NULL and use wdays[i-1].

    >
    > That's wrong. And the correct declaration


    Yes, it needs to be "const char *wdays[]".

    > And the correct declaration
    > static char *day[] =
    > { "Error", "Sunday", "Monday", "Tuesday", "Wednesday",
    > "Thursday", "Friday", "Saturday"


    It's *a* correct declaration. Once the "[]" is added, I fail to see
    why your declaration is more correct. In fact, I'd declare it as

    const char *const wdays[] = ...

    Whether it should be static or not depends on how it's used. Using
    NULL rather than the string "Error" as an error marker seems
    reasonable.

    > when suggested by me three days ago was met with the reply
    >> Martin Ambuhl: The compiler is complaining about "Too many
    >> initializer" whenever I tried compiling lines like:
    >> static char *day[] =
    >> { "Error", "Sunday", "Monday", "Tuesday", "Wednesday",
    >> "Thursday", "Friday", "Saturday" };


    Which is probably a flaw in the OP's compiler; I suggested changing
    "[]" to "[8]" as a possible workaround.

    --
    Keith Thompson (The_Other_Keith) <>
    [...]
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Jan 2, 2008
    #19
  20. Keith Thompson wrote:
    > Martin Ambuhl <> writes:
    >> Army1987 wrote:
    >>> You don't even need a function.
    >>> const char *wdays = { NULL, "Sunday", "Monday" /*etc.*/ }
    >>> and you can use wdays. Or even, throw away the NULL and use wdays[i-1].

    >> That's wrong. And the correct declaration

    >
    > Yes, it needs to be "const char *wdays[]".
    >
    >> And the correct declaration
    >> static char *day[] =
    >> { "Error", "Sunday", "Monday", "Tuesday", "Wednesday",
    >> "Thursday", "Friday", "Saturday"

    >
    > It's *a* correct declaration. Once the "[]" is added, I fail to see
    > why your declaration is more correct.


    You yourself have noted that "const char *wdays" is wrong and "const
    char *wdays[]" is correct. I fail to see how in the world you can be
    obtuse enough not to see that mine is more correct than Army1987's.

    A more interesting question is why Army1987 would post such an obvious
    error three days after more correct (despite your inconsistent claim
    that you can't see why it is) answer had been posted.
     
    Martin Ambuhl, Jan 2, 2008
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Guest
    Replies:
    3
    Views:
    1,846
    Alexandre
    Dec 22, 2003
  2. Jack
    Replies:
    2
    Views:
    457
  3. Guest
    Replies:
    3
    Views:
    526
    Alexandre
    Dec 22, 2003
  4. Guest
    Replies:
    3
    Views:
    1,095
    Alexandre
    Dec 22, 2003
  5. SimonC
    Replies:
    13
    Views:
    338
    Dr John Stockton
    Jan 4, 2005
Loading...

Share This Page