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

hello world.

文章分类

全部博文(308)

分类: C/C++

2010-08-22 15:56:34

    有一个班4个学生,5门课。1.求第一门课的平均分。2.找出有两门以上课程不及格的学生,并输出他们的学号和全部课程成绩和平均成绩.3.找出平均成绩在90分以上,或者全部课程成绩在85分以上的学生。分别编写三个函数实现以上3个要求。
    我们可以把学生成绩保存在二维数组,通过函数传递过去数据,在函数中定义一维数组的指针变量。根据要求,对每个学生的成绩进行扫描,输出结果。代码如下:
 

#include <stdio.h>

float average(int [][5],int,int);
void search_fail2(int [][5],int);
void search_good(int [][5],int);
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;
    /*
    printf("please input 5 number(4 line):\n");
    for (i = 0; i < 4;i++)
    {
        scanf("%d %d %d %d",&arr[i][0],&arr[i][1],&arr[i][2],&arr[i][3],&arr[i][4]);
    }
    */

    printf("the source data is :\n");
    print_source(arr,4);
    printf("\nthe 1 kemu average is :%.2f\n\n",average(arr,4,0));
    search_fail2(arr,4);
    search_good(arr,4);
    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;
}

void search_fail2(int arr[][5],int n)
{
     int i,j,(*p)[5],count = 0;
     float average = 0;
     p = arr;
     for (i = 0; i < n;i++)
     {
         for (j = 0 ;j < 5; j++)
         {
             if (*(*(p+i)+j) < 60)
             {
                count++;
             }
         }
         
         if (count >= 2)
         {
            printf("NO.%d is fall,the data is \n",i);
            for (j = 0; j < 5; j++)
            {
                printf("%d ",*(*(p+i)+j));
                average += *(*(p+i)+j);
            }
            printf("\nthe average is %.2f\n\n",average / 5);
         }
         count =0;
         average = 0;
         
     }
}

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");
     }
}

void search_good(int arr[][5],int n)
{
     int i,j,count = 0;
     int (*p)[5] = arr;
     float average = 0;
     for (i = 0; i < n ; i++)
     {
         for (j = 0; j < 5; j++)
         {
             if (*(*(p + i) + j) >= 85)
             {
                count++;
             }
             average += *(*(p + i) + j);
         }
         if (count == 5 || (average / 5 > 90) )
         {
            printf("the good student is NO.%d\n",i);
         }
         count = 0;
         average = 0;
         
     }
}


运行结果如下:

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