Computer problems,Computer help
*AX SOFT>>>Programming & Design

How to write this C code?


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

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);
}

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.

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.

Tags
  General - Computers & Internet   Software   Security   Programming & Design   Facebook   Flickr   Google   MSN   MySpace
Related information
  • What's the command for writing francesca with s' having that thing above?

    艢艣艤艥拧 click start and goto search, search files for a program named character map, it will give you commands for any characters you want. best answer appreciated :)

    ...
  • NAND Gate question?

    a) K-MAP in SOP form is correct: AC'+A'C b) to make the circuit using NAND gates, you will need 5. A' and C' will require both inputs tied together (2 gates) then make A&#...

  • How do you solve the following problem recursively?

    I'm not familiar with Java functions (you need mod and a print statement that doesn't add a newline) . Formatting is probably a little screwed up...want it in python? public static vo...

  • Adobe AIR...just another widget platform?

    Because Adobe doesn't have any Widgets or gadget or konfabulator of it's own. This is a new trend and they want to get in on it. Simple as that.

    ...
  • I downloaded one .nrg file. How do i access it?

    Its actually a nero image file.. An image file is file that has all the files in a cd compiled in it. u need a virtual drive to mount this .nrg file, so that u can use it the best virtual ...

  • Is there a calls limit for Google AJAX search API and Youtube API?

    There is no call limit, however be aware that all AJAX web request take some time to process the request and return a response. You must allow some sort of small wait state or waiting mechanism wi...

  • C++ Leap Year program with "variable is being used without being initialized" and always return true problem

    I think there are two problems in your code. First, the warning is talking about this function char moreData(){ char carryOn; cout<<"Do you want to enter more data? (y/n) "; ...

  • How do i store data in order from visual basic6 to MS access?

    Do you have an index on this table? If not, create one in Access or use "select * from table order by Field1" as your adodc source.

    ...
  •  

    Categories--Copyright/IP Policy--Contact Webmaster