【正文】
序中,函數(shù)fun的功能是:從n個(gè)學(xué)生的成績(jī)中統(tǒng)計(jì)出低于平均分的學(xué)生人數(shù),此人數(shù)作為函數(shù)值返回,平均分存放在形參aver所指的存儲(chǔ)單元中。例如,輸入8名學(xué)生的成績(jī):、60、79864則低于平均分的學(xué)生人數(shù)為4()。include include include define N 20int fun(float *s, int n,float *aver){ float ave ,t=。 int count=0,k,i。 for(k=0。kn。k++)/*************found**************/ t=s[k]。 ave=t/n。 for(i=0。in。i++) if(s[i]ave) count++。/*************found**************/ *aver=amp。ave。 return count。}void main(){ float s[30],aver。 int m,i。 system(CLS)。 printf(\nPlease enter m: )。 scanf(%d,amp。m)。 printf(\nPlease enter %d mark :\n,m)。 for(i=0。im。i++) scanf(%f,s+i)。 printf(\nThe number of students :%d\n,fun(s,m,amp。aver))。 printf(Ave=%f\n,aver)。}【參考答案】(1)t+=s[k]。(2)*aver=ave。題目十平均值問(wèn)題52. 下列給定程序中,函數(shù)fun的功能是:給定n個(gè)實(shí)數(shù),輸出平均值,并統(tǒng)計(jì)平均值以上(含平均值)的實(shí)數(shù)個(gè)數(shù)。例如,n=8時(shí),、、在平均值以上的實(shí)數(shù)個(gè)數(shù)應(yīng)為5。請(qǐng)改正程序中的錯(cuò)誤,使其能得出正確的結(jié)果。include include include int fun(float x[], int n)/************found************/ int j, c=0。 float xa=。 for (j=0。 jn。 j++) xa += x[j]/n。 printf(ave =%f\n,xa)。 for (j=0。 jn。 j++)/************found************/ if (x[j] = xa) c++。 return c。}main (){float x[100] = {, , , , , , , }。 system(cls)。 printf(%d\n, fun (x, 8))。}(1) {int j, c=0。float xa=。(2)if (x[j] = xa)題目十一字符串連接11. 例如,若主函數(shù)中a字符串為aBCDeFgH,b字符串為Abcd,則c中的字符串應(yīng)為aBcdeFgH。規(guī)定輸入的字符串中只包含字母和*號(hào)。請(qǐng)編寫(xiě)函數(shù)fun,其功能是:除了字符串前導(dǎo)的*號(hào)之外,將串中其它*號(hào)全部刪除。在編寫(xiě)函數(shù)時(shí),不得使用C語(yǔ)言提供的字符串函數(shù)。include include void fun(char *p ,char *q, char *c){/************found************/ int k = 1。/************found************/ while( *p != *q ) { if( *p*q ) c[k]=*q。 else c[k]=*p。 if(*p) p++。 if(*q) q++。 k++。 }}main(){ char a[10]=aBCDeFgH, b[10]=ABcd, c[80]={39。\039。}。 fun(a,b,c)。 printf(The string a: )。 puts(a)。 printf(The string b: )。 puts(b)。 printf(The result : )。 puts(c)。}(1) int k=0。(2)while (*p||*q)題目十一字符串連接22. 下列給定程序中函數(shù)fun的功能是:將m(1≤m≤10)個(gè)字符串連接起來(lái),組成一個(gè)新串,放入pt所指存儲(chǔ)區(qū)中。例如:把三個(gè)串a(chǎn)bc、CD、EF連接起來(lái),結(jié)果是abcCDEF。include include void fun ( char str[][10], int m, char *pt ){/************found************/ Int k, q, i 。 for ( k = 0。 k m。 k++ ) { q = strlen ( str [k] )。 for (i=0。 iq。 i++)/************found************/ pt[i] = str[k,i] 。 pt += q 。 pt[0] = 0 。 }}main( ){ int m, h 。 char s[10][10], p[120] 。 printf( \nPlease enter m: ) 。 scanf(%d, amp。m) 。 gets(s[0]) 。 printf( \nPlease enter %d string:\n, m ) 。 for ( h = 0。 h m。 h++ ) gets( s[h]) 。 fun(s, m, p) 。 printf( \nThe result is : %s\n, p) 。}(1)int k, q, i。(2)pt[i] = str[k][i]。 題目十一字符串連接32. 下列給定程序中函數(shù)fun的功能是:判斷ch中的字符是否與str所指串中的某個(gè)字符相同;若相同,什么也不做,若不同,則將其插在串的最后。include include /**********found**********/void fun(char str, char ch ){ while ( *str amp。amp。 *str != ch ) str++。/**********found**********/ if ( *str == ch ) { str [ 0 ] = ch。/**********found**********/ str[1] = 39。039。 }}main( ){ char s[81], c 。 printf( \nPlease enter a string:\n )。 gets ( s )。 printf (\n Please enter the character to search : )。 c = getchar()。 fun(s, c) 。 printf( \nThe result is %s\n, s)。}【參考答案】(1)void fun(char *str, char ch )(2)if ( *str != ch )(3)str[1] = 0。題目十一字符串連接42. 給定程序中函數(shù)fun的功能是:首先把b所指字符串中的字符按逆序存放,然后將a所指字符串中的字符和b所指字符串中的字符,按排列的順序交叉合并到c所指數(shù)組中,過(guò)長(zhǎng)的剩余字符接在c所指的數(shù)組的尾部。例如,當(dāng)a所指字符串中的內(nèi)容為abcdefg,b所指字符串中的內(nèi)容為1234時(shí),c所指數(shù)組中的內(nèi)容應(yīng)為a4b3c2d1efg;而當(dāng)a所指字符串中的內(nèi)容為1234,b所指字符串的內(nèi)容為abcdefg時(shí),c所指數(shù)組中的內(nèi)容應(yīng)該為1g2f3e4dcba。include include void fun( char *a, char *b, char *c ){ int i , j。 char ch。 i = 0。 j = strlen(b)1。/************found************/ while ( i j ) { ch = b[i]。 b[i] = b[j]。 b[j] = ch。 i++。 j。 } while ( *a || *b ) {/************found************/ If ( *a ) { *c = *a。 c++。 a++。 } if ( *b ) { *c = *b。 c++。 b++。 } } *c = 0。}main(){ char s1[100],s2[100],t[200]。 printf(\nEnter s1 string : )。scanf(%s,s1)。 printf(\nEnter s2 string : )。scanf(%s,s2)。 fun( s1, s2, t )。 printf(\nThe result is : %s\n, t )。}(1)while ( i j )(2)if ( *a )題目十二求和問(wèn)題12. 下列給定程序中fun函數(shù)的功能是:求表達(dá)式s=aa… aa-…- aaa - aa -a(此處aa… aa表示n個(gè)a,a和n的值在1至 9之間)例如,a=3,n=6,則以上表達(dá)式為:s=333333-33333-3333-333-33-3其值是296298。a和n是fun函數(shù)的形參,表達(dá)式的值作為函數(shù)值傳回main函數(shù)。include long fun (int a, int n){ int j 。/**************found**************/ long s = 0, t = 1 。 for ( j = 0 。 j n 。 j++) t = t * 10 + a 。 s = t 。 for ( j = 1 。 j n 。 j++) {/**************found**************/ t = t % 10 。 s = s t 。 } return(s) 。}main( ){ int a, n 。 printf( \nPlease enter a and n:) 。 scanf( %d%d, amp。a, amp。n ) 。 printf( The value of function is: %ld\n, fun ( a, n ) )。}(1)long s=0, t=0。(2)t=t/10。題目十二求和問(wèn)題22. 下列給定程序中函數(shù)fun的功能是:計(jì)算S=f(n)+f(n+1)+…+f(0)+f(1)+f(2)+…+f(n)的值。例如,當(dāng)n的值為5時(shí)。f(x)函數(shù)定義如下:include include include include /*************found**************/f(double x){ if(x==||x==) return 。 else if(x) return(x1)/(x2)。 else return(x+1)/(x2)。}double fun(int n){ int i。 double s=,y。 for(i=n。 i=n。i++) { y=f(*i)。 s+=y。}/*************found**************/ return s}void main(){ system(CLS)。 printf(%f\n, fun(5))。}(1)double f(double x)(2)return s 。題目十二求和問(wèn)題32. 下列給定程序中函數(shù)fun的功能是:計(jì)算函數(shù)F(x,y,z)=(x+y)/(x-y)+(z+y)/(z-y)的值。其中x和y的值不相等,z和y的值不相等。例如,當(dāng)x的值為9,y的值為11,z的值為15時(shí)。include include include /************found************/define FU(m,n) ((m/n))float fun(float a,float b,float c){ float value。 value=FU(a+b,ab)+FU(c+b,cb)。