У меня есть 2 структуры с именами Class и Student. Я хочу рассчитать среднюю оценку студентов, обратившись к массиву Student[30] в структуре Class.
Вот мой код:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <Math.h>
typedef struct Student{
char name [20] ;
int grade ;
int number ;
} Student;
typedef struct Class{
char className[20] ;
Student Students[30] ;
} Class;
double calculateAverageGrade(/* I want to pass the Students[30] array here */ ) {
int i ;
int sum = 0 ;
for(i=0;i<3;i++) {
}
return sum/3 ;
}
int main() {
Student s1 = {"John",75,758} ;
Student s2 = {"Jack",85,123} ;
Student s3 = {"Lisandra",50,321} ;
Class c1 = {'\0'} ;
strcpy(c1.className,"Physics") ;
c1.Students[0] = s1 ;
c1.Students[1] = s2 ;
c1.Students[2] = s3 ;
calculateAverageGrade(/*needs to take Students[30] array*/);
return 0 ;
}
Я пробовал что-то Class Students[30] , Class.Student Students[30] подобное, но они не работали.





Вы можете рассчитать средний балл вот так, почти как в вашей программе. Обратите внимание, что я добавил количество студентов в вызов функции, но ваш тип Class действительно должен содержать это в любом случае, и было бы лучше передать Class в функцию, а не только содержащийся в ней массив.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
typedef struct Student{
char name [20];
int grade;
int number;
}Student;
typedef struct Class{
char className[20];
Student Students[30];
}Class;
double calculateAverageGrade(Student *Students, int count) { // pass the number of students
int i;
int sum = 0;
for(i = 0; i < count; i++) { // don't guess the number of students
sum += Students[i].grade;
}
return (double)sum / count; // otherwise integer division
}
int main(void) {
Student s1 = { "John", 75, 758 };
Student s2 = { "Jack", 85, 123 };
Student s3 = { "Lisandra", 50, 321 };
Class c1 = {'\0'};
strcpy(c1.className, "Physics");
c1.Students[0] = s1;
c1.Students[1] = s2;
c1.Students[2] = s3;
printf("The average grade in %s is %.2f\n", c1.className,
calculateAverageGrade(c1.Students, 3));
return 0;
}
Вывод программы:
The average grade in Physics is 70.00
Привет! Опубликуйте Минимальный, полный и проверяемый пример, который показывает проблему. Это также должно показать файлы
#include, которые вы используете. Трудно читать фрагменты кода с двойным или тройным интервалом или компилировать их.