谭浩强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