iT邦幫忙

0

二維動態記憶體配置,增加輸出各班平均與回收記憶體***只剩各班平均不會寫***

  • 分享至 

  • xImage
#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;}
Han iT邦研究生 1 級 ‧ 2021-09-04 21:50:06 檢舉
把上次的發問選最佳解答,海綿大大可能會考慮幫你
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

1
海綿寶寶
iT邦大神 1 級 ‧ 2021-09-04 22:35:08
最佳解答

先前問的問題如果已經解決了
就選個最佳解答

https://ithelp.ithome.com.tw/upload/images/20210904/20001787uw5s2jhub0.png

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;
}

我要發表回答

立即登入回答