Write a C function to calculate the roots of the quadratic equation. The function must use two pointer parameters, one to receive the coefficients a, b, and c, and the other to send the roots to the calling function
Write a C function to calculate the roots of the quadratic equation. The function must use two pointer parameters, one to receive the coefficients a, b, and c, and the other to send the roots to the calling function
1: #include<stdio.h>
2: #include<conio.h>
3: void solve(int*,float*);
4: int main()
5: {
6: int a[3];
7: float *root;
8: clrscr();
9: printf("Enter the value of A : ");
10: scanf("%d",&a[0]);
11: printf("Enter the value of B : ");
12: scanf("%d",&a[1]);
13: printf("Enter the value of C : ");
14: scanf("%d",&a[2]);
15: solve(a,root);
16: if(root==NULL)
17: printf("The root is not possible\n");
18: else if(root[0]==root[1])
19: printf("The root is %.2f\n",root[0]);
20: else
21: printf("The roots are %.2f & %.2f\n",root[0],root[1]);
22: getch();
23: return 0;
24: }
25: void solve(int *a,float *r)
26: {
27: float d;
28: d=a[1]*a[1]-4*a[0]*a[2];
29: if(d<0)
30: {
31: r=NULL;
32: }
33: else if(d==0)
34: {
35: r[0]=r[1]=-a[1]/(2*a[0]);
36: }
37: else
38: {
39: r[0]=-a[1]/(2*a[0]);
40: r[1]=-a[1]/(2*a[0]);
41: }
42: }
No comments:
Post a Comment