07.19C

来源:互联网 发布:爱的算法 刘宇昆 编辑:程序博客网 时间:2024/05/15 23:44

【项目1-翻转数组】
  下面的程序的输出为10 9 8 7 6 5 4 3 2 1。也就是说,调用reverse(b,10);后,b数组中的元素正好“翻转”过来了。请定义reverse函数,实现这个功能。

解法:

#include <stdio.h>#include <stdlib.h>void reverse(int*,int);int main(){    int n,i;    printf("please enter a number n:");    scanf("%d",&n);    int *p;    p=(int*)malloc(n*sizeof(int));    fflush(stdin);    printf("please enter ten numbers:");    for(i=0;i<n;++i)    {        scanf("%d",(p+i));    }    reverse(p,n);    return 0;}void reverse(int *a,int n){    int i;    printf("the elements in the array are(reverse order):");    for (i=0;i<n;++i)    {        printf("%d ",*(a+(n-i-1)));    }}
【项目2 - 成绩处理函数版】
  在数组score中将要存储了某小组C程序设计的成绩,请设计实现下面的各功能函数,并在main函数中调用,组合成一个完整的应用:
(1)输入小组人数及成绩,要保证成绩在0-100之间;
(2)输出该小组的最高成绩、最低成绩、平均成绩;
(3)输出考得最高成绩和最低成绩的同学的人数;
(4)输出考得最高成绩和最低成绩的同学的学号(设数组下标即学号,可能有相同的成绩)。
(5)求出所有同学成绩的标准偏差,标准偏差公式为,其中为样本(即某同学成绩),为均值(前面已经求出),为样本数目;

解法:

#include <stdio.h>#include <stdlib.h>#include <math.h>int *score_in(int*,int);//function of the score inputingint *score_m(int*,int);//function of the max, min,and averageint *score_num(int*, int);//function of the amount and number of of the min and maxdouble stand_deviation(int*,int);int main(){    int n;    int *m;//存放平均数,最高成绩和最低成绩    int *m_num;//最高和最低成绩的人数    m=(int*)malloc(3*sizeof(int));    m_num=(int*)malloc(2*sizeof(int));    double s=0;//所有成绩的标准差    int *iScore;    iScore=(int*)malloc(n*sizeof(int));    printf("please enter the amount of the students in the group:");    scanf("%d",&n);    iScore=(int*)malloc(n*sizeof(int));    fflush(stdin);    iScore=score_in(iScore,n);    m=score_m(iScore,n);    printf("the highest score of the group is:%d\n",*m);    printf("the lowest score of the group is:%d\n",*(m+1));    printf("the average score of the group is:%d\n",*(m+2));    m_num=score_num(iScore,n);    printf("the total amount of the highest score is:%d\n",*m_num);    printf("the total amount of the lowest score is:%d\n",*(m_num+1));    s=stand_deviation(iScore,n);    printf("the stand deviation of the students' score is;%.2lf\n",s);    return 0;    free(m);    free(m_num);}int *score_in(int *score,int n){    int i=0;    do    {        printf("please enter the %d th student's score:",i);        scanf("%d",(score+i));        if(*(score+i)<0||*(score+i)>100)            continue;        ++i;    }while(i<n);   return(score);}int *score_m(int *a,int n){    int *m;    m=(int*)malloc(3*sizeof(int));    int min=101;    int max=0;    int aver=0;    int i;    int sum=0;    for(i=0;i<n;++i)    {        sum +=*(a+i);        if(*(a+i)<min)            min=*(a+i);        if(*(a+i)>max)            max=*(a+i);    }    aver=sum/n;    *m=max;    *(m+1)=min;    *(m+2)=aver;    return(m);    free(m);}int *score_num(int *a,int n){  int iMax=0;//最高分的人数  int iMin=0;//最低分的人数  int *num_m;  num_m=(int*)malloc(3*sizeof(int));//用于存放调用函数的结果  int *n_stu;  n_stu=(int*)malloc(3*sizeof(int));  int i;  num_m=score_m(a,n);  for(i=0;i<n;++i)  {      if(*(a+i)==*num_m)        ++iMax;      if(*(a+i)==*(num_m+1))        ++iMin;  }  *n_stu=iMax;  *(n_stu+1)=iMin;  return(n_stu);  free(num_m);  free(n_stu);}double stand_deviation(int *a,int n){    int *m;    double sum=0;//累加和    m=score_m(a,n);    int i;    int s;//标准差    for(i=0;i<n;++i)    {        sum += pow((*(a+i)-m[2]),2);    }    s=sqrt(sum/(n-1));    return(s);}























































































































































0 0
原创粉丝点击