Advertisement
Kostiggig

Untitled

Feb 19th, 2023
524
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.34 KB | None | 0 0
  1. // main.c
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5.  
  6. #include "students.h"
  7.  
  8. #define OUTPUT_FILE_NAME "output.txt"
  9.  
  10. // gcc -o main main.c students.c
  11.  
  12. int main() {
  13.     int *students = NULL;
  14.     // KEY is a mark, VALUE is count of students who get that mark
  15.     int map[] = {0, 0, 0, 0, 0, 0};
  16.  
  17.     int currIndex = 0;
  18.     int mark;
  19.     do
  20.     {
  21.         printf("\nEnter a mark for %d student(0 - to exit): ", currIndex + 1);
  22.         scanf("%d", &mark);
  23.         if(mark != 0 && (mark < 2 || mark > 5)) printf("\nMark only can be in range (2, 5)");
  24.         else {
  25.             if(mark == 0) break;
  26.             students = (int*)realloc(students, (currIndex + 1) * sizeof(int));
  27.             students[currIndex++] = mark;
  28.             map[mark] = map[mark] + 1;
  29.         }
  30.     } while (mark != 0);
  31.  
  32.     int countOfStudents = 0;
  33.     calculate_and_print_metadata_about_students(students, map, &countOfStudents);
  34.     print_students_to_file(OUTPUT_FILE_NAME, students, &countOfStudents);
  35.     print_ith_student_mark_or_exit(students, &countOfStudents);    
  36.    
  37.     free(students);
  38.     return 0;
  39. }
  40.  
  41. // students.h
  42.  
  43. #pragma once
  44. #include <stdio.h>
  45. #include <stdlib.h>
  46.  
  47. #define EXCELLENT_MARK 5
  48. #define NEGATIVE_MARK 2
  49.  
  50. void print_students_to_file(char *file_name, int *students, int *countOfStudents);
  51. void print_ith_student_mark_or_exit(int *students, int *countOfStudents);
  52.  
  53. void calculate_and_print_metadata_about_students(int *students, const int map[], int *countOfStudents);
  54.  
  55. void fill_students_manually(int **students, int *map);
  56. void fill_students_via_random(int **students, int *map, unsigned long count);
  57.  
  58. // students.c
  59.  
  60. #include "students.h"
  61.  
  62. void calculate_and_print_metadata_about_students(int *students, const int map[], int *countOfStudents) {
  63.     double averageMark = 0.0;
  64.     double sumOfMarks = 0;
  65.    
  66.     for(int mark = 2; mark <= 5; mark++) {
  67.         sumOfMarks += mark * map[mark];
  68.         *countOfStudents += map[mark];
  69.     }
  70.    
  71.     for(int i = 0; i < *countOfStudents; i++) {
  72.         printf("\n%5d'th student's mark is %d", i + 1, students[i]);
  73.     }
  74.  
  75.     averageMark = sumOfMarks / *countOfStudents;
  76.     printf("\n\nAverage mark is %.3lf", averageMark);
  77.  
  78.     printf("\n\nCount of students who get excellent mark %d", map[EXCELLENT_MARK]);
  79.     printf("\n\nCount of students who get negative mark %d", map[NEGATIVE_MARK]);
  80. }
  81.  
  82. void print_students_to_file(char *file_name, int *students, int *countOfStudents) {
  83.     FILE *output = fopen(file_name, "w");
  84.     if(output != NULL) {
  85.         for(int i = 0; i < *countOfStudents; i++) {
  86.             fprintf(output, "\n%5d'th student's mark is %d", i + 1, students[i]);
  87.             fputs("\n", output);
  88.         }
  89.     } else {
  90.         printf("printToFile Error: file does not exist yet");
  91.     }
  92.     fclose(output);
  93. }
  94.  
  95. void print_ith_student_mark_or_exit(int *students, int *countOfStudents) {
  96.     int numberOfStudent = 0;
  97.     do
  98.     {
  99.         printf("\nEnter a number of student who mark you want to see(0 - to exit): ");
  100.         scanf("%d", &numberOfStudent);
  101.  
  102.         if(numberOfStudent < 0 || numberOfStudent > *countOfStudents) {
  103.             printf("\nUser by %d number cannot be find...", numberOfStudent);
  104.         } else {
  105.             if(numberOfStudent > 0) {
  106.                 printf("%d'th student's mark is %d", numberOfStudent, students[numberOfStudent - 1]);
  107.             }
  108.         }
  109.     } while (numberOfStudent != 0);
  110. }
  111.  
  112. void fill_students_manually(int **students, int *map) {
  113.     int currIndex = 0;
  114.     int mark;
  115.     do
  116.     {
  117.         printf("\nEnter a mark for %d student(0 - to exit): ", currIndex + 1);
  118.         scanf("%d", &mark);
  119.         if(mark != 0 && (mark < 2 || mark > 5)) printf("\nMark only can be in range (2, 5)");
  120.         else {
  121.             if(mark == 0) break;
  122.             *students = realloc(*students, (currIndex + 1) * sizeof(int));
  123.             *students[currIndex++] = mark;
  124.             map[mark] = map[mark] + 1;
  125.         }
  126.     } while (mark != 0);
  127. }
  128. void fill_students_using_random(int **students, int *map, unsigned long count) {
  129.     int currIndex = 0;
  130.     for(int i = 0; i < count; i++) {
  131.         int mark = rand() % (EXCELLENT_MARK - NEGATIVE_MARK + 1) + NEGATIVE_MARK;
  132.         *students = (int*)realloc(*students, (currIndex + 1) * sizeof(int));
  133.         *students[currIndex++] = mark;
  134.         map[mark] = map[mark] + 1;
  135.     }
  136. }
  137.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement