c语言数组和递归编下

2024-11-24 06:21:44
推荐回答(2个)
回答1:

程序如下:
首先说明一下,因为你的student定义的并不是一个数组所以我是将student1,student2,student3分开算的,至于你说的不局限于3个,当然可以了,只不过你要分开求而已。如下:
average(&student1);
average(&student2);
average(&student3);
total(&student1);
total(&student2);
total(&student3);
我定义了两个函数一个average是求平均分的,还有一个total是求总分的,两个函数的形参都是student类型的指针,所以你只需要传入你定义的student类型变量的地址就行,如student1,那么他的地址就是&student1,你应该知道指针指向的是一个地址,所以得传地址了。
typedef struct date
{
int month;
int day;
int year;
}data;

typedef struct subject
{
int chinese;
int math;
int english;
}subject;

typedef struct student
{
int num;
char name[20];
int age;
data birthday;
subject score;
}student;

student student1={001,"Wang li",20,5,12,1990,70,80,90};
student student2={002,"Chen li",19,4,8,1991,70,80,90};
student student3={003,"he li",20,4,3,1991,70,80,90};

float average(student * s)
{
float aver;
aver=(s->score.chinese+s->score.math+s->score.english)/3;
return aver;
}

int total(student * s)
{
int total;
total=s->score.chinese+s->score.math+s->score.english;
return total;
}

void main()
{
float a;
int t;
a=average(&student1);
t=total(&student1);
printf("%f\n%d",a,t);
}

回答2:

二分法不用递归就可以了,非要用递归的话 假设a(i)为从小到大排列的数组,K为所要查找的关键值,L为数组下标的下限,U为上限private function Find(L as integer,U as integer)as integer dim i as integer if U-L=0 then msgbox("所查关键值K不存在!") i=(L U)/2 if K=a(i) then Find=i exit function elseif K