- //一个学习小组有5个人,每人有3门课成绩,构建二维数组,遍历显示并求每门课程的平均分和三门课程的总平均分
- #include <stdio.h>
- #define M 5
- #define N 3
- int i,j;
- void printAvg(int arr[M][N]){
-
- float avg = 0;
- for(j=0;j<N;j++){
- int sum = 0;
- for(i=0;i<M;i++){
- sum += arr[i][j];
- }
- avg += (sum/5.0f);
- printf("第%d门课程的平均分是%.2f\n",j+1,sum/5.0f);
- }
- printf("三门课程的总平均分是%.2f\n",avg/3.0f);
- }
- int main(){
- int a[M][N] = {53,28,39,45,69,88,92,56,12,35,49,100,68,58,47};
-
- for(i=0;i<M;i++){
- for(j=0;j<N;j++){
- printf("a[%d][%d] = %d\t",i,j,a[i][j]);
- }
- printf("\n");
- }
- printAvg(a);
- return 0;
- }
复制代码
|
|