I want to program a discriminant, but I don't know how to do it.

Asked 2 years ago, Updated 2 years ago, 28 views

I want to define a discriminant, but I don't know what to do.
I defined it as follows.
If the discriminant D=b*b-4*a*c>0, it has two solutions, so num=2. If the discriminant D=0, it is complex, so num=1. If the discriminant D<0, it has no solution, so num=0, but it did not compile well.
I think there is no problem with int num_of_ans1a(), but I would like you to tell me what the problem is with int main().

 int num_of_ans1a(inta, intb, intc)
{
    int num;
    if(b*b-4*a*c>0){
        num = 2;
    } if(b*b-4*a*c=0){
        num = 1;
    } else{
        num = 2;
    }
    return num;
}

int main (void)
{
    intn;
    n = num_of_ans1a();
    printf("The result is %d.",n);

    return n;
}

c

2022-09-29 22:30

2 Answers

The reasons for the compilation error are as follows:

    The second if statement of the
  • function num_of_ans1a uses = but is considered an assignment operator and has failed.
    You must use == to compare to 0.k As Kunif replied.
  • When calling num_of_ans1a() in
  • main(), the argument (a,b,c) is not specified.

There are no compilation errors, but there are the following problems:

We defined num = 0 because there is no solution for the discriminant D<0

For the discriminant D<0, the else in the second if statement is num=2.

Also, when the first if statement is passed, the else clause of the second if statement is executed.
 Else must precede the second if.


2022-09-29 22:30

int num_of_ans1a() in the second if where (b*b-4*a*c=0) is a substitution rather than a conditional expression.

Wouldn't two = be (b*b-4*a*c==0)?

The n=num_of_ans1a(); call in intmain(void) does not specify any arguments.

Aren't these two the reasons why you can't compile?

addition:

By the way, it's easy to look up and discover by being included in your development environment (some IDE, editor, compiler) or by using customized extensions and Lint tools.

You don't need the questions themselves, or you can write more information and get answers when you ask them.


2022-09-29 22:30

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.