-
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();
}
[code][/code]tagged by Salem
-
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.
-
>>Furthermore, what's up with your main function?
You mean this one? :
>>void main()
:rolleyes:
It should be declared like this:
>>int main(void)
and return 0; at the end.