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

Problem 5A (50 points) PROGRAMMING IN C Data format We refer to crew members by

ID: 3711370 • Letter: P

Question

Problem 5A (50 points) PROGRAMMING IN C

Data format

We refer to crew members by their ID numbers, which are integers between 0 and 999. Every person has a unique ID; in particular, the ID of Captain Picard is 0. Some values between 0 and 999 may be unused, which means that the total number of crew members may be less than 1,000. The personnel database is a text file, which contains between 1 and 1,000 lines. Each line includes two distinct IDs, separated by a single space. The second ID specifies the supervisor of the person with the first ID. For example, the line “200 100” indicates that Person 100 is the supervisor of Person 200.

Example file:

1 0

2 0

200 100

300 100

200 100

300 200

Problem 5A (50 points) PROGRAMMING IN C

Implement a program that counts the number of crew members, that is,the number of distinct IDs in the personnel file. For instance, the file shown above includes six distinct IDs: 0, 1, 2,

100, 200, and 300.

Input and output:

Your program should input the name ofthe personnel file, and print the number of distinct IDs in the file. The file name is a string, the length of which is between 1 and 60.

Example:

OCommand Prompt C:homework5a Enter name of the input file: personnel.txt The crew includes 6 members

Explanation / Answer

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

int main()
{
printf("Enter name of the input file: ");
char filename[15];
scanf("%s", filename);
  
FILE *fp;
fp = fopen(filename, "r");
if (fp == NULL)
{
printf("Cannot open file ");
exit(0);
}
  
int arr[1000];
int i;
for (i = 0; i < 1000; i++) {
arr[i] = 0;
}
  
while (1) {
int id1, id2;
int ret = fscanf(fp, "%d %d", &id1, &id2);
if(ret == 2) {
arr[id1] = 1;
arr[id2] = 1;
}
else if(ret == EOF) {
break;
} else {
printf("No match. ");
}
}
int count = 0;
for(i = 0; i < 1000; i++) {
if (arr[i] != 0) {
count++;
}
}
printf(" The crew includes %d members ", count);
  
return 0;
}

Sample run

Enter name of the input file: personnel.txt

The crew includes 6 members