Skip to content

Instantly share code, notes, and snippets.

@TopRoupi
Created March 7, 2024 13:39
Show Gist options
  • Save TopRoupi/535031b0b8af2fe8a134e240e51e2bae to your computer and use it in GitHub Desktop.
Save TopRoupi/535031b0b8af2fe8a134e240e51e2bae to your computer and use it in GitHub Desktop.
EX structs
//gerenciar informações sobre alunos de um curso de ateísmo
#include <stdio.h>
#include <stdlib.h>
struct Exam {
char topic[50];
float grade;
};
struct Student {
char name[100];
char code[100];
struct Exam *exams;
};
struct Exam *constructExams() {
//struct Exam exams[3];
struct Exam *exams = malloc(sizeof(struct Exam) * 3);
//são três avaliações, uma sobre os planetas, outra sobre os Jedi e uma sobre o enredo
struct Exam planetsExam = {"planets", 0};
struct Exam jediExam = {"jedi", 0};
struct Exam plotExam = {"plot", 0};
exams[0] = planetsExam;
exams[1] = jediExam;
exams[2] = plotExam;
return exams;
}
float studentGradesAverage(struct Student student) {
float total = 0;
total += student.exams[0].grade;
total += student.exams[1].grade;
total += student.exams[2].grade;
return total / 3;
}
float studentExamsAverage(struct Student student) {
float total = 0;
total += student.exams[0].grade;
total += student.exams[1].grade;
total += student.exams[2].grade;
return total / 3;
}
float studentFirstTwoExamsAverage(struct Student student) {
float total = 0;
total += student.exams[0].grade;
total += student.exams[1].grade;
return total / 2;
}
void studentInspect(struct Student student) {
printf("-----------------STUDENT-INSPECT---------------------\n");
printf(" student name: %s\n", student.name);
printf(" student code: %s\n", student.code);
printf(" -----------------GRADES-INSPECT---------------------\n");
printf(" student exam1 topic: %s\n", student.exams[0].topic);
printf(" student exam1 grade: %.2f\n", student.exams[0].grade);
printf(" ----------------------------------------------------\n");
printf(" student exam2 topic: %s\n", student.exams[1].topic);
printf(" student exam2 grade: %.2f\n", student.exams[1].grade);
printf(" ----------------------------------------------------\n");
printf(" student exam3 topic: %s\n", student.exams[2].topic);
printf(" student exam3 grade: %.2f\n", student.exams[2].grade);
printf(" ----------------------------------------------------\n");
printf(" student first two grades average: %.2f\n", studentFirstTwoExamsAverage(student));
printf(" student grades average: %.2f\n", studentExamsAverage(student));
}
//Implemente funções que recebam esse array como argumento para calcular médias e exibir informações
int main()
{
// brazil is a catholic country i bet a school for atheists wont need many students
int studentsNumber = 3;
struct Student students[studentsNumber];
// DRY is a meme
struct Student student1 = {"Nil", "0001", constructExams()};
student1.exams[0].grade = 6;
student1.exams[1].grade = 4;
student1.exams[2].grade = 7;
students[0] = student1;
struct Student student2 = {"Sena", "0002", constructExams()};
student2.exams[0].grade = 3;
student2.exams[1].grade = 4;
student2.exams[2].grade = 1;
students[1] = student2;
struct Student student3 = {"Cloe", "0003", constructExams()};
student3.exams[0].grade = 5;
student3.exams[1].grade = 8;
student3.exams[2].grade = 6;
students[2] = student3;
for(int i = 0; i < studentsNumber; i++) {
studentInspect(students[i]);
}
//struct Student student = {"Nil", "0001", constructExams()};
//studentInspect(student);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment