Hi,

I seem to be having a problem with my code below, which I would like to be able to use to return 2 values from the result of a quadratic equation operation. The eReturn function takes care of negative square root values. I'm not getting any errors back, just both of my results as 0.

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

void eReturn()
{
    printf("\nOne or both of your answers is complex.\n");
    return;
}

void QuadFormula(float a, float b, float c)
{
    float x1, x2, y, dis, *d, *e;


    dis = ((b*b)-(4*a*c));

    if (dis<0)
    eReturn();

    else if (dis>0)

    {
        y = sqrt(dis);

        x1 = (-b+y)/(2*a);
        x2 = (-b+y)/(2*a);

        d = &x1;
        e = &x2;
    }
}



int main()
{
    float a, b, c, *d, *e, result1, result2;


    printf("Please enter your value for a: ");
    scanf("%f", &a);

    printf("\nPlease enter your value for b: ");
    scanf("%f", &b);

    printf("\nPlease enter your value for c: ");
    scanf("%f", &c);

    QuadFormula(a, b, c);

    result1 = *d;
    result2 = *e;

    printf("\nThe results of the calculation  (%.0fx^2 + %.0fx + %.0f) are %f and %f", a, b, c, result1, result2);
}

Any help would be appreciated :)

Collin

void functions like eReturn, don't return anything, so putting the word "return" in there, is completely superflous.

And you can only return one item from a function. If you need more, I'd send it to the function as a pointer in the parameter. Say I want to return i and sum. I could do it this way:

int myIvariable, sum=0;

myIvariable = myFunction(&sum); //sending the address of sum
//now myIVariable has the value of i from myFunction(), and sum has a new value, also.
printf("myIvariable: %d,  sum: %d \n",myIvariable, sum);

int myFunction(int *sum) { //*sum is now the sum amount and "sum" is a pointer
   int i;
   for(i=0;i<22;i++) //a simple for loop
      *sum += i;

   return i;
}

If that doesn't help you, run through a C tutorial (there are scads of them on the net), because this is a very important concept in C.

Hi, Collin.

The variables *d and *e are never passed passed into the function QuadFormula.

The main program calls the subroutine, and assigns *d and *e to result1 and result2, without ever passing them in to the routine.

Also, I would recommend initializing variables before using them. That way you are always starting from a known quantity. As the programs you write get bigger and more complicated, it is better to have them initialized than have to face an "Undefined Variable" error later on.

hi colin,i think this would be help

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

int main()
{
    float A,B,C,test;
    float x1,x2;

    printf("This program will solve the quadratic equations\n");

    printf("\nEnter the integer value a,b and c for the equations Ax^2 + Bx + C\n\n\n");

    printf("Enter the value of integer A : \n\n");
    scanf("%d", &A);
    printf("Enter the value of integer B : \n\n");
    scanf("%d", &B);
    printf("Enter the value of integer C : \n\n");
    scanf("%d", &C);

    test=(B*B)-(4*A*C);//to know if it valid or not

    if(test<0)
    {
        printf("This equations do not have the root\n\n\n");
    }

    else if (test==0)

    {
        x1=-B/(2*A);
        printf("\n\nThe value of X is : %.2f\n\n", x1);
    }

    else
    {
        x1=(-B + sqrt(test)/(2*A));

        x2=(-B - sqrt(test)/(2*A));

        printf("\n\nThe values of X is %.2f and %.2f\n\n",x1,x2);

    }

    getchar();



}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.