展会信息港展会大全

C语言数组指针的小例子
来源:互联网   发布日期:2016-01-26 10:19:01   浏览:1327次  

导读:1、功能:输入6个学生的5门课程成绩,计算出每个学生的平均分和每门课程的平均分。2、C语言实现代码:(其实就是用二维数组来实现的,二维数组的引用传递使用数组指针来完成)复制代码 代码如下: include std ...

1、功能:输入6个学生的5门课程成绩,计算出每个学生的平均分和每门课程的平均分。

2、C语言实现代码:(其实就是用二维数组来实现的,二维数组的引用传递使用数组指针来完成)

复制代码 代码如下:

#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=%fn" , 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);

}

}

赞助本站

人工智能实验室

相关热词: 数组指针 C语言

AiLab云推荐
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港