49 date:2021.3.17
要点:
详细代码如下:
#include <stdio.h>
#define N 9
/**********found**********/
void fun(int a[], int n) //a[]数组无需指定参数
{ int i, j, max, min, px, pn, t;
/**********found**********/
for (i=0; i<n-1; i+=2) //每次操作两个数,最大和最小
{ max = min = a[i];
px = pn = i;
/**********found**********/
for (j=i+1; j<n; j++) //!!!!
{ if (max < a[j])
{ max = a[j]; px = j; }
if (min > a[j])
{ min = a[j]; pn = j; }
}
if (px != i)
{ t = a[i]; a[i] = max; a[px] = t;
if (pn == i) pn= px;
}
if (pn != i+1)
{ t = a[i+1]; a[i+1] = min; a[pn] = t; }
}
}
void main()
{ int b[N]={1,4,2,3,9,6,5,8,7}, i;
printf("\nThe original data :\n");
for (i=0; i<N; i++) printf("%4d ", b[i]);
printf("\n");
fun(b, N);
printf("\nThe data after moving :\n");
for (i=0; i<N; i++) printf("%4d ", b[i]);
printf("\n");
}
详细代码如下:
#include <stdio.h>
#define N 8
typedef struct
{ char num[10];
double s;
} STREC;
double fun( STREC *a, STREC *b, int *n )
{
int i,j,k = 0;
double sum = 0.0,avg;
//求平均值
for(i = 0; i < N; i++)
{
sum += a[i].s;
}
avg =(double)sum / N;
//找低于平均分的学生,放入b数组
for(j = 0; j < N; j++)
{
if(a[j].s < avg)
b[k++]=a[j];
}
*n = k;
return avg;
}
void main()
{ STREC s[N]={{"GA05",85},{"GA03",76},{"GA02",69},{"GA04",85},
{"GA01",91},{"GA07",72},{"GA08",64},{"GA06",87}};
STREC h[N],t;FILE *out ;
int i,j,n; double ave;
ave=fun( s,h,&n );
printf("The %d student data which is lower than %7.3f:\n",n,ave);
for(i=0;i<n; i++)
printf("%s %4.1f\n",h[i].num,h[i].s);
printf("\n");
out = fopen("out.dat","w") ;
fprintf(out, "%d\n%7.3f\n", n, ave);
for(i=0;i<n-1;i++)
for(j=i+1;j<n;j++)
if(h[i].s>h[j].s) {t=h[i] ;h[i]=h[j]; h[j]=t;}
for(i=0;i<n; i++)
fprintf(out,"%4.1f\n",h[i].s);
fclose(out);
}