C程序查找根错误
我正在用C编写一个具有以下规范的函数:C程序查找根错误,c,function,imperative-programming,C,Function,Imperative Programming,我正在用C编写一个具有以下规范的函数: float find_root(float a, float b, float c, float p, float q); find_root取二次方程和区间(p,q)的系数a,b,c。它将在给定的时间间隔内返回此方程的根 例如:find_root(1,-8,15,2,4)应该生成一个“接近”3.0的根 我已经编写了以下代码,但我不明白为什么它不起作用: #include<stdio.h> #include<math.h> ma
float find_root(float a, float b, float c, float p, float q);
find_root
取二次方程和区间(p,q)的系数a,b,c。它将在给定的时间间隔内返回此方程的根
例如:find_root(1,-8,15,2,4)
应该生成一个“接近”3.0的根
我已经编写了以下代码,但我不明白为什么它不起作用:
#include<stdio.h>
#include<math.h>
main()
{
printf("Hello World");
}
float find_root(float a, float b, float c, float p, float q) {
float d,root1,root2;
d = b * b - 4 * a * c;
root1 = ( -b + sqrt(d)) / (2* a);
root2 = ( -b - sqrt(d)) / (2* a);
if (root1<=q || root1>=p)
{
return root1;
}
return root2;
}
#包括
#包括
main()
{
printf(“你好世界”);
}
浮点查找_根(浮点a、浮点b、浮点c、浮点p、浮点q){
浮子d,根1,根2;
d=b*b-4*a*c;
root1=(-b+sqrt(d))/(2*a);
root2=(-b-sqrt(d))/(2*a);
if(root1=p)
{
返回根1;
}
返回根2;
}
请告诉我错误是什么。您的程序无法运行,因为您从未从
main()调用find\u root()
find_root()
不支持单独运行。您的程序从main()
开始执行。您需要从main()
调用子函数以使其执行
更改main以调用find_root()
,如下所示
int main() //put proper signature
{
float anser = 0;
answer = find_root(1, -8, 15, 2, 4); //taken from the question
printf("The anser is %f\n", answer); //end with a \n, stdout is line buffered
return 0; //return some value, good practice
}
然后,像这样编译程序
gcc -o output yourfilename.c -lm
除此之外,对于find_root()
函数中的逻辑问题,请遵循@paxdiablo先生建议的方法,因为您从未从main()
调用find_root()
,所以您的程序无法运行
find_root()
不支持单独运行。您的程序从main()
开始执行。您需要从main()
调用子函数以使其执行
更改main以调用find_root()
,如下所示
int main() //put proper signature
{
float anser = 0;
answer = find_root(1, -8, 15, 2, 4); //taken from the question
printf("The anser is %f\n", answer); //end with a \n, stdout is line buffered
return 0; //return some value, good practice
}
然后,像这样编译程序
gcc -o output yourfilename.c -lm
除此之外,对于find_root()
函数中的逻辑问题,请按照@paxdiablo先生建议的方式执行根据定义,您的程序从main
开始
您的main
函数没有调用find\u root
,但它应该调用
您需要使用所有警告和调试信息(gcc-Wall-Wextra-g
)进行编译,然后使用调试器(gdb
)一步一步地运行代码以了解程序的行为,因此请使用
gcc -Wall -Wextra -g yoursource.c -lm -o yourbinary
或与
clang -Wall -Wextra -g yoursource.c -lm -o yourbinary
然后学习如何使用gdb
(例如,在没有调试器的情况下运行gdb./yourbinary
…以及以后的/yourbinary
)
然后,您将思考并改进源代码,重新编译它,然后再次调试它。重复这个过程,直到你对你的程序满意为止
顺便说一句,您最好用\n
结束printf
格式字符串或了解
不要忘记阅读您正在调用的每个函数(如…)的文档
您可能希望为您的程序提供一些参数(通过main(int argc,char**argv)
…)。您可以使用将它们转换为double
还可以阅读一些你应该避免的东西
顺便说一句,您可以使用任何标准的C编译器(以及emacs
或gedit
)作为您的家庭作业,例如在Linux笔记本电脑上使用gcc
或clang
(然后使用gdb
)。您不需要特定的seashell
根据定义,您的程序从main
开始
您的main
函数没有调用find\u root
,但它应该调用
您需要使用所有警告和调试信息(gcc-Wall-Wextra-g
)进行编译,然后使用调试器(gdb
)一步一步地运行代码以了解程序的行为,因此请使用
gcc -Wall -Wextra -g yoursource.c -lm -o yourbinary
或与
clang -Wall -Wextra -g yoursource.c -lm -o yourbinary
然后学习如何使用gdb
(例如,在没有调试器的情况下运行gdb./yourbinary
…以及以后的/yourbinary
)
然后,您将思考并改进源代码,重新编译它,然后再次调试它。重复这个过程,直到你对你的程序满意为止
顺便说一句,您最好用\n
结束printf
格式字符串或了解
不要忘记阅读您正在调用的每个函数(如…)的文档
您可能希望为您的程序提供一些参数(通过main(int argc,char**argv)
…)。您可以使用将它们转换为double
还可以阅读一些你应该避免的东西
顺便说一句,您可以使用任何标准的C编译器(以及emacs
或gedit
)作为您的家庭作业,例如在Linux笔记本电脑上使用gcc
或clang
(然后使用gdb
)。对于该数据,您不需要特定的seashell
,您的两个根是5
和3
。使用p==2
和q==4
:
if (root1<=q || root1>=p)
如以下程序所示,这将生成正确的3
:
#include<stdio.h>
#include<math.h>
float find_root (float a, float b, float c, float p, float q) {
float d,root1,root2;
d = b * b - 4 * a * c;
root1 = ( -b + sqrt(d)) / (2* a);
root2 = ( -b - sqrt(d)) / (2* a);
if ((p <= root1) && (root1 <= q))
return root1;
return root2;
}
int main (void) {
printf ("%f\n", find_root(1, -8, 15, 2, 4));
return 0;
}
最后,如果您的编译器抱怨无法链接sqrt
(根据您的一条评论),您可能会发现可以通过指定数学库来解决此问题,例如:
gcc -o myprog myprog.c -lm
对于该数据,您的两个根是5
和3
。使用p==2
和q==4
:
if (root1<=q || root1>=p)
如以下程序所示,这将生成正确的3
:
#include<stdio.h>
#include<math.h>
float find_root (float a, float b, float c, float p, float q) {
float d,root1,root2;
d = b * b - 4 * a * c;
root1 = ( -b + sqrt(d)) / (2* a);
root2 = ( -b - sqrt(d)) / (2* a);
if ((p <= root1) && (root1 <= q))
return root1;
return root2;
}
int main (void) {
printf ("%f\n", find_root(1, -8, 15, 2, 4));
return 0;
}
最后,如果您的编译器抱怨无法链接sqrt
(根据您的一条评论),您可能会发现可以通过指定数学库来解决此问题,例如:
gcc -o myprog myprog.c -lm
首先,您需要在sqrt(d)之前检查d的值
您还应该检查root2是否在间隔内
您还需要有一种返回“无根间隔”的方法。首先,您需要在s之前检查d的值