1、功能:輸入6個(gè)學(xué)生的5門課程成績(jī),計(jì)算出每個(gè)學(xué)生的平均分和每門課程的平均分。
2、C語言實(shí)現(xiàn)代碼:(其實(shí)就是用二維數(shù)組來實(shí)現(xiàn)的,二維數(shù)組的引用傳遞使用數(shù)組指針來完成)
代碼如下:
#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);
}
}
更多信息請(qǐng)查看IT技術(shù)專欄