Thread: Quadratic Using pointers

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    11

    Quadratic Using pointers

    Hello!

    I am working on a program that calculates the quadratic formula using pointers. Here is as far as I have gotten:

    Code:
    #include "stdafx.h"
    #include "stdio.h"
    #include "math.h"
    
    /*Function Prototype*/
     
    int quad_1(double a, double b, double c);
    int quad_2(double a, double b, double c);
    
    
    int main (void)
    {
    	double result1,result2;
    	double a,b,c;
    
    	printf("the equation is ax^2+bx+c=0,enter a value for a\n");
    	scanf("%lf",&a);
    	printf("enter a value for b\n");
    	scanf("%lf",&b);
    	printf("enter a value for c\n");
    	scanf("%lf",&c);
    	result1= quad_1(a,b,c);
    	result2= quad_2(a,b,c);
    printf("result here %f\n",result1);
    printf("result here %f\n",result2);
    	return 0;
    }
    int quad_1(double a,double b, double c)
    {
    	double x1;
    	if(((b*b)- 4*a*c >= 0)&& (a!=0))
    	{
    		x1 = sqrt((b * b) - (4 * a * c));
            x1 = (-b + x1) / (2 * a);
    		return x1;
    	}
        
    	else return -1;
    }
    int quad_2(double a,double b, double c)
    {
    	double x2;
    	if(((b*b)- 4*a*c >= 0)&& (a!=0))
    	{
    		x2= sqrt((b * b) - (4 * a * c));
    		x2 = (-b - x2) / (2 * a);
    		return x2;
    	}
    	else return -1;
    }

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Good for you. Did you have a question?
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You can't return a double from a function that's designated as returning int, like quad_1. Are you supposed to give a fourth argument (specifically double *result) for the result to be stored in?

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    11
    yes, sorry I forgot to put my question. I do not think my pointers are correct because I need an output and it just seems like a function

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    11
    yes, I need a place where results are stored!

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So pass it in.
    Code:
    int quad_1(double a, double b, double c, double *result);
    (Or, if you're Elysia,
    Code:
    int quad_1(double a, double b, double c, double* result);
    )

  7. #7
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    or

    Code:
    int quad_1(double a, double b, double c, double * result);
    That way everyone loves you!

  8. #8
    Registered User
    Join Date
    Oct 2008
    Posts
    11
    where would I put that in the function prototype?

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    That is the function prototype.

  10. #10
    Registered User
    Join Date
    Oct 2008
    Posts
    11
    Quote Originally Posted by tabstop View Post
    That is the function prototype.
    what about quad2?

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Why wouldn't it be exactly the same?

  12. #12
    Registered User
    Join Date
    Oct 2008
    Posts
    11
    i am asking because (26) : error C2660: 'quad_1' : function does not take 3 arguments
    quad_2' : function does not take 3 arguments

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    No, they don't. So put 4 freakin' arguments inside the parentheses when you call the function already.

  14. #14
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Well that was interesting ... A question about pointers in a program that doesn't contain a single pointer variable! Is there actually something about pointers you'd like to ask us?

    The good news is that there's plenty of room for optimisation .
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  15. #15
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    if the functions simply returned double, it would work, right?
    Code:
    double quad_1(double a,double b, double c)
    ... in the prototypes as well as the actual function headers.
    But the OP's question seemed to imply this may be a pointers exercise. So the four argument way is the solution. Until it's made clearer what the parameters of the homework is.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 05:59 AM
  2. Replies: 4
    Last Post: 12-10-2006, 07:08 PM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM