-1
\$\begingroup\$

This is program that computes the minimum of the function \$y=(ax+b)^2\$. The program will find the value of \$x\$ that gives the minimum \$y\$. It will start with an initial value \$x\$ that you assign.

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int a, b, i;
    float threshold, xt1, xt0, l_r, x, min_y;
    threshold=0.001;
    l_r=0.01;
    xt0=500;
    scanf("%d",&a);
    scanf("%d",&b);
    xt1=xt0-l_r*2*a*(a*xt0+b);

    for (i=0;i<20;++i){
        while ((xt1-xt0)>threshold){
            xt1=xt1-l_r;
            return xt1;
        }
        while ((xt1-xt0)<threshold && (xt1-xt0)!=0){
            xt1=x;
            min_y=2*a*(a*x+b);
            printf("min y is %f",min_y);
            break;
        }
    }
    return 0;
}
\$\endgroup\$
  • 2
    \$\begingroup\$ Unfortunately, this appears to be off-topic here because this doesn't appear to be working as intended. Please note that Code Review is about improving existing, working code. Code Review is not the site to ask for help in fixing or changing what your code does. Once the code does what you want, we would love to help you do the same thing in a cleaner way! Please see our help center for more information. \$\endgroup\$ – Edward Oct 31 at 19:50
  • \$\begingroup\$ @Edward How is it not working? I don't see any specifics. It's closeable due to lack of context, for sure, but I don't see a not-working-as-intended. \$\endgroup\$ – Reinderien Nov 1 at 0:42
  • 2
    \$\begingroup\$ The line return xt1; is very suspect, don’t you think? \$\endgroup\$ – Edward Nov 1 at 0:45
  • \$\begingroup\$ @Edward Help me out, which combination of a and b will fail? \$\endgroup\$ – konijn Nov 1 at 13:44
  • \$\begingroup\$ @konijn: try a=1, b=-600. \$\endgroup\$ – Edward Nov 1 at 14:27
2
\$\begingroup\$

Firstly, the iterative approach is unnecessary, as we know the minimum must be at x = -b/a. I'll assume that the use of a quadratic function here is supposed to be illustrative of a more general function.

There's an unnecessary include of <stdlib.h>, so we can drop that. return 0; is not needed at the end of main(), so we can omit that too.

It's clearer to declare main() as taking no arguments: int main(void).

It's 20 years since local variables needed to be declared at the beginning of their enclosing scope; it's better to declare where first initialised if possible, to reduce the likelihood of accidentally using an uninitialised variable. This is a bug we have in this program, where we use x before it's ever initialised.

Always, always check the result of scanf() and family before assuming that assignments were made. If either of these fail, then we can't assume that a and b contain valid values:

if (scanf("%d%d", &a, &b) != 2) {
    fputs("Invalid inputs\n", stderr);
    return EXIT_FAILURE;   /* from <stdlib.h> */
}

As an aside, why do we only want integer values for a and b? I think they should be double (as should all the float variables).

A while loop that ends in an unconditional break or return can only ever run 0 or 1 times, so it's equivalent to an if statement, and should be written as such. In fact, why are we returning a float from main()?

The code disagrees with the description in printing the result:

        printf("min y is %lf",min_y);

Weren't we supposed to print the minimum x ordinate? (min_y is trivially always zero for y = (a​x + b)², isn't it?)

\$\endgroup\$
1
\$\begingroup\$

With numeric tasks that could yield a wide range of answers, more informative to use "%e" than "%f" - else small non-zero values appear as "0.000000".

// printf("min y is %f",min_y);
printf("min y is %e\n",min_y);
\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.