|
发表于 2018-11-30 03:54:44
|
显示全部楼层
本楼为最佳答案
本帖最后由 TakeOver5 于 2018-11-30 04:28 编辑
上方已经解决这个问题了,不过有几个小 Bug 稍微除错一下
- #include "stdio.h"
- #define student_num 200 //最多可以输入200人
- int main()
- {
- int student[student_num][3];
- int i, student_now;
- int Total_score[3] = {0};
- int student_score[student_num] = {0};
- float student_total_score[student_num] = {0}, average[3] = {0, 0, 0};
- printf("请输入:");
- scanf("%d", &student_now);
- for(i = 0; i < student_now; i++)
- {
- printf("请输入第%d的3门成绩:", i+1);
- scanf("%d, %d, %d", &student[i][0], &student[i][1], &student[i][2]);
- }
- for(i= 0; i< student_now; i++)
- {
- Total_score[0] += student[i][0];
- Total_score[1] += student[i][1];
- Total_score[2] += student[i][2];
- student_score[i] += student[i][0] + student[i][1] + student[i][2];
- student_total_score[i] = (float)student_score[i] / 3;
- }
- average[0] = (float)Total_score[0]/student_now;
- average[1] = (float)Total_score[1]/student_now;
- average[2] = (float)Total_score[2]/student_now;
- for(i= 0 ; i< student_now; i++)
- {
- printf("第%d位同学的总分是:%d \r\n", i+1, student_score[i]);
- printf("第%d位同学的平均分是:%f \r\n", i+1, student_total_score[i]);
- }
-
- printf("全班的总分是:%d, %d, %d \r\n", Total_score[0], Total_score[1], Total_score[2]);
- printf("全班的平均分是:%f, %f, %f \r\n", average[0], average[1], average[2]);
- return 0;
- }
复制代码
不过若想在 C 语言完成动态数组,那么可以使用 malloc() 函数实现
- #include <stdio.h>
- #include <stdlib.h>
- int main(void){
-
- int course = 3; // 課程門數
- int student_count; // 學生人數
- int i,j;
-
- printf("輸入學生人數:");
- scanf("%d", &student_count);
-
- int **grade, *data;
- int *student_total_score, *course_total_score;
-
- //int grade[student_count][course];
- grade = (int**)malloc(student_count*sizeof(int*) + student_count*course*sizeof(int));
- data = (int*)grade+student_count;
- for(i=0; i<student_count; i++, data+=course)
- grade[i] = data;
-
- //int student_total_score[student_count] = {0};
- student_total_score = (int*)malloc(student_count*sizeof(int));
- for(i=0; i<student_count; i++)
- student_total_score[i] = 0;
-
- //int course_total_score[course] = {0};
- course_total_score = (int*)malloc(course*sizeof(int));
- for(i=0; i<course; i++)
- course_total_score[i] = 0;
-
- for(i=0; i<student_count; i++){
- printf("學生編號 %02d 的 %d 個成績輸入(空白分隔):", i+1, course);
- for(j=0; j<course; j++){
- scanf("%d", &grade[i][j]);
- /* 當前學生的總分 */
- student_total_score[i] += grade[i][j];
-
- /* 每門課程的總分 */
- course_total_score[j] += grade[i][j];
- }
- }
-
- printf("\n**********課程成績**********\n");
-
- printf("總分:");
- for(i=0; i<course; i++)
- printf("%d\t", course_total_score[i]);
-
- printf("\n");
-
- printf("平均:");
- for(i=0; i<course; i++)
- printf("%.1f\t", (float)course_total_score[i] / student_count);
-
- printf("\n**********個人成績**********\n");
-
- /* 打印所有學生的分數、總分和平均分 */
- for(i=0; i<student_count; i++){
- printf("學生編號 %02d:\t", i+1);
- for(j=0; j<course; j++){
- printf("%d\t", grade[i][j]);
- }
- printf("%d\t", student_total_score[i]);
- printf("%.1f\t", (float)student_total_score[i] / course);
- printf("\n");
- }
-
- free(grade);
- free(student_total_score);
- free(course_total_score);
-
- return 0;
- }
复制代码 |
|