想寻求帮助!我为3个科目(数学,英语和自然科学)打分,可以分为三个不同的组。
我根据建议使用“结构”。
我无法打印每个主题和整个主题的平均值。平均值显示为零。
for (int i = 0; i < num && i < Max_NUM; i++)
{
for (j = 0; j < 3; j++)
{ //loop calculating sum of array elements
sum=(sum+std[i].subject[j]); //sum of mark for 3 subjects
sum2=sum/num;
std[i].subject[j] =(sum2/Max_Grade)*100; // average mark
//for total average mark (sub 0 avg mark+ sub 1 avg mark+ sub 2 avg mark)
totalSUB =(std[i].subject[0]+std[i].subject[1]+std[i].subject[2])/3;
}
//to print 3lines: for average marks of each subject: sub 0, sub 1, sub2
printf("\nAverage mark: %.2f",std[i].subject[j]);
//to print average marks for each subject: sub 0, sub 1, sub2
printf("\nTotalSUB- Average mark: %.2f",totalSUB);
return 0;
} //end of if
#include <stdio.h>
#include <stdlib.h>
#define Max_Grade 415
#define Max_NUM 150
int main()
{
int i,j,num;//variable declaration
double avg=0.0,sum=0.0, sum2=0.0, totalSUB=0.0;
struct student
{
double subject[3];
};
struct student std[Max_NUM];
printf("Enter number of students: ");
scanf("%d",&num);
// for loop to read i'th student's j'th subject
for (int i = 0; i < num && i < Max_NUM; i++)
{
for (j = 0; j < 3; j++)
{
printf("Enter marks of student %d for subject %d: ", i, j);
scanf("%f", &std[i].subject[j]);
//sleep(2);
}
}
for (int i = 0; i < num && i < Max_NUM; i++)
{
for (j = 0; j < 3; j++)
{ //loop calculating sum of array elements
sum=(sum+std[i].subject[j]); //sum of mark for 3 subjects
sum2=sum/num;
std[i].subject[j] =(sum2/Max_Grade)*100; // average mark
//for total average mark (sub 0 avg mark+ sub 1 avg mark+ sub 2 avg mark)
totalSUB =(std[i].subject[0]+std[i].subject[1]+std[i].subject[2])/3;
}
//to print 3lines: for average marks of each subject: sub 0, sub 1, sub2
printf("\nAverage mark: %.2f",std[i].subject[j]);
//to print average marks for each subject: sub 0, sub 1, sub2
printf("\nTotalSUB- Average mark: %.2f",totalSUB);
return 0;
} //end of if
} //end of int main()
您将达到如下所示的效果。用您想要的科目构建结构,并为那么多学生分配记忆。在每个学生std
的循环中,您可以阅读每个学生的学科标记。同样,您也可以计算平均分数。
struct student {
int subject[3];
};
struct student std[Max_NUM];
printf("Enter number of students: ");
scanf("%d",&num);
/* for loop to read i'th student's j'th subject*/
for (int i = 0; i < num && i < Max_NUM; i++) {
for (j = 0; j < 3; j++) {
printf("Enter marks of student %d for subject %d: ", i, j);
scanf("%d", &std[i].subject[j]);
}
}