Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // main.c
- #include <stdio.h>
- #include <stdlib.h>
- #include "students.h"
- #define OUTPUT_FILE_NAME "output.txt"
- // gcc -o main main.c students.c
- int main() {
- int *students = NULL;
- // KEY is a mark, VALUE is count of students who get that mark
- int map[] = {0, 0, 0, 0, 0, 0};
- int currIndex = 0;
- int mark;
- do
- {
- printf("\nEnter a mark for %d student(0 - to exit): ", currIndex + 1);
- scanf("%d", &mark);
- if(mark != 0 && (mark < 2 || mark > 5)) printf("\nMark only can be in range (2, 5)");
- else {
- if(mark == 0) break;
- students = (int*)realloc(students, (currIndex + 1) * sizeof(int));
- students[currIndex++] = mark;
- map[mark] = map[mark] + 1;
- }
- } while (mark != 0);
- int countOfStudents = 0;
- calculate_and_print_metadata_about_students(students, map, &countOfStudents);
- print_students_to_file(OUTPUT_FILE_NAME, students, &countOfStudents);
- print_ith_student_mark_or_exit(students, &countOfStudents);
- free(students);
- return 0;
- }
- // students.h
- #pragma once
- #include <stdio.h>
- #include <stdlib.h>
- #define EXCELLENT_MARK 5
- #define NEGATIVE_MARK 2
- void print_students_to_file(char *file_name, int *students, int *countOfStudents);
- void print_ith_student_mark_or_exit(int *students, int *countOfStudents);
- void calculate_and_print_metadata_about_students(int *students, const int map[], int *countOfStudents);
- void fill_students_manually(int **students, int *map);
- void fill_students_via_random(int **students, int *map, unsigned long count);
- // students.c
- #include "students.h"
- void calculate_and_print_metadata_about_students(int *students, const int map[], int *countOfStudents) {
- double averageMark = 0.0;
- double sumOfMarks = 0;
- for(int mark = 2; mark <= 5; mark++) {
- sumOfMarks += mark * map[mark];
- *countOfStudents += map[mark];
- }
- for(int i = 0; i < *countOfStudents; i++) {
- printf("\n%5d'th student's mark is %d", i + 1, students[i]);
- }
- averageMark = sumOfMarks / *countOfStudents;
- printf("\n\nAverage mark is %.3lf", averageMark);
- printf("\n\nCount of students who get excellent mark %d", map[EXCELLENT_MARK]);
- printf("\n\nCount of students who get negative mark %d", map[NEGATIVE_MARK]);
- }
- void print_students_to_file(char *file_name, int *students, int *countOfStudents) {
- FILE *output = fopen(file_name, "w");
- if(output != NULL) {
- for(int i = 0; i < *countOfStudents; i++) {
- fprintf(output, "\n%5d'th student's mark is %d", i + 1, students[i]);
- fputs("\n", output);
- }
- } else {
- printf("printToFile Error: file does not exist yet");
- }
- fclose(output);
- }
- void print_ith_student_mark_or_exit(int *students, int *countOfStudents) {
- int numberOfStudent = 0;
- do
- {
- printf("\nEnter a number of student who mark you want to see(0 - to exit): ");
- scanf("%d", &numberOfStudent);
- if(numberOfStudent < 0 || numberOfStudent > *countOfStudents) {
- printf("\nUser by %d number cannot be find...", numberOfStudent);
- } else {
- if(numberOfStudent > 0) {
- printf("%d'th student's mark is %d", numberOfStudent, students[numberOfStudent - 1]);
- }
- }
- } while (numberOfStudent != 0);
- }
- void fill_students_manually(int **students, int *map) {
- int currIndex = 0;
- int mark;
- do
- {
- printf("\nEnter a mark for %d student(0 - to exit): ", currIndex + 1);
- scanf("%d", &mark);
- if(mark != 0 && (mark < 2 || mark > 5)) printf("\nMark only can be in range (2, 5)");
- else {
- if(mark == 0) break;
- *students = realloc(*students, (currIndex + 1) * sizeof(int));
- *students[currIndex++] = mark;
- map[mark] = map[mark] + 1;
- }
- } while (mark != 0);
- }
- void fill_students_using_random(int **students, int *map, unsigned long count) {
- int currIndex = 0;
- for(int i = 0; i < count; i++) {
- int mark = rand() % (EXCELLENT_MARK - NEGATIVE_MARK + 1) + NEGATIVE_MARK;
- *students = (int*)realloc(*students, (currIndex + 1) * sizeof(int));
- *students[currIndex++] = mark;
- map[mark] = map[mark] + 1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement