Page 1 of 1

Improving C++ code

Posted: Tue Sep 09, 2003 8:24 am
by Kon-Tiki
I've got this in my code for a program

Code: Select all

if(home.longi >= 0 && home.longi < 7.5) {
   home.zone = 0;
}
if(home.longi >= 7.5 && home.longi < 22.5) {
   home.zone = 1;
}
if(home.longi >= 22.5 && home.longi < 37.5) {
   home.zone = 2;
}
if(home.longi >= 37.5 && home.longi < 52.5) {
   home.zone = 3;
}
if(home.longi >= 52.5 && home.longi < 67.5) {
   home.zone = 4;
}
if(home.longi >= 67.5 && home.longi < 82.5) {
   home.zone = 5;
}
if(home.longi >= 82.5 && home.longi < 97.5) {
   home.zone = 6;
}
if(home.longi >= 97.5 && home.longi < 112.5) {
   home.zone = 7;
}
if(home.longi >= 112.5 && home.longi < 127.5) {
   home.zone = 8;
}
if(home.longi >= 127.5 && home.longi < 142.5) {
   home.zone = 9;
}
if(home.longi >= 142.5 && home.longi < 157.5) {
   home.zone = 10;
}
if(home.longi >= 157.5 && home.longi < 172.5) {
   home.zone = 11;
}
if(home.longi >= 172.5) {
   home.zone = 12;
}
I know it's long, but it was my last resort. I've tried everything I know, checked my tutorials at least five times, tried fiddling with commands I didn't know up till then, fiddled with old commands, tried making different calculations, some simple, some overly complicated, but it never did what it had to do.

Now I've come to the conclusion that the easiest way out is checking if a given value, inputted by the user (here stored in home.longi) is positioned between two set values. If it isn't, it should go on to the next two values, check if it's positioned there, etc. I tried some more tricks for it (thought I came to a solution using an array and a for-loop, but that seems to be completely the wrong way as well), so this's the only working code I managed to make. I know there must be an easier way to get this done, without all those ifs, but can't find it.

So, my question is: how can I improve this code so that it won't be so long and still do the same?
Thanks.

Edit: Forgot to say this: it should do it with target.longi on target.zone too, if that might help.

Re:Improving C++ code

Posted: Tue Sep 09, 2003 1:51 pm
by BI lazy
hm. Your problem is kinda intriguing:

you have to search a certain set of values out of a given array of them.

So why don't store the values in an array?

Say:

Code: Select all

float longis[]={0,10.5,20.5,30.5,40.5,50.5,60.5,..,n};
int addtime[]={0,1,2,3,4,5,6,7,8,9,10,11,12};
int max=7; //or the number of elements in your array - 1
int zone=0;

then for each test(home or target), you crawl throu this array: 

while(zone<(max-1)&&x>=longis[zone]&&x<longis[zone+1])
  zone++;

the index should indicate your desired time zone.
hour=hour+addtime[zone];

check it out, fiddle with it and if I am in err, tell it too. I 'm on too much coffee now to think straight on... but I hope I have grasped your problem right.

Re:Improving C++ code

Posted: Tue Sep 09, 2003 1:58 pm
by BI lazy
Waaah...

awful...complicated thinking. Of course, you can also simply add the index (zone) to your hour to get the value it has in your target time zone...

Re:Improving C++ code

Posted: Tue Sep 09, 2003 4:21 pm
by Schol-R-LEA
I agree with Beyond Infinity, in that table lookup is the way to go; but I'd go even further, by making it an external text file which the program reads at start up, and which can thus be edited without recompiling the program. Just a suggestion.