源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

C语言数组指针的小例子

  • 时间:2022-01-24 05:07 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:C语言数组指针的小例子
1、功能:输入6个学生的5门课程成绩,计算出每个学生的平均分和每门课程的平均分。 2、C语言实现代码:(其实就是用二维数组来实现的,二维数组的引用传递使用数组指针来完成)
[u]复制代码[/u] 代码如下:
#include <stdio.h> #define STUDENT 5 #define SCORE 6 void input_array(float (*score)[STUDENT]); void avg_score(float (*score)[STUDENT]); void avg_course(float (*score)[STUDENT]); /**  * calculate student average score and course average socore.  */ int main(){     float a[SCORE][STUDENT];     input_array(a);     avg_course(a);     avg_score(a); } void input_array(float (*score)[STUDENT]){     int i, j;     for(i=0; i<SCORE; i++){         printf("input the %d student score:", i+1);         for(j=0; j<STUDENT; j++){             scanf("%f", score[i] + j);         }     } } void avg_course(float (*score)[STUDENT]){     int i,j;     float s;     for(j=0; j<STUDENT; j++){         printf("course%d  ", j);     }     printf("\n");     for(i=0; i<SCORE; i++){         s=0;         for(j=0; j<STUDENT; j++){             printf("%f  ", *(score[i] + j));             s += *(score[i] + j);         }         printf("\ts=%f, avg=%f\n" , s,s/STUDENT);     } } void avg_score(float (*score)[STUDENT]){     int i,j;     float s;     for(i=0; i<STUDENT; i++){         s = 0;         for(j=0; j<SCORE;j++){             s+= *(score[j]+i);         }         printf("%f  " , s/SCORE);     } }
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部