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

This is a discussion on New to programming, writing a cubic formula calculator, having stupid/newbie issues within the C Programming forums, part of the General Programming Boards category; Hey guys. Im trying to write a program to calculate the roots of a cubic using Cardanos formula. Im new ...

  1. #1
    jdi
    jdi is offline
    Registered User
    Join Date
    Aug 2009
    Posts
    4

    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. #2
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    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.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  3. #3
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    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. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by jdi View Post
    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).
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My new ASUS Crosshair 2 Formula issues
    By VirtualAce in forum Tech Board
    Replies: 8
    Last Post: 03-02-2009, 07:47 PM
  2. writing a formula in C++
    By komalwaseem in forum C++ Programming
    Replies: 6
    Last Post: 10-06-2007, 01:02 PM
  3. Writing to two streams: va_arg issues
    By dwks in forum C Programming
    Replies: 2
    Last Post: 09-26-2007, 10:14 AM
  4. Code writing issues Part 2...
    By jaybo684 in forum C++ Programming
    Replies: 10
    Last Post: 08-01-2005, 08:28 AM
  5. Code writing issues..
    By jaybo684 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2005, 07:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21