Question
It should be twi dismantions. and it should dyanamically create GTAs and TAs processes based on the the number of the chpaters (you can assume
It should be twi dismantions. and it should dyanamically create GTAs and TAs processes based on the the number of the chpaters (you can assume that each chapter will always have two assingments).
The program should have a teacher process that reads the grades ("sample_in_grades.txt" grades file is available under the Assignment 1 section on Myls) of all assignments of all the chapters and creates a two dimensional matrix of grades
#include
#include
#include
#include
#include
#include
#define MAX_STUDENTS 10
#define MAX_CHAPTERS 3
#define MAX_ASSIGNMENTS 2
void writeOutput(char* command, char* output) {
/* Complete code to save the commands in a out`put.txt*/
FILE *fp;
fp = fopen("output.txt", "a");
if (fp == NULL) {
printf("Error opening file ");
exit(1);
}
fprintf(fp, "The output of: %s : is ", command);
fprintf(fp, ">>>>>>>>>>>>>>> %s<<<<<<<<<<<<<<< ", output);
/* Your code here*/
fclose(fp);
}
int main(int argc, char* argv[]) { //
int grades[MAX_STUDENTS][MAX_CHAPTERS][MAX_ASSIGNMENTS];
// Read grades from file
char* filename = argv[1];
FILE *file;
file = fopen(filename, "r");
if (file == NULL) {
printf("Error opening file ");
exit(1);
}
int student, chapter, assignment;
for (student = 0; student < MAX_STUDENTS; student++) {
for (chapter = 0; chapter < MAX_CHAPTERS; chapter++) {
for (assignment = 0; assignment < MAX_ASSIGNMENTS; assignment++) {
fscanf(file, "%d", &grades[student][chapter][assignment]);
}
}
}
fclose(file);
// Teacher process
int i;
for (i = 0; i < MAX_CHAPTERS; i++) {
int chapterPID = fork();
if (chapterPID == 0) {
// GTA process
int j;
for (j = 0; j < MAX_ASSIGNMENTS; j++) {
int assignmentPID = fork();
if (assignmentPID == 0) {
// TA process
int k;
float sum = 0;
for (k = 0; k < MAX_STUDENTS; k++) {
sum += grades[k][i][j];
}
float average = sum / MAX_STUDENTS;
char output[50];
sprintf(output, "Assignment %d - Chapter %d Average = %.6f", j + 1, i + 1, average);
writeOutput("TA process", output);
exit(0);
}
else {
int status;
waitpid(assignmentPID, &status, 0);
}
}
exit(0);
}
else {
int status;
waitpid(chapterPID, &status, 0);
}
}
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started