Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Problem 5B (50 points + 5 bonus) Implement a program in C that checks the person

ID: 3710930 • Letter: P

Question

Problem 5B (50 points + 5 bonus)
Implement a program in C that checks the personnel file for two types of errors: (1) crew members
other than the captain who have no supervisor, and (2) crew members other than the captain who
have multiple supervisors. Note that your program does not need to check whether the captain
has a supervisor (or multiple supervisors); you may assume that the file is correct in that respect.
For instance, the file shown above has one error of each type: Person 100 has no supervisor, and
Person 300 has two supervisors. On the other hand, note that the duplicate record “200 100”
(lines 3 and 5) is not considered an error, since it shows the same supervisor for Person 200.
Input and output: Your program should input the name of the personnel file, and print (1) the
number of people other than the captain with no supervisor, and (2) the number of people with
multiple supervisors. The file name is a string, the length of which is between 1 and 60.

Here is the text file:

1 0
2 0
200 100
300 100
200 100
300 200
Make sure the program can run in Borland compiler.

Explanation / Answer


#include < stdio.h > #include < stdlib.h > #include < string.h >

void main() {
FILE * filePointer = fopen("input.txt", "r");

char c;
int temp, columns = 0, rows = 0;
int i, j, z, x, y, crewlist[50][50], crew, count;
int num;
do {
c = getc(filePointer);
if ((temp != 2) && (c == ' ' || c == ' '))
columns++;
if (c == ' ') {
temp = 2;
rows++;
}
} while (c != EOF);
rewind(filePointer);
  
for (i = 0; i < rows; i++) {
for (j = 0; j < columns; j++) {
fscanf(filePointer, " %d", & num);
crewlist[i][j] = num;
}
}

for (i = 0; i < rows; i++) {
count = 0;
for (j = 0; j < columns; j++) {
crew = crewlist[i][j];

for (x = 0; x < rows; x++) {
for (y = 0; y < columns; y++) {
if (crew == crewlist[x][y] && x == 0) {
count++;
}

}

}

if (count == 0) printf("No supervisors found for %d ", crew);
else printf("%d supervisors found for %d ", count, crew);
printf(" ");
}

}
fclose(filePointer);
getch();
}