简体   繁体   中英

Declarations, Definitions and Calls

i need to gain a better understanding of function definition, declarations and proper calls using this program. I really need the understanding of how to use them. Could you show me the proper way to write this program with all three correct and explained?

#include <stdio.h>
#include <math.h>

quad_equation(float a, float b, float c);

int main()

{

    float a, b, c, determinant, r1,r2;

    printf("Enter coefficients a, b and c: ");

    scanf("%f%f%f",&a,&b,&c);

    determinant=b*b-4*a*c;

    if (determinant>0)

    {

        r1= (-b+sqrt(determinant))/(2*a);

        r2= (-b-sqrt(determinant))/(2*a);

        printf("Roots are: %.2f and %.2f",r1 , r2);

    }

    else if (determinant==0) { r1 = r2 = -b/(2*a);

    printf("Roots are: %.2f and %.2f", r1, r2);

    }


    else (determinant<0);

    {

    printf("Both roots are complex");

    }

    return 0;

I just solved this exact question here : (I guess this is a part of an assignment )

https://stackoverflow.com/a/19826495/1253932

Also looking at your code .. you never use the function quad equation .. also you haven't defined the type of the function ( int/void/float/char) etc.

For ease: ( here is the entire code ) -- ask me if you don't understand anything

#include <stdio.h>
 #include <math.h>

// function declarations

void twoRoots (float a,float b,float delta);
void oneRoot (float a,float b,float delta);

 int main (void)                        
    {
    //Local Declarations
    float a;
    float b;
    float c;
    float delta;

   // float solution;

    printf("Input coefficient a.\n");
    scanf("%f", &a);
    printf("Input coefficient b.\n");
    scanf("%f", &b);
    printf("Input coefficient c.\n");
    scanf("%f", &c);
    printf("%0.2fx^2 + %0.2fx + %0.2f\n", a, b, c);

    delta = (float)(b*b) - (float)(4.0 * a * c);

    printf("delta = %0.2f\n",delta);

    if (delta > 0){
         twoRoots(a,b,delta);
    }else if (delta == 0) {
         oneRoot(a,b,delta);
    }else if (delta < 0.0){
         printf("There are no real roots\n");
    }

    return 0;
} 

void twoRoots (float a,float b,float delta)
{

    float xOne;
    float xTwo;

    float deltaRoot;

    printf("There are two distinct roots.\n");
    deltaRoot = sqrt(delta);
    xOne = (-b + deltaRoot) / (2*a);
    xTwo = (-b - deltaRoot) / (2*a);
    printf("%.2f", xOne);
    printf("%.2f", xTwo);
} 



void oneRoot(float a,float b,float delta)
{

    float xOne;
  //  float xTwo;
   // float deltaRoot;

    printf("There is exactly one distinct root\n");
    xOne = -b / (2*a);
    printf("%.2f", xOne);

}

EDIT:

A slightly more optimized and better functioning code that I made from the above mentioned code:

http://pastebin.com/GS65PvH6

Edit2:

From your comments you try to do this:

printf("Enter coefficients a, b and c: ");
scanf("%f%f%f",&a,&b,&c);

This will fail if you input something like this: 121

Beacuse scanf will read the whole 121 into a and it will have nothing for b , c ( rather it will put \\n(enter) into b and undefined into c )

So use the scanf the way I have used it in my code

OK - this is full of problems! I attempt to point them out, and show what "better" looks like. I hope this helps.

quad_equation(float a, float b, float c);

This is probably intended to be a "function prototype". A prototype tells the compiler "I am going to use this function later, and this is how it needs to be called, and the type it returns". You did not specify a return type; probably you want to use int to say whether you found roots or not, and print out the result in the function. Better would be to pass space for two return values as a parameter:

int quad_equation(float a, float b, float c, float* x1, float* x2);

Now we can use the main program to get input/output, and let the function solve the problem:

int main(void) {
{
float a, b, c, r1, r2;
int n;

// here you get the inputs; that seems OK
printf("Enter coefficients a, b and c: ");
scanf("%f %f %f",&a,&b,&c);

// now you have to "call your function"
// note that I make sure to follow the prototype: I assign the return value to an int
// and I pass five parameters: the coefficients a, b, c and the address of two variables
// x1 and x2. These addresses will be where the function puts the roots
n = quad_equation(a, b, c, &r1, &r2);

// when the function returns, I can print the results:
printf("There are %d roots:\n", n);

// based on the value of n, I change what I want to print out:
if (n == 2) printf(" %f and ", r1);  // when there are two roots I print "root 1 and"
if (n > 0) printf("%f\n", r2);       // when there is at least one root, I print it
// note that if n == 0, I would skip both print statements
// and all you would have gotten was "There are 0 roots" in the output

}

int quad_equation(float a, float b, float c, float* x1, float* x2) {
// function that computes roots of quadratic equation
// and returns result in x1 and x2
// it returns the number of roots as the return value of the function

float determinant;

  determinant=b*b-4*a*c;

  if (determinant>0)
  {
    *x1 = (-b+sqrt(determinant))/(2*a);
    *x2= (-b-sqrt(determinant))/(2*a);
    return 2;
  }

if (determinant==0) { 
  *x1 = *x2 = -b/(2*a);
  return 1;
}

return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM