UTERMINAL
TERMINAL
NEXT
$V1$
ENCORE5
PAUSE
ENCORE1
ENCORE
BOX
SOLUTION
GO
Let us study this example together!
Step 1 is to establish a clear goal.
The primary goal is to: Determine which candidates are
qualified for the position offered!
The secondary goal is to:
Print the name and the telephone number
of the candidates
We'll divide the process into 3 steps:
$PROMPT$
1. Read the client information.
We'll use the function read_client() to do this.
Typical input is shown on the terminal.
John Forsyth 867-8549 4510 X
Anne Blakely 207-4281 4209 XXX
$PROMPT$
2. Compare the candidate information with
the request information. The function
that will do this is compatible().
3. Print the name and the telephone number
of the compatible candidate. This step will
be done inside the main() function.
Potential Candidates
Alex Anderson 279-1436
Judy Boston 362-9867
Prior to solving each of the functions, let's create one data structure
for describing the candidates which we'll call struct candidate!
struct candidate {
We require a variable called id for storing the name and
phone number of the user. It should hold up to 50
characters in length + a space for the NULL character.
How would you declare it?
Answer here:
That is correct!
You've got it on the 2nd try.
Good!, Now you understand the concept!
It is an array of characters!
51 characters all together!
Please review Lesson 6, Topic 1: Declaring Arrays
FORGET1
The correct declaration is char id[51];
char id[51]; /* name and tel. no */
Both the salary and the experience
are of the same type, which is _________.
Answer here:
That is correct!
You've got it on the 2nd try.
Good!, Now you understand the concept!
It is a whole number.
It is either an integer or a short.
Please review Lesson 2, Topic 4: Data Types
FORGET2
The correct answer is short or int!
short salary; /* salary in $Ks */
short experience; /* years of experience */
Finally, the six specialties are represented with
an array of six characters, i.e.
char specialties[6]; /* areas of specialization */
};
The function read_client() has the argument
client of type struct candidate.
Inside the function we need a character buffer c,
and a character counter i.
Both of these should be of type:
Answer here:
That is correct!
You've got it on the 2nd try.
You've got it finally!
It should be a whole number.
It should be an integer.
Please review Lesson 9, Topic 2: Structure Definition and Declaration
FORGET3
The correct choice is int or short.
Therefore, the function assumes the above skeleton:
read_client(client)
struct candidate *client;
{
int i, c;
:
:
:
}
Because the elements of the structure are of different types,
different approaches are required for reading/processing them.
If the identity name character counter i is initially set to 0,
the correct statement for reading a client id of 50 characters is:
a while ((c=client->id[i++] = getchar()) != EOF) &&
(i < 50));
b for (; (c=client->id[i] = getchar()) != EOF &&
i < 50 ; i++);
c do { c = client->id[i] = getchar()); }
while (i < 50 && c != EOF);
d b and c
e a and b
f none of the above
Select one:
That is correct!
You've got it on the 2nd try.
Good, you understand the concept!
Try again, please.
Note that in 3 the counter is not incremented!
Please review Lesson 3, Topic 3: Arithmetic Conversions
FORGET4
The correct answer is e . Both parts a and b are correct. Note
that the counter is not incremented in choice c , which is an error!
To read the client's salary, an integer which occupies 2
fields, which of the following statements is correct?
a scanf("%2d", &client->salary);
b scanf("%2s", client->salary);
c scanf("%2f", &client->salary);
Select one:
That is correct!
You've got it on the 2nd try.
Good, you understand the concept!
Concentrate on the type and the fact that it is a pointer.
Concentrate on the format specifier as well!
Please review Lesson 8, Topic 7: Formatted Text File I/O
FORGET5
The correct answer is a . Choices b and c are wrong because
the conversion specifier is of the wrong type. In b the
argument is not a pointer variable which is also an error!
Which of the following statements will read correctly, the six
possible specialties of the candidate?
a for (i = 0; i < 6; )
client->specialties[i] = getchar();
b while ((client->specialties[i] = getchar() != EOF);
c for (i = 0; i < 6; i++ )
client->specialties[i] = getchar();
Select one:
That is correct!
You've got it on the 2nd try.
Good, you understand the concept!
Concentrate on the type and the fact that it is a pointer.
Concentrate on the format specifier as well!
Please review Lesson 3, Topic 7: The while Statement
FORGET6
The correct answer is c . The first two choices have
either incorrect syntax or the no increment of the counter!
Finally, reading any remaining spaces that were unintentionally
entered at the end of specialties with gets(s), the function
returns 1 if it was successful, 0 otherwise!
char *gets(), s;
And the executable part of the function assumes the above form.
while (((c=client->id[i++] =
getchar()) != EOF) && (i < 50));
if (c != EOF) { /* THEN THERE IS A FULL RECORD */
client->id[50] = '\0';
/* READ CLIENT's SALARY */
scanf("%2d", &client->salary);
/* READ YEARS OF EXPERIENCE */
scanf("%d", &client->experience);
/* READ CLIENT's SPECIALTIES */
for (i = 0; i < 6; i++)
client->specialties[i] = getchar();
gets(s);
/* RETURN O.K. CODE */
return(1);
}
return EOF;
The function compatible() will compare
whether the request information is compatible
with the client information, and will return a
TRUE value if so, and FALSE otherwise.
Therefore, TRUE and FALSE constants need to be defined. This,
together with the arguments, request and client which are
pointers to the type candidate, produce the above code:
#define TRUE 1
#define FALSE 0
compatible(request, client)
struct candidate * request, *client;
{
}
Initially compatibility is assumed to be FALSE, and the specialty
counter i is uninitialized, i.e.
int compat = FALSE, i;
The request matches the client if:
a) the requested salary is not greater by more
than 3K, than the salary offered, and if ..
b) the requested experience is not greater by more than
2 years of the client's experience ...
if ((client->salary - request->salary) <= 3) &&
((request->experience - client->experience) <= 2)) {
:
Now the specialty on the request has to be compared with
the specialty of the client, i.e.
for (i=0; i < 6; i++)
if(request->specialties[i] == 'X' &&
client->specialties[i] == 'X') {
compat = TRUE; break;
}
} return compat;
}
Finally, the composition of the main() is straightforward,
after the functions have been discussed. It is shown above:
main()
{
struct candidate request, person;
printf("\n\nThe Following Candidates Meet");
printf(" The Requirements\n\n");
if ((read_client(&request) != EOF))
while ((read_client(&person) != EOF))
if (compatible(&request, &person))
printf("%s\n", person.id);
}