|
That works out pretty good, thanks.
My current favourite word is: Delicious!
-SK Genius
Game Programming articles start - here[ ^]-
|
|
|
|
|
Hello,
previously i have posted in wrong forum.
Problem is - User can input any number, program need to take decision based on inputs.
Let suppose input is stored in variable n;
based on value of n some function will be called.
program logic can be -
if(n > 1 && n < 10 )
callfun1();
if(n > 11 && n < 20 )
callfun2();
if(n > 25 && n < 30 )
callfun3();
if(n > 33 && n < 38 )
callfun4();
if(n > 1 && n < 10 )
callfun5();
.
.
.
and so on
if(n > minlimit && n < maxlimit )
callfunX();
Is there any easy way to avoid if else chain to do similar work.
Or is there any way to change the limits in if condition at some central place something using #define.
Please provide your inputs/help to help me finding other ways to optimize programming.
Thank
Vikas
vicky
|
|
|
|
|
You could arrange them in such a way that they effectively do a binary search. That wouldn't bring the total number of tests down but it would run faster (singe every time execution flows through it it can only take 1 path, so O(log n) steps instead of O(n))
Or you could fill a giant array with function pointers for O(1) time.
Or maybe you could change the functions to not need to be selected anymore (using funny bit magic if necessary) but whether that's possible depends on the function of course.
|
|
|
|
|
a little help is the use of the keyword else
if(n > 1 && n < 10 )
{
callfun1();
}
else if(n > 11 && n < 20 )
....
and sort it so that the most likely cases coming first.
AFAIK the compiler resolves switch in a similar way.
Best is the use of enums.
Press F1 for help or google it.
Greetings from Germany
|
|
|
|
|
You ignored the responses you got on this question the first time. Reposting is rude. Perhaps you can explain why you ignored the previous responses and still reposted the same question.
|
|
|
|
|
I am very sorry for my re-post.
I have re-posted my question because at other discussion thread i was getting responses in terms of C# and .Net, but i need response in terms of pure C.
I have not done it intentionally, because of web page movement by mistake i've clicked at C# discussion link and posted the question instead of C/C++ category.
Thank
Vikas
vicky
|
|
|
|
|
vikasvds wrote: I am very sorry for my re-post.
Ok, but have you begun to understand that OOD/OOP has solutions to writing conditional code?
|
|
|
|
|
You probably need to take a step back, and think about what you're trying to achieve, and lay it out in a more logical pattern. Perhaps you can convert the value 'n' into a unique parameter for a switch statement.
There's also nothing wrong with using "else" so I'm not sure why you'd want to avoid it...
There are three kinds of people in the world - those who can count and those who can't...
|
|
|
|
|
molesworth wrote: There's also nothing wrong with using "else" so I'm not sure why you'd want to avoid it...
Here are some quotes from Kent Beck in his book Implementation Patterns[^]
If/then and switch statements are the simplest form of instance –specific behavior….
The more paths through a program the less likely the program is to be correct….The proliferation of conditionals reduces reliability….This problem is compounded when conditionals are duplicated.
These problems can all be eliminated by converting the conditional logic to messages, either with subclasses or delegation.
|
|
|
|
|
led mike wrote: The more paths through a program the less likely the program is to be correct
Aye, that's very true, and anything that can reduce complexity will also reduce the likelihood of bugs, and improve readability and, more importantly, testability.
However, sometimes there's no way to improve on an if/else construct, and when used sensibly they're the right tools for the job. When you get into a morass of nested if/elseif/elseif... blocks, then you certainly do introduce problems.
Sheesh! Next thing you'll be telling me "goto" is a bad idea
There are three kinds of people in the world - those who can count and those who can't...
|
|
|
|
|
I wouldn't subclass a if .
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
Why not? It's simple
class MyIf : public if
{
};
IT'S FRIDAY!!!!!
|
|
|
|
|
And now saturday here :pizza: :whisky:
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
Wow, why would someone vote this a '1'?
|
|
|
|
|
|
molesworth wrote:
There's also nothing wrong with using "else" so I'm not sure why you'd want to avoid it...
Adding else s to this code would dramatically change it's flow. Note that for n in a range (1,10) all callfunc s would be called.
Greetings - Jacek Gajek
|
|
|
|
|
Have you something personal against if-else ?
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
CPallini wrote: Have you something personal against if-else?
Yes, i don't like if-else
I hate them
vicky
|
|
|
|
|
Try
#define WHATEVER_YOU_LIKE_AS_IF if
#define WHATEVER_YOU_LIKE_AS_ELSE else
and then, for instance
WHATEVER_YOU_LIKE_AS_IF (i>0 && i<10)
WHATEVER_YOU_LIKE_AS_ELSE WHATEVER_YOU_LIKE_AS_IF (i>11 && i<20)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
You can try something like this
int func1()
{
return 0;
}
int func2()
{
return 0;
}
typedef int (*funcptr)();
struct condition
{
int low;
int high;
funcptr func;
};
condition arr[] = {
{ 10, 20, func1 },
{ 20, 30, func2 } // Can add more data here
};
int n=17;
for (int i=0;i<2;++i)
{
if (n > arr[i].low && n < arr[i].high)
{
arr[i].func();
// If you want you can break here
}
}
-Arun
|
|
|
|
|
Thank you very much all of your response,
The approach Arun has suggested is i think the way i was looking for as best and fasted approach.
Thanks Arun to help me suggesting this approach, it's really nice and definitely fastest method uses full potential of C/C++ language.
Thanks again.
Vikas
vicky
|
|
|
|
|
One more advantage of this method i can change limits any time at one location for any if condition.
So to change the limits i need to see only at one place.
Thanks
Vikas
vicky
|
|
|
|
|
Hai!
Using MessageBox or AfxMessageBox we are able to display some message to the user during runtime, the message may include the value returned from a function etc.
Can we do the other way ??
I want the user should enter one byte data during run time, depending on the entered value i want to proceed my calculation .
Is it posible in MFC, if yes how to do it !!
Thanks!
|
|
|
|
|
kapardhi wrote: I want the user should enter one byte data during run time, depending on the entered value i want to proceed my calculation .
Is it posible in MFC, if yes how to do it !!
So why not just create a dialog box with an edit control?
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
|
|
|
|
|
Is there no other way other than creating a dialog !!!
Any inbuilt messagebox !!
thanks!
|
|
|
|