C //例 7.10 有一个一维数组score,内放10个学生成绩,求平均成绩。

编程入门 行业动态 更新时间:2024-10-26 17:24:40

C //例 7.10 有一个一维数组score,内放10个学生<a href=https://www.elefans.com/category/jswz/34/1769940.html style=成绩,求平均成绩。"/>

C //例 7.10 有一个一维数组score,内放10个学生成绩,求平均成绩。

C程序设计 (第四版) 谭浩强 例 7.10

例 7.10 有一个一维数组score,内放10个学生成绩,求平均成绩。

IDE工具:VS2010
Note: 使用不同的IDE工具可能有部分差异。

 

代码块
方法:使用指针、动态分配内存
#include <stdio.h>
#include <stdlib.h>#define N 10void initialScore(float **score, int n){*score = (float*)malloc(n * sizeof(float));
}void inputScore(float *score, int n){printf("Enter the scores of %d students: ", n);for(int i = 0; i < n; i++){scanf("%f", &score[i]);}
}float average(float *score, int n){float sum = 0.0;for(int i = 0; i < n; i++){sum += score[i];}return sum / n;
}void freeScore(float **score){free(*score);
}int main(){float *score = NULL;initialScore(&score, N);inputScore(score, N);printf("Average = %.2f\n", average(score, N));freeScore(&score);system("pause");return 0;
}

更多推荐

C //例 7.10 有一个一维数组score,内放10个学生成绩,求平均成绩。

本文发布于:2023-11-15 02:21:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1592038.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:成绩   数组   有一个   平均   学生

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!