#include<stdio.h>
#include<stdlib.h>
int main(){
int i,j;
double sum = 0,aver;
int **student;
int m,n;
double Avg;
scanf("%d",&m);
scanf("%d",&n);
student = (int **)malloc(sizeof(int *) * m);
for(j=0;j<m;j++)
student[j] = (int *)malloc(sizeof(int) * n);
for(j=0;j<m;j++){
printf("class%d:\n",j+1);
for(i=0;i<n;i++){
printf("stu%d:",i+1);
scanf("%d",&student[j][i]);
}
}
for(j=0;j<m;j++)
for(i=0;i<n;i++)
sum+=student[j][i];
aver=sum/(m*n);
printf("avg= %lf\n",aver);
return 0;}
先前問的問題如果已經解決了
就選個最佳解答
2
9
1 2 3 4 5 6 7 8 10
11 21 31 41 51 61 71 81 94
#include<stdio.h>
#include<stdlib.h>
int main(){
int i,j;
double sum = 0,aver;
int **student;
int m,n;
double Avg;
double *AvgClass, SumClass;
scanf("%d",&m);
scanf("%d",&n);
student = (int **)malloc(sizeof(int *) * m);
AvgClass = (double *)malloc(sizeof(double) * m);
for(j=0;j<m;j++)
student[j] = (int *)malloc(sizeof(int) * n);
for(j=0;j<m;j++){
printf("class%d:\n",j+1);
for(i=0;i<n;i++){
printf("stu%d:",i+1);
scanf("%d",&student[j][i]);
}
}
printf("\n");
for(j=0;j<m;j++) {
SumClass = 0;
printf("Class %d :", j+1);
for(i=0;i<n;i++) {
printf("%4d ", student[j][i]);
sum+=student[j][i];
SumClass+=student[j][i];
}
AvgClass[j] = SumClass / n;
printf("Sum=%.0f, Avg=%lf\n", j+1, SumClass, AvgClass[j]);
}
aver=sum/(m*n);
printf("avg= %lf\n",aver);
return 0;
}