//函數fun的功能是:計算形參x所指數組中N個數的平均值(規定所有數均為正數),作為函數返回,並將大於平均值的數放在形參y所指數組中,在主函數中輸出。
//重難點:對結構體數據進行求平均值。
1 #include <stdlib.h> 2 #include <stdio.h> 3 #define N 10 4 double fun(double x[],double *y) 5 { int i,j; double av; 6 /**********found**********/ 7 av=0; 8 /**********found**********/ 9 for (i = 0; i < N; i++) 10 av = av + x[i]/10; 11 for(i=j=0; i<N; i++) 12 /**********found**********/ 13 if(x[i]>av) y[j++]= x[i]; 14 y[j]=-1;//判斷結束標識符 15 return av; 16 } 17 void main() 18 { int i; double x[N],y[N]; 19 for(i=0; i<N; i++){ x[i]=rand()%50; printf("%4.0f ",x[i]);} 20 printf("\n"); 21 printf("\nThe average is: %f\n",fun(x,y)); 22 for(i=0; y[i]>=0; i++) printf("%5.1f ",y[i]); 23 printf("\n"); 24 }
//函數fun的功能是:根據整型參數m,計算如下公式的值。
//重難點:函數返回值類型相一致。
1 #include <stdlib.h> 2 #include <conio.h> 3 #include <stdio.h> 4 /*************found**************/ 5 double fun (int m) 6 { double y=0, d; 7 int i; 8 /*************found**************/ 9 for (i = 100; i <= m; i += 100) 10 { 11 {d = (double)i*(double)i; 12 y += 1.0 / d; 13 } 14 } 15 return(y); 16 } 17 void main() 18 { int n=2000; 19 system("CLS");//執行控制台命令cls,就是CMD下面的cls,功能是清屏,清除所有顯示的信息。 20 printf("\nThe result is %1f\n",fun(n)); 21 }
//在此程序中,已知學生的記錄由學號和學習成績構成,N名學生的數據已存入a結構體數組中。編寫fun函數:找出成績最低的學生記錄,通過形參返回主函數(規定只有一個最低分)。
//重難點:對結構體數據的處理。
1 #include<stdio.h> 2 #include<string.h> 3 #include<conio.h> 4 #include<stdlib.h> 5 #define N 10 6 typedef struct ss 7 { char num[10]; 8 int s; 9 } STU; 10 void fun(STU a[], STU *s) 11 { 12 int i; 13 //s = a;這個相當於把首地址互等 14 s[0] = a[0];//這個把值互等 15 for (i = 0; i < N; i++) 16 { 17 if (a[i].s < s[0].s) 18 { 19 s[0] = a[i]; 20 } 21 } 22 } 23 void main() 24 { 25 FILE *wf; 26 STU a[N]={{ "A01",81},{ "A02",89},{ "A03",66},{ "A04",87},{ "A05",77}, 27 { "A06",90},{ "A07",79},{ "A08",61},{ "A09",80},{ "A10",71}},m; 28 int i; 29 system("CLS");//執行控制台命令cls,就是CMD下面的cls,功能是清屏,清除所有顯示的信息。 30 printf("*****The original data*****\n"); 31 for(i=0;i<N;i++) 32 printf("No=%s Mark=%d\n", a[i].num,a[i].s); 33 fun(a,&m); 34 printf("*****THE RESULT*****\n"); 35 printf("The lowest :%s, %d\n",m.num,m.s); 36 /******************************/ 37 wf=fopen("out.dat","w"); 38 fprintf(wf,"%s, %d",m.num,m.s); 39 fclose(wf); 40 /*****************************/ 41 }