Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > function returning days of the week

Reply
Thread Tools

function returning days of the week

 
 
ssylee
Guest
Posts: n/a
 
      12-31-2007
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.
 
Reply With Quote
 
 
 
 
Richard Bos
Guest
Posts: n/a
 
      12-31-2007
ssylee <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
Martin Ambuhl
Guest
Posts: n/a
 
      12-31-2007
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.
 
Reply With Quote
 
user923005
Guest
Posts: n/a
 
      12-31-2007
On Dec 31, 1:10*am, ssylee <(E-Mail Removed)> 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.
 
Reply With Quote
 
ssylee
Guest
Posts: n/a
 
      01-01-2008
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.
 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      01-01-2008
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.
 
Reply With Quote
 
user923005
Guest
Posts: n/a
 
      01-01-2008
On Dec 31, 10:03*pm, ssylee <(E-Mail Removed)> 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.

 
Reply With Quote
 
ssylee
Guest
Posts: n/a
 
      01-01-2008
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.
 
Reply With Quote
 
ssylee
Guest
Posts: n/a
 
      01-01-2008
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!
 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      01-01-2008
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Calender control, how to display week days as English even windows region is Chinese? Jack ASP .Net 2 10-12-2005 01:28 AM
the penultimate week and last week of data for each month SimonC Javascript 13 01-04-2005 10:20 PM
ASP.NET: Day / Work Week / Week / Month web calendar control with view like MS Outlook ASP .Net Web Controls 3 12-22-2003 10:42 PM
ASP.NET: Day / Work Week / Week / Month web calendar control with view like MS Outlook ASP .Net 3 12-22-2003 10:42 PM
ASP.NET: Day / Work Week / Week / Month web calendar control with view like MS Outlook ASP .Net Building Controls 3 12-22-2003 10:42 PM



Advertisments