Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2538828
  • 博文数量: 308
  • 博客积分: 5547
  • 博客等级: 大校
  • 技术积分: 3782
  • 用 户 组: 普通用户
  • 注册时间: 2009-11-24 09:47
个人简介

hello world.

文章分类

全部博文(308)

分类: C/C++

2010-08-23 16:24:35

    有一个班4个学生,5门课。1.求第一门课的平均分。2.找出有两门以上课程不及格的学生,并输出他们的学号和全部课程成绩和平均成绩.3.找出平均成绩在90分以上,或者全部课程成绩在85分以上的学生。分别编写三个函数实现以上3个要求。
    我已经将程序写过了,今天又看了返回指针值的函数。这一小节(使用谭浩强编写的《C程序设计》)。对代码进行重新编写,代码如下:
 

#include <stdio.h>

float average(int [][5],int,int);
int *search_fail2(int (*)[5]);
int *search_good(int (*)[5]);
void print_source(int [][5],int);
int main(int argc, char *argv[])
{
    int arr[4][5] ={{80,50,70,65,30},{77,88,99,66,88},{98,95,96,96,92},{66,68,80,30,40}}; //test

    int i,j,*p,*p_good;
    float f_average = 0;
    
    printf("the source data is :\n");
    print_source(arr,4);
    printf("\nthe 1 kemu average is :%.2f\n\n",average(arr,4,0));
    for (i = 0; i < 4;i++)
    {
        p = search_fail2(arr + i);
        p_good = search_good(arr + i);
        if (p == *(arr + i))
        {
           printf("No.%d fall score: ",i);
           for(j = 0; j < 5; j++)
           {
                 printf("%d ",*(p + j));
                 f_average += *(p + j);
           }
           printf("\nthe average is %.2f\n",f_average / 5);
        }
        f_average = 0;
        
        if (p_good == *(arr + i))
        {
           printf("No.%d is good student\n",i);
        }
    }
    system("pause");
    return 0;
}

float average(int arr[][5],int n,int k)
{
      int i,j,(*p)[5];
      p = arr;
      float result = 0;
      for (i = 0; i < n; i++)
      {
          result += *(*(p+i)+k);
      }
      return result / n;
}

int *search_fail2(int (*point)[5])
{
     int i,count = 0;
     int *p = *(point + 1);
     for (i = 0; i < 5;i++)
     {
         if (*(*point + i) < 60)
         {
            count ++;
         }
         
         if (count >= 2)
         {
            p = point;
            break;
         }
     }
     return p;
}

void print_source(int arr[][5],int n)
{
     int i,j;
     int (*p)[5] = arr;
     for (i = 0; i < n; i++)
     {
         printf("NO.%d: ",i);
         for (j = 0; j < 5;j++)
         {
             printf("%d ",*(*(p+i)+j));
         }
         printf("\n");
     }
}

int *search_good(int (*point)[5])
{

     int i,count = 0;
     int *p = *(point + 1);
     float average = 0;
     for (i = 0; i < 5;i++)
     {
         average += *(*point + i);
         if (*(*point + i) >= 85)
         {
            count ++;
         }
                    
     }
     if (count == 5 || average / 5 > 90)
     {
        p = point;
     }
     return p;
}


阅读(699) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~