Without using the derivative, you'd need to continue the loop without bounds, and have it end when fxnext is less than fx. I'd suggest initializing both of these so that fxnext is greater than fx, and then use that as your while condition, so that you can repeat it indefinitely (at least in theory). Something like this:
- int main(void)
-
{
-
int x=0;
-
int fx = 0;
-
int fxnext = 1;
-
-
printf("f(x) x.\n");
-
printf("-----\n");
-
-
while (fxnext > fx)
-
{
-
fx = (500*x) - (10*x*x);
-
x = x + 1;
-
fxnext = (500*x) - (10*x*x);
-
}
-
x = x - 1;
-
printf("%d %d\n",x,fx);
-
}
That should cause the loop to repeat until fxnext is less than fx. This would make fx the max of the function (after the loop x is the value that gives fxnext, so you need to decrease it afterwards to).
The only problem is that if the function doesn't have a max, or it's very large, you would run into overflow problems with x (although you might be more likely to find overflow problems with fx and fxnext). Also, this assumes that the max of the function occurs at a positive x value. To find the max in either direction you may want to run a loop in both directions (one increasing x the other decreasing it), and then compare the maxes found by each loop. Although you'd still have the potential overflow problems.