Thread: pointer's problem

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    5

    pointer's problem

    please help me to correct this program
    i don't know why the answer in the final is incorrect!!!
    Code:
    #include<stdio.h>
    #include<math.h>
    #include<conio.h>
    void input()
    {
    float a,b,c;
    printf("Coefficient of X^2 = ");
    scanf("%f",&a);
    printf("Coefficient of X = ");
    scanf("%f",&b);
    printf("Constant term = ");
    scanf("%f",&c);
    }
    void quadratic(float a, float b, float c, float *d, float *root1, float *root2)
    {
    
    *d=pow(b,2)-(4*a*c);
    if (d>=0)
    {
    *root1=(-b+(sqrt(pow(b,2)-(4*b*c)))/(2*a));
    *root2=(-b-(sqrt(pow(b,2)-(4*b*c)))/(2*a));
    }
    }
    
    void output()
    {
    float d,root1,root2;
    if (d<0)
    printf("\nThe root of X are imaginary.");
    else if (d==0)
    printf("\nThe root of X is %g.",root1);
    else if (d>0)
    printf("\nThe roots of X are %g and %g.\n",root1,root2);
    }
    void main()
    {
    float a,b,c,d,root1,root2;
    input();
    quadratic(a,b,c,&d,&root1,&root2);
    output();
    
    }


    &#91;code]&#91;/code]tagged by Salem

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You're not passing anything to your output function. You're using localized, uninitialized variables in your output function to display the results. How about actually passing parameters to it instead of using local variables?

    Furthermore, what's up with your main function? X^2 = ? You don't give them X, so how are they supposed to input the square of X?

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>Furthermore, what's up with your main function?
    You mean this one? :
    >>void main()

    It should be declared like this:
    >>int main(void)
    and return 0; at the end.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with file handling (pointers)
    By hmk in forum C Programming
    Replies: 5
    Last Post: 09-19-2008, 10:03 AM
  2. Problem with pointers
    By kotoko in forum C Programming
    Replies: 3
    Last Post: 06-12-2008, 05:17 AM
  3. A problem with pointers
    By vsla in forum C Programming
    Replies: 2
    Last Post: 10-10-2007, 04:14 AM
  4. Returning pointer to array of pointers problem
    By jimzy in forum C Programming
    Replies: 15
    Last Post: 11-11-2006, 06:38 AM
  5. Problem writing swap using pointers
    By joshdick in forum C++ Programming
    Replies: 1
    Last Post: 02-29-2004, 10:06 PM