C语言!帮忙看下我写的这个程序错在哪里了,谢谢!用的是二次插值法.
我编写的程序:===========================#include"stdio.h"#include"math.h"#include"conio.h"v...
我编写的程序:
===========================
#include "stdio.h"
#include "math.h"
#include "conio.h"
void main()
{
float *area(float a1,float p,float a[3]);
float f(float x);
float ar,fr;
float a2,a3;
float f1,f2,f3;
float a1=10,p=0.01,e=0.00001;
float pa[3];
area(a1,p,pa);
a1=pa[0];
a2=pa[1];
a3=pa[2];
f1=f(a1);
f2=f(a2);
f3=f(a3);
do
{
ar=((a3*a3-a2*a2)*f1+(a1*a1-a3*a3)*f2+(a2*a2-a1*a1)*f3);
ar=ar/2/((a3-a2)*f1+(a1-a3)*f2+(a2-a1)*f3);
fr=f(ar);
if(ar>a2)
{if(fr>f2)
{a3=ar;f3=fr;}
else if(fr<f2)
{a1=a2;f1=f2;
a2=ar;f2=fr;}
else
{a3=ar;a1=a2;a2=(a1+a3)/2;
f1=f2;f3=fr;f2=f(a2);}
}
else if(ar<a2)
{if(fr>f2)
{a1=ar;f1=fr;}
else if(fr<f2)
{a3=a2;f3=f2;
a2=ar;f2=fr;}
else
{a1=ar;a3=a2;a2=(a1+a3)/2;
f1=fr;f3=f2;f2=f(a2);}
}
if(fabs(a1-a3)<=e) break;
}while(1);
if(f2<fr)
{ar=a2;fr=f2;}
printf("\nx*=%f\nf(x*)=%f",ar,fr);
}
float *area(float a1,float p,float a[3])
{float f(float x);
float a2,f2,a3,f3,temp;
float acc=0.01;
float f1=f(a1);
float p;
while(1)
{a2=a1+p;f2=f(a2);
if(f2>=f1)
{if(fabs(f2-f1)<acc)
p=p/2;
else
p=-p;
}
else break;
}
while(1)
{a3=a2+p;f3=f(a3);
if(f2<=f3) break;
p=2*p;
a1=a2;f1=f2;
a2=a3;f2=f3;
}
if(a1>a3)
{temp=a1;a1=a3;a3=temp;}
a[0]=a1;a[1]=a2;a[2]=a3;
return a;
}
float f(float x)
{float y=pow(x,4)-4*pow(x,3)-6*pow(x,2)-16*x+4;
return y;
}
=================================
可是选择运行后却出现以下警告:
--------------------------------------------------
警告� temp.c 78: 'storep' 指定一个值给从未使用过的哪一个 在函数
编译成功!
--------------------------------------------------
错误:0 警告:1
我不知道该怎么改了..望高手解答,谢谢谢谢!!! 展开
===========================
#include "stdio.h"
#include "math.h"
#include "conio.h"
void main()
{
float *area(float a1,float p,float a[3]);
float f(float x);
float ar,fr;
float a2,a3;
float f1,f2,f3;
float a1=10,p=0.01,e=0.00001;
float pa[3];
area(a1,p,pa);
a1=pa[0];
a2=pa[1];
a3=pa[2];
f1=f(a1);
f2=f(a2);
f3=f(a3);
do
{
ar=((a3*a3-a2*a2)*f1+(a1*a1-a3*a3)*f2+(a2*a2-a1*a1)*f3);
ar=ar/2/((a3-a2)*f1+(a1-a3)*f2+(a2-a1)*f3);
fr=f(ar);
if(ar>a2)
{if(fr>f2)
{a3=ar;f3=fr;}
else if(fr<f2)
{a1=a2;f1=f2;
a2=ar;f2=fr;}
else
{a3=ar;a1=a2;a2=(a1+a3)/2;
f1=f2;f3=fr;f2=f(a2);}
}
else if(ar<a2)
{if(fr>f2)
{a1=ar;f1=fr;}
else if(fr<f2)
{a3=a2;f3=f2;
a2=ar;f2=fr;}
else
{a1=ar;a3=a2;a2=(a1+a3)/2;
f1=fr;f3=f2;f2=f(a2);}
}
if(fabs(a1-a3)<=e) break;
}while(1);
if(f2<fr)
{ar=a2;fr=f2;}
printf("\nx*=%f\nf(x*)=%f",ar,fr);
}
float *area(float a1,float p,float a[3])
{float f(float x);
float a2,f2,a3,f3,temp;
float acc=0.01;
float f1=f(a1);
float p;
while(1)
{a2=a1+p;f2=f(a2);
if(f2>=f1)
{if(fabs(f2-f1)<acc)
p=p/2;
else
p=-p;
}
else break;
}
while(1)
{a3=a2+p;f3=f(a3);
if(f2<=f3) break;
p=2*p;
a1=a2;f1=f2;
a2=a3;f2=f3;
}
if(a1>a3)
{temp=a1;a1=a3;a3=temp;}
a[0]=a1;a[1]=a2;a[2]=a3;
return a;
}
float f(float x)
{float y=pow(x,4)-4*pow(x,3)-6*pow(x,2)-16*x+4;
return y;
}
=================================
可是选择运行后却出现以下警告:
--------------------------------------------------
警告� temp.c 78: 'storep' 指定一个值给从未使用过的哪一个 在函数
编译成功!
--------------------------------------------------
错误:0 警告:1
我不知道该怎么改了..望高手解答,谢谢谢谢!!! 展开
1个回答
展开全部
其他的地方没有错,area函数定义的变量p的地方有错:
#include "stdio.h"
#include "math.h"
#include "conio.h"
void main()
{
float *area(float a1,float p,float a[3]);
float f(float x);
float ar,fr;
float a2,a3;
float f1,f2,f3;
float a1=10,p=0.01,e=0.00001;
float pa[3];
area(a1,p,pa);
a1=pa[0];
a2=pa[1];
a3=pa[2];
f1=f(a1);
f2=f(a2);
f3=f(a3);
do
{
ar=((a3*a3-a2*a2)*f1+(a1*a1-a3*a3)*f2+(a2*a2-a1*a1)*f3);
ar=ar/2/((a3-a2)*f1+(a1-a3)*f2+(a2-a1)*f3);
fr=f(ar);
if(ar>a2)
{
if(fr>f2)
{
a3=ar;f3=fr;}
else if(fr<f2)
{
a1=a2;f1=f2;
a2=ar;f2=fr;}
else
{
a3=ar;a1=a2;a2=(a1+a3)/2;
f1=f2;f3=fr;f2=f(a2);}
}
else if(ar<a2)
{
if(fr>f2)
{
a1=ar;f1=fr;}
else if(fr<f2)
{
a3=a2;f3=f2;
a2=ar;f2=fr;}
else
{
a1=ar;a3=a2;a2=(a1+a3)/2;
f1=fr;f3=f2;f2=f(a2);}
}
if(fabs(a1-a3)<=e) break;
}while(1);
if(f2<fr)
{
ar=a2;fr=f2;}
printf("\nx*=%f\nf(x*)=%f",ar,fr);
}
float *area(float a1,float p,float a[3])
{
float f(float x);
float a2,f2,a3,f3,temp;
float acc=0.01;
float f1=f(a1);
//float p;//这里有重复定义的错,p已经是函数的参数了,
while(1)
{
a2=a1+p;f2=f(a2);
if(f2>=f1)
{
if(fabs(f2-f1)<acc)
p=p/2;
else
p=-p;
}
else break;
}
while(1)
{
a3=a2+p;f3=f(a3);
if(f2<=f3) break;
p=2*p;
a1=a2;f1=f2;
a2=a3;f2=f3;
}
if(a1>a3)
{
temp=a1;a1=a3;a3=temp;}
a[0]=a1;a[1]=a2;a[2]=a3;
return a;
}
float f(float x)
{
float y=pow(x,4)-4*pow(x,3)-6*pow(x,2)-16*x+4;
return y;
}
#include "stdio.h"
#include "math.h"
#include "conio.h"
void main()
{
float *area(float a1,float p,float a[3]);
float f(float x);
float ar,fr;
float a2,a3;
float f1,f2,f3;
float a1=10,p=0.01,e=0.00001;
float pa[3];
area(a1,p,pa);
a1=pa[0];
a2=pa[1];
a3=pa[2];
f1=f(a1);
f2=f(a2);
f3=f(a3);
do
{
ar=((a3*a3-a2*a2)*f1+(a1*a1-a3*a3)*f2+(a2*a2-a1*a1)*f3);
ar=ar/2/((a3-a2)*f1+(a1-a3)*f2+(a2-a1)*f3);
fr=f(ar);
if(ar>a2)
{
if(fr>f2)
{
a3=ar;f3=fr;}
else if(fr<f2)
{
a1=a2;f1=f2;
a2=ar;f2=fr;}
else
{
a3=ar;a1=a2;a2=(a1+a3)/2;
f1=f2;f3=fr;f2=f(a2);}
}
else if(ar<a2)
{
if(fr>f2)
{
a1=ar;f1=fr;}
else if(fr<f2)
{
a3=a2;f3=f2;
a2=ar;f2=fr;}
else
{
a1=ar;a3=a2;a2=(a1+a3)/2;
f1=fr;f3=f2;f2=f(a2);}
}
if(fabs(a1-a3)<=e) break;
}while(1);
if(f2<fr)
{
ar=a2;fr=f2;}
printf("\nx*=%f\nf(x*)=%f",ar,fr);
}
float *area(float a1,float p,float a[3])
{
float f(float x);
float a2,f2,a3,f3,temp;
float acc=0.01;
float f1=f(a1);
//float p;//这里有重复定义的错,p已经是函数的参数了,
while(1)
{
a2=a1+p;f2=f(a2);
if(f2>=f1)
{
if(fabs(f2-f1)<acc)
p=p/2;
else
p=-p;
}
else break;
}
while(1)
{
a3=a2+p;f3=f(a3);
if(f2<=f3) break;
p=2*p;
a1=a2;f1=f2;
a2=a3;f2=f3;
}
if(a1>a3)
{
temp=a1;a1=a3;a3=temp;}
a[0]=a1;a[1]=a2;a[2]=a3;
return a;
}
float f(float x)
{
float y=pow(x,4)-4*pow(x,3)-6*pow(x,2)-16*x+4;
return y;
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询