求大佬帮忙写一个简单的程序
才学到数组,被这个难住了, 这个问题可以通过二维数组解决#include <stdio.h>
int main(void){
int course = 3; // 課程門數
int student_count; // 學生人數
int i,j;
printf("輸入學生人數:");
scanf("%d", &student_count);
int grade;
int student_total_score = {0};
int course_total_score = {0};
for(i=0; i<student_count; i++){
printf("學生編號 %02d 的 %d 個成績輸入(空白分隔):", i+1, course);
for(j=0; j<course; j++){
scanf("%d", &grade);
/* 當前學生的總分 */
student_total_score += grade;
/* 每門課程的總分 */
course_total_score += grade;
}
}
printf("\n**********課程成績**********\n");
printf("總分:");
for(i=0; i<course; i++)
printf("%d\t", course_total_score);
printf("\n");
printf("平均:");
for(i=0; i<course; i++)
printf("%.1f\t", float(course_total_score) / 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);
}
printf("%d\t", student_total_score);
printf("%.1f\t", float(student_total_score) / course);
printf("\n");
}
} TakeOver5 发表于 2018-11-29 06:21
这个问题可以通过二维数组解决
scanf("%d", &student_count);
int grade;
int student_total_score = {0};
int course_total_score = {0};
这里是错的,不能动态定义数组 saberAMD 发表于 2018-11-29 09:19
scanf("%d", &student_count);
int grade;
抱歉我这里使用 C++ 编译,没注意到这个问题 #include "stdio.h"
#define student_num 200 //最多可以输入200人
int main()
{
int student;
int i, student_now;
int Total_score;
int student_score = {0};
float student_total_score = {0}, average = {0, 0, 0};
scanf("请输入:%d", &student_now);
for(i = 0; i < student_now; i++)
{
printf("请输入第%d的3门成绩:\r\n", i+1);
scanf("%d, %d, %d", &student, &student, &student);
}
for(i= 0; i< student_now; i++)
{
Total_score += student;
Total_score += student;
Total_score += student;
student_score += student + student + student;
student_total_score = (float)student_score / 3;
}
average = (float)Total_score/student_now;
average = (float)Total_score/student_now;
average = (float)Total_score/student_now;
for(i= 0 ; i< student_now; i++)
{
printf("第%d位同学的总分是:%d \r\n", i+1, student_score);
printf("第%d位同学的平均分是:%f \r\n", i+1, student_total_score);
printf("全班的总分是:%d, %d, %d \r\n", Total_score, Total_score, Total_score);
printf("全班的平均分是:%f, %f, %f \r\n", average, average, average);
}
return 0;
} 本帖最后由 TakeOver5 于 2018-11-30 04:28 编辑
上方已经解决这个问题了,不过有几个小 Bug 稍微除错一下
#include "stdio.h"
#define student_num 200 //最多可以输入200人
int main()
{
int student;
int i, student_now;
int Total_score = {0};
int student_score = {0};
float student_total_score = {0}, average = {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, &student, &student);
}
for(i= 0; i< student_now; i++)
{
Total_score += student;
Total_score += student;
Total_score += student;
student_score += student + student + student;
student_total_score = (float)student_score / 3;
}
average = (float)Total_score/student_now;
average = (float)Total_score/student_now;
average = (float)Total_score/student_now;
for(i= 0 ; i< student_now; i++)
{
printf("第%d位同学的总分是:%d \r\n", i+1, student_score);
printf("第%d位同学的平均分是:%f \r\n", i+1, student_total_score);
}
printf("全班的总分是:%d, %d, %d \r\n", Total_score, Total_score, Total_score);
printf("全班的平均分是:%f, %f, %f \r\n", average, average, average);
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;
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 = data;
//int student_total_score = {0};
student_total_score = (int*)malloc(student_count*sizeof(int));
for(i=0; i<student_count; i++)
student_total_score = 0;
//int course_total_score = {0};
course_total_score = (int*)malloc(course*sizeof(int));
for(i=0; i<course; i++)
course_total_score = 0;
for(i=0; i<student_count; i++){
printf("學生編號 %02d 的 %d 個成績輸入(空白分隔):", i+1, course);
for(j=0; j<course; j++){
scanf("%d", &grade);
/* 當前學生的總分 */
student_total_score += grade;
/* 每門課程的總分 */
course_total_score += grade;
}
}
printf("\n**********課程成績**********\n");
printf("總分:");
for(i=0; i<course; i++)
printf("%d\t", course_total_score);
printf("\n");
printf("平均:");
for(i=0; i<course; i++)
printf("%.1f\t", (float)course_total_score / 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);
}
printf("%d\t", student_total_score);
printf("%.1f\t", (float)student_total_score / course);
printf("\n");
}
free(grade);
free(student_total_score);
free(course_total_score);
return 0;
}
页:
[1]