谭浩强c语言程序设计作业,谭浩强《C语言程序设计》第7章习题解答(13、14题)...
譚浩強《C語言程序設計》第7章習題解答(13、14題)
13 用遞歸法求n階勒讓德多項式的值,遞歸公式為:
p0(x) =1 ; p1(x) = x
;?pn(x) =
((2n-1)·x·pn-1(x) – (n-1)pn-2(x)/n
,(n>1)。
程序:
#include
#include "math.h"
int main(){
float Legendre(float x,int n);
float x=1.3; float y;
int n=4;
y=Legendre(x,n);
printf("x=%f, y=%f\n" ,x , y);
return 0;
}
float Legendre(float x,int n){
float value;
if(n==0)
value
=1;
else
if(n==1)
value =x;
else {
value = (2*n-1)*x*Legendre(x, n-1)-(n-1)*Legendre(x,n-2);
value /=n;
}
return value;
}
14
輸入10個學生5門課的成績分別用函數求:①每個學生的平均分;②每門課程的平均分;③找出最高的分數對應的學生及其課程;④求平均分方差:
σ=Σxi2
/n–(Σxi/n)2
,xi為某一學生的平均分
程序:
#include
float student[10] ;
float
scores[10][5]={{80,75,82,53,74},{85,56,77,80,59},{60,66,62,63,64},
{65,66,67,68,69},{70,71,72,73,74},
{75,76,77,78,79},{80,81,82,83,84},
{85,86,87,88,89},{90,91,92,93,94},{95,96,97,98,99}};
float averOfStudent[10]; // 單個學生的平均分
int maxi = 0;?// 最高分學生的位置標記
int
maxj;?// 最高分課程的位置標記
int main(){
float average(float a[], int n);
void getMax(float x[10][5]); //
取得最高分所在的位置
float xigma(float a[10]);
// answer 1 : for average
of studeng
for(int i=0; i<10;
i++){
averOfStudent[i] = average(scores[i],5);
printf("average of student%d =%f\n",i ,
averOfStudent[i]);
}
// answer 2:average of
lessons
int k=0 ;
for(k=0; k<5; k++){
for(int j=0; j<10; j++)
student[j] = scores[k][j];
printf("The lesson%d's avrage is
%f\n",k,average(student,10));
}
// answer 3: highest score
of all
getMax(scores);
printf("The Lagerst Score
belong student %d lesson %d, score=%f.
\n",maxi,maxj,scores[maxi][maxj]);
// answer 3: xigma for
average
float xig =
xigma(averOfStudent);
printf("xigma of averages =
%f.\n", xig);
return 0;
}
float average(float a[], int
n){?// for lessons
int sum
=0;
for(int
i=0; i
sum += a[i];
return
sum/n;
}
float xigma(float a[10]){
float xig =0;
for(int i=0;i<10; i++)
xig +=
a[i]*a[i];
xig /=10;
xig -= average(a,10)*average(a,10);
return xig;
}
void getMax(float x[10][5]){
float max =0;
for(int
i=0;i<10;i++)
for(int j=0; j<5; j++)
if(max
max = x[i][j];
maxi=i; maxj=j;
}
}
總結
以上是生活随笔為你收集整理的谭浩强c语言程序设计作业,谭浩强《C语言程序设计》第7章习题解答(13、14题)...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mybatis 执行插入操作,inser
- 下一篇: HDU4055 - number str