# Thread: New to programming, writing a cubic formula calculator, having stupid/newbie issues

1. ## New to programming, writing a cubic formula calculator, having stupid/newbie issues

Hey guys.

Im trying to write a program to calculate the roots of a cubic using Cardanos formula. Im new to programming and C, i've been at this problem for about 4 hours or so. I struggled to get my head around function prototypes and I dont really think i've worked it out yet.

Firstly, if i omit the A1 = blah line, it compiles, but I get "Q = nan S = nan", i dont understand why its returning "not a number" for these values??

Secondly, i dont understand how to use the pow() operator on the A1 line.

Thirdly, it wont compile because i think i've stuffed up the use of the function. I dont quite understand whats going on.

Fourthly.. should it be int main(), or float main(), the values that are to be calculated could be floating point numbers, not integers.

Fifthly.. I want it all in terms of a = 1, hence my if (a != 1) line, i used an if statement to do this, but then i wasn't sure if i needed an else statement. cant i just remove the else statement? is that redundant? i just want it to test if a is equal to 1, and if it isnt, i want it to divide the other coefficents by a.

Thanks so much for reading this and thanks alot if you post.

Code:

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

float sign(float S); /* sign function prototype */

int main()
{
float Q, S, a, b, c, d, A1, x1, x2, x3, theta;

/* Get coefficients from user */

printf("Cubic coefficient a? ");
scanf("%f", &a);

printf("Cubic coefficient b? ");
scanf("%f", &a);

printf("Cubic coefficient c? ");
scanf("%f", &a);

printf("Cubic coefficient d? ");
scanf("%f", &a);

/* Make sure a = 1, if it doesn't, recalculate the rest of the coefficents */

if (a != 1)
{
a = 1;
b = b/a;
c = c/a;
d = d/a;
}
else
{
a = a;
b = b;
c = c;
d = d;
}

/* Now to do some calculations to be used later in the program */

Q = (b*b - 3.*c)/(9.0);
S = (2.*b*b - 9.*b*c + 27.*d)/(54.0);
A1 = -sign(S)((sqrt[S*S - Q*Q*Q] + fabs(S))^(1/3);
printf("The value of %f = Q, and %f = S is\n" Q, S);

/* calculations will be done here */

/* if Q^3 - S^2 > 0 will be done here */

/* if Q^3 - S^2 <= 0 will be done here */

}

float sign(float S)
{
if (S > 0)
return 1;
else if (S < 0)
return -1;
else
return 0;
}

2. 1. You're inputting a not b,c,d. Look in your scanf() function.
2. x raised to the power y=pow(x,y). Use this.
3.
float sign(float S);
You're returning float but actually 1,-1,0. So it should return int.
4. Whatever you do in your code, main's declaration is like this.
Code:
int main(void)
5. Your else statement is redundant, you can remove it without any issues.

3. First of all you should double check that you are actually assigning all your variables.... looks like you copy-and-pasted the scanf("%f", &a); a lot.

int main... is correct regardless of whether you are using floats in your program. It's unrelated.

4. Originally Posted by jdi
I struggled to get my head around function prototypes and I dont really think i've worked it out yet.
You are only asking for unnecessary confusion by focussing on some kind of math problem at the same time as you try to learn how to use (eg) function prototypes. Focus exclusively on writing and testing functions using the most basic elements you can -- eg, pass a number in, multiply by 2, return the number, print it out. Once you are comfortable with that, you can get more complicated with the math or whatever higher purpose you are trying to serve.

Secondly, i dont understand how to use the pow() operator on the A1 line.
pow() is not an operator, and it you don't have one on the A1 line. How would you like to use it?

Thirdly, it wont compile because i think i've stuffed up the use of the function. I dont quite understand whats going on.
When did you stop being able to compile it? You should not write so much without checking to make sure it will compile (this relates to what I said first off). You should write a couple of lines, use some kind of temporary printf() output to test the current state of affairs, then compile and run and keep repeating the process every few lines at this stage.

Anyway, what is the error: "Stuffed up use of function on line 12?"

Fourthly.. should it be int main(), or float main(), the values that are to be calculated could be floating point numbers, not integers.
It should always be int main and never anything else; main() returns an integer to the operating system at the end of the program (possibly indicating the outcome, etc). So it should never return a float or anything other than int. This has more or less nothing to do with the other variables. Make sure you include a return statement at the end of main() and that it does return an int (eg, "return 0" is fine).