Question: #include #include #include #include #define PASS _ THRESHOLD 6 0 int * * grades; int num _ students, num _ questions; int * question _

#include
#include
#include
#include
#define PASS_THRESHOLD 60
int **grades;
int num_students, num_questions;
int *question_pass_counts;
int total_pass_count =0;
int max_grade =0, min_grade =100;
sem_t sem;
typedef struct {
int id;
float average;
int passed;
} Student;
Student *students;
void read_data(const char *filename){
FILE *file = fopen(filename,"r");
if (!file){
perror("Error opening input file");
exit(EXIT_FAILURE);
}
fscanf(file,"%d %d", &num_students, &num_questions);
grades = malloc(num_students * sizeof(int *));
for (int i =0; i < num_students; i++){
grades[i]= malloc(num_questions * sizeof(int));
}
students = malloc(num_students * sizeof(Student));
question_pass_counts = calloc(num_questions, sizeof(int));
for (int i =0; i < num_students; i++){
fscanf(file,"%d", &students[i].id);
for (int j =0; j < num_questions; j++){
fscanf(file,"%d", &grades[i][j]);
}
}
fclose(file);
}
void *process_student(void *arg){
int student_index =*(int *)arg;
free(arg);
float sum =0;
int local_max =0, local_min =100;
for (int j =0; j < num_questions; j++){
int grade = grades[student_index][j];
sum += grade;
if (grade >= PASS_THRESHOLD){
sem_wait(&sem);
question_pass_counts[j]++;
sem_post(&sem);
}
if (grade > local_max) local_max = grade;
if (grade < local_min) local_min = grade;
}
students[student_index].average = sum / num_questions;
students[student_index].passed =(students[student_index].average >= PASS_THRESHOLD);
sem_wait(&sem);
if (students[student_index].passed) total_pass_count++;
if (local_max > max_grade) max_grade = local_max;
if (local_min < min_grade) min_grade = local_min;
sem_post(&sem);
return NULL;
}
void write_results(const char *filename){
FILE *file = fopen(filename,"w");
if (!file){
perror("Error opening output file");
exit(EXIT_FAILURE);
}
for (int i =0; i < num_students; i++){
fprintf(file,"%d %.2f %s
", students[i].id, students[i].average,
students[i].passed ? "Passed" : "Failed");
}
fprintf(file,"
--- Overall Statistics ---
");
fprintf(file, "Number of students passing each question:
");
for (int i =0; i < num_questions; i++){
fprintf(file, "Question %d: %d students passed.
", i +1, question_pass_counts[i]);
}
fprintf(file, "Total number of students who passed overall: %d
", total_pass_count);
fprintf(file, "Highest grade: %d
", max_grade);
fprintf(file, "Lowest grade: %d
", min_grade);
fclose(file);
}
int main(){
read_data("input.txt");
sem_init(&sem, 0,1);
pthread_t *threads = malloc(num_students * sizeof(pthread_t));
for (int i =0; i < num_students; i++){
int *arg = malloc(sizeof(int));
*arg = i;
pthread_create(&threads[i], NULL, process_student, arg);
}
for (int i =0; i < num_students; i++){
pthread_join(threads[i], NULL);
}
write_results("results.txt");
for (int i =0; i < num_students; i++){
free(grades[i]);
}
free(grades);
free(question_pass_counts);
free(students);
free(threads);
sem_destroy(&sem);
return 0;
}
can you solve the above code with a different solution method using multithreading and semophore again
I added sample input and output below
input:
3
4
10178859045
10245605530
10388929590
output:
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

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!