Question: #include #include #include #include #define PASS _ THRESHOLD 5 0 / / Minimum average to pass / / Global variables for statistics int * students

#include
#include
#include
#include
#define PASS_THRESHOLD 50// Minimum average to pass
// Global variables for statistics
int *students_passing_per_question; // Array for passing stats per question
int total_students_passing =0;
int highest_grade =0, lowest_grade =100; // Start with extreme values
sem_t stats_semaphore; // Semaphore to protect shared stats
typedef struct {
int student_id;
int num_grades;
int *grades;
} StudentData;
// Function to process each student's data
void* process_student(void* arg){
StudentData* data =(StudentData*)arg;
float sum =0;
for (int i =0; i data->num_grades; i++){
sum += data->grades[i];
// Update per-question passing stats safely
if (data->grades[i]>= PASS_THRESHOLD){
sem_wait(&stats_semaphore);
students_passing_per_question[i]++;
sem_post(&stats_semaphore);
}
}
float average = sum / data->num_grades;
// Update overall stats safely
sem_wait(&stats_semaphore);
if (average >= PASS_THRESHOLD){
total_students_passing++;
}
for (int i =0; i data->num_grades; i++){
if (data->grades[i]> highest_grade){
highest_grade = data->grades[i];
}
if (data->grades[i] lowest_grade){
lowest_grade = data->grades[i];
}
}
sem_post(&stats_semaphore);
// Print student result
printf("%d %.2f %s
", data->student_id, average, average >= PASS_THRESHOLD ? "Passed" : "Failed");
pthread_exit(NULL);
}
int main(){
FILE *input_file = fopen("input.txt","r");
if (input_file == NULL){
perror("Error opening input file");
return 1;
}
int num_students, num_questions;
fscanf(input_file, "%d", &num_students);
fscanf(input_file, "%d", &num_questions);
students_passing_per_question =(int*)calloc(num_questions, sizeof(int));
sem_init(&stats_semaphore, 0,1);
pthread_t threads[num_students];
StudentData student_data[num_students];
for (int i =0; i num_students; i++){
student_data[i].grades =(int*)malloc(num_questions * sizeof(int));
student_data[i].num_grades = num_questions;
fscanf(input_file, "%d", &student_data[i].student_id);
for (int j =0; j num_questions; j++){
fscanf(input_file, "%d", &student_data[i].grades[j]);
}
pthread_create(&threads[i], NULL, process_student, &student_data[i]);
}
for (int i =0; i num_students; i++){
pthread_join(threads[i], NULL);
free(student_data[i].grades);
}
fclose(input_file);
// Print overall statistics
printf("
--- Overall Statistics ---
");
printf("Number of students passing each question:
");
for (int i =0; i num_questions; i++){
printf("Question %d: %d students passed.
", i +1, students_passing_per_question[i]);
}
printf("Total number of students who passed overall: %d
", total_students_passing);
printf("Highest grade: %d
", highest_grade);
printf("Lowest grade: %d
", lowest_grade);
free(students_passing_per_question);
sem_destroy(&stats_semaphore);
return 0;
}
In this code, my output should be like the result output when given the input I have added in the photos, but my result is like the result of out32. I can not understand the problem, I need a correction in my code, thanks in advance.
input:
3
4
10178859045
10245605530
10388929590
results.txt :
Dosya Dzen Biim Grnm Yardm
10174.50 Passed
10247.50 Failed
10391.25 Passed
--- Overall Statistics ---
Number of students passing each question:
Question 1: 2 students passed.|
Question 2: 3 students passed.
Question 3: 2 students passed.
Question 4: 1 students passed.
Total number of students who passed overall: 2
Highest grade: 95
Lowest grade: 30
out32:
10174.50 Passed
10247.50 Failed
10391.25 Passed
--- Overall Statistics ---
Number of students passing each question:
Question 1: 2 students passed.
Question 2: 3 students passed.
Question 3: 3 students passed.
Question 4: 1 students passed.
Total number of students who passed overall: 2
Highest grade: 95
Lowest grade: 30
input.txt - Not Defteri
#include #include #include #include #define PASS

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!