Advertisement
informaticage

Example struct

Oct 6th, 2017
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.28 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int main()
  5. {
  6.     typedef struct Student
  7.     {
  8.       char name[100];
  9.       char birth_date[12];
  10.       float scores[10];
  11.     } Student;
  12.  
  13.     int student_number;
  14.     printf("Number of students: ");
  15.     scanf("%d", &student_number);
  16.  
  17.     Student *students = malloc(sizeof(Student) * student_number);
  18.     int i;
  19.     for(i = 0; i < student_number; i++) {
  20.         printf("Student number %d", i);
  21.         printf("Name, Birth date %d", i);
  22.         scanf("%s %s", &students[i].name, &students[i].birth_date);
  23.         printf("Number of exams for the student %s", students[i].name);
  24.         int exam_number;
  25.         scanf("%d", &exam_number);
  26.         int j;
  27.         for(j = 0; j < exam_number; j++) {
  28.             printf("Exam number %d", j);
  29.             scanf("%f", &students[i].scores[j]);
  30.         }
  31.     }
  32.  
  33.     printf("Printing student average: \n");
  34.     for(i = 0; i < student_number; i++) {
  35.         printf("Student number %d - Name: %s - date of bird: %s ", i, students[i].name, students[i].birth_date);
  36.         float sum = 0;
  37.         int j;
  38.         for(j = 0; j < sizeof(students[i].scores) / sizeof(int); j++) {
  39.             sum += students[i].scores[j];
  40.         }
  41.         printf("Student average: %f", sum);
  42.     }
  43.     return 0;
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement