82.请编写一个函数fun,它的功能是:计算n门课程的平均分,计算结果作为函数值返回。
例如:若有5门课程的成绩是:90.5,72,80,61.5,55,则函数的值为71.80。
♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣
#include <stdio.h>
float fun (float *a, int n)
{
float av=0.0;
int i;
for(i=0;i<n;i )
av=av a[i];
return (av/n);
}
main()
{
FILE *wf;
float score[30]={90.5,72,80,61.5,55},aver;
aver=fun(score,5);
printf("\nAverage score is: %5.2f\n",aver);
wf=fopen("out.dat","w");
fprintf(wf,"%5.2f",aver);
fclose(wf);
}
83.假定输入的字符串中只包含字母和*号。请编写函数fun,它的功能是:将字符串尾部的*号全部删除,前面和中间的*号不删除。
例如,若字符串中的内容为****A*BC*DEF*G*******,删除后,字符串中的内容则应当是****A*BC*DEF*G。在编写函数时,不得使用C语言提供的字符串函数。
♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣
#include <stdio.h>
#include <conio.h>
void fun (char *a )
{
while(*a!='\0') a ;
a--;
while(*a=='*') a--;
*(a 1)='\0';
}
main()
{
FILE *wf;
char s[81],*t="****A*BC*DEF*G*******";
printf("Enter a string :\n");
gets(s);
fun(s);
printf("The string after deleted:\n");
puts(s);
wf=fopen("out.dat","w");
fun(t);
fprintf(wf,"%s",t);
fclose(wf);
}
84.请编写函数fun,其功能是:将两个两位数的正整数a、b合并形成一个整数放在c中。合并的方式是:将a数的十位和个位数依次放在c数的个位和百位上,b数的十位和个位数依次放在c数的千位和十位上。
例如,当a=45,b=12,调用该函数后c=1524。
♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣
#include <conio.h>
#include <stdio.h>
/*本题的关键在于如何表示出个、十、百、千位数。对于一个两位的整数,用10对它求余得到个位数上的数,将它除10得到十位数上的数。*/
void fun ( int a, int b , long *c)
{ *c=(b/10)*1000 (a%10)*100 (b%10)*10 a/10 ; }
main()
{ int a,b; long c;
printf(" input a, b: ");
scanf("%d%d", &a,&b);
fun(a,b,&c);
printf(" the result is :%ld\n", c);
}
85.N名学生的成绩已在主函数中放入一个带头节点的链表结构中,h指向链表的头节点。请编写函数fun,它的功能是:求出平均分,由函数值返回。
例如,若学生的成绩是85,76,69,85,91,72,64,87;则平均分应当是78.625。
♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣
#include<stdlib.h>
#include<stdio.h>
#define N 8
struct slist
{ double s;
struct slist *next;
};
typedef struct slist STRUC;
double fun(STRUC *h)
{
double av=0.0;
STRUC *p= h->next;
while(p!=NULL)
{av=av p->s ;
p=p->next;
}
return av/N;
}
STRUC *creat(double *s)
{
STRUC *h,*p,*q;
int i=0;
h=p=( STRUC*)malloc(sizeof(STRUC));
p->s=0;
while(i<N)
{q=( STRUC*)malloc(sizeof(STRUC));
q->s=s[i];i ;p->next=q;p=q;
}
p->next=0;
return h;
}
outlist(STRUC *h)
{
STRUC *p;
p=h->next;
printf("head ");
do
{printf("->%4.1f ",p->s);
p=p->next;
}
while(p!=0);
}
main()
{
FILE *wf;
double s[N]={85,76,69,85,91,72,64,87},ave;
STRUC *h;
h=creat(s);
outlist(h);
ave=fun(h);
printf("ave=%6.3f\n ",ave);
wf=fopen("out.dat","w");
fprintf(wf,"%6.3f",ave);
fclose(wf);
}
86.请编写函数fun,其功能是:计算并输出给定10个数的方差:
例如,给定的10个数为95.0,89.0,76.0,65.0,88.0,72.0,85.0,81.0,90.0,56.0,则输出为S=11.730729。
♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣
#include<math.h>
#include<stdio.h>
double fun(double x[10])
{
double x1=0.0, s=0.0 ;
int i;
for(i=0;i<10;i )
x1=x1 x[i];
x1=x1/10;
for(i=0;i<10;i )
s=s (x[i]-x1)*(x[i]-x1);
return sqrt(s/10) ;
}
main()
{
FILE *wf;
double s,x[10]={95.0,89.0,76.0,65.0,88.0,72.0,85.0,81.0,90.0,56.0};
int i;
printf("\nThe original data is:\n");
for(i=0;i<10;i )
printf("%6.1f ",x[i]);
printf("\n\n ");
s=fun(x);
printf("s=%f\n\n ",s);
wf=fopen("out.dat","w");
fprintf(wf,"%f",s);
fclose(wf);
}