一个指针要同时接受三个变量,求指教!打印出来如下图.We know that theroots of a quadratic equation of the formax2 + bx + c = 0 are given by the following equations: x1 = ( -b + square – root ( b2 – 4ac ) )/ 2a ;
来源:学生作业帮助网 编辑:作业帮 时间:2024/06/14 20:06:06
一个指针要同时接受三个变量,求指教!打印出来如下图.
We know that theroots of a quadratic equation of the form
ax2 + bx + c = 0 are given by the following equations:
x1 = ( -b + square – root ( b2 – 4ac ) )/ 2a ;
x2 = ( -b - square – root ( b2 – 4ac ) )/ 2a ;
Write a function to calculate the roots.The functionmust use two pointer parameters,one to receive the coefficients a,b,and c,and the other to send the roots to the calling function.(Hint:function namesimilary to void answer(int *c,float *root))
你忘了一句话”数组即指针”.
只要把指针指向一个数组,就OK了.
代码如下.
#include<stdio.h>#include<math.h>
void answer(int* c, float* root)
{
char ch = 'A';
int k = 0;
float fTemp;
for (ch = 'A'; ch < 'D'; ch++)
{
printf("Enter the Value of %c : ", ch);
scanf("%d", &c[ch - 'A']);
}
k = c[1] * c[1] - 4 * c[0] * c[2];
if (k < 0)
{
printf("the root is not possible.\n");
}
else if (k == 0)
{
root[0] = root[1] = (-c[1]) / (2 * c[0]);
printf("the root is %.2f\n", root[0]);
}
else
{
fTemp = sqrt((double)k);
root[0] = (-c[1] + fTemp) / (2 * c[0]);
root[1] = (-c[1] - fTemp) / (2 * c[0]);
printf("the roots are %.2f and %.2f\n", root[0], root[1]);
}
}
void main()
{
int factor[3] = {0};
float root[2] = {0};
answer(factor, root);
}