Question:
How to write this C code?
2008-02-26 21:52:32 UTC
I have the following function prototype:
int ForeignTimeToEastern (int hour, int adjustment);

I need to write C code that converts London time, Stockholm, etc to eastern time using that prototype. Here's more info:

* London time is 5 hours ahead of Eastern Time
* Stockholm time is 6 hours ahead of Eastern Time
*Tampere and Helsinki time is 7 hours ahead of Eastern Time
* St. Petersburg time is 8 hours ahead of Eastern Time

Please help!! thanks
Four answers:
Bill M
2008-02-26 22:08:52 UTC
I'd use something like shown below



int ForeignTimeToEastern (int hour, int adjustment)

{

int temptime;

int timefactor;



switch (adjustment)

{

case 0: timefactor=-5; // london time

break;

case 1: timefactor=-6; // stockhom time

break;

case 2: timefactor=-7; // tampere and helsinki

break;

case 3: timefactor=-8; // st. pete

}



temptime=time+timefactor;

//need to check for time out of range

if (temptime <= 0) temptime = temptime + 12; //or 24 if you are using 24 hour time



return (temptime);

}
Craig R
2008-02-26 22:53:52 UTC
I'm not sure what "adjustment" is supposed to represent, but it looks to me like it is the offset from the foreign time to eastern time. So the function is easy:



int ForeignTimeToEastern(int hour, int adjustment)

{

return (hour + adjustment) % 24;

}



So for London call ForeignTimeToEastern(hour, -5). Etc.
BalRog
2008-02-26 22:16:25 UTC
If you use BillM's code you will get the wrong answer in every case except case 3 (St. Petersburg). The reason is that he did not include "break" statements at the end of his cases, so the execution will just continue into the next case, whether the "adjustment" matches that case or not. So, regardless of the "adjustment" value, the final value of the "timefactor" variable will always be "-8".



To fix, change as follows:



case 0: timefactor=-5; // london time

break;

case 1: timefactor=-6; // stockhom time

break;

case 2: timefactor=-7; // tampere and helsinki

break;

case 3: timefactor=-8; // st. pete

break;



Furthermore it is really error-prone to NOT have a "default" case to handle erroneous values of "adjustment".



-------------

EDIT: it looks like he changed his code to include the "break" statements I suggested. Still no "default" case though. Maybe he'll catch that in the next edit.
?
2016-05-24 05:04:59 UTC
I will assum that you have a terminal window, or you are sitting in front of a linux computer. After you have logged in you will either be facing a terminal window or a desktop. If you see a desktop find a way to start a terminal. In the terminal you need to use a text editor. For many years people have argued which is better vi or emacs, but now there are others. In the terminal on the command line type "vi helloworld.cpp" or "emacs helloworld.cpp" or "xemacs helloworld.cpp" or "vim helloworld.cpp" or "gedit hellowrold.cpp" or "nano helloworld.cpp". All of these are example of text editor in unix. Gedit is like windows notepad, and there are menus to save files. In vi and vim it isn't as obvious, it is the one that I use the most because you don't have to waste time touching your mouse. Whe vi starts you will see a blank screen press i to be in interactive mode. Type your code, and hist esc : w q this escapes you from interactive mode, and then writes and quite vi or vim. In xemacs and emacs you can use the mouse like notepad. In nano the commands are on the bottome of the screen. I hope this helps.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...