PAGETURNER
SOMECLEAN
ENCORE2
ENCORE1
CLEAN
ENCORE
BOX
SOLUTION
GO
Let's look at this example together.
Step 1 is to establish a clear goal.
The goal is to determine the number of characters
and the number of lines in the standard input.
We will use two facilities of the macro preprocessor
which will be discussed in detail in Lesson 5:
#define, for defining constants and
#include, for including a file.
To calculate the number of characters and the number of
lines in the current line, we create the three variables:
nc, nl and i all of which should be which data type?
Answer here:
That is correct!
You've got it on the 2nd try.
You've got it finally!
Which type represents whole numbers?
Please try again.
Please review Topic 1: Types of Control Constructs
FORGET1
The correct choice is int .
Using this and the macro preprocessor information,
the skeleton for the solution takes above form:
#define LIMIT 80 /* line length */
#include <stdio.h>
main()
{
int c; /* character buffer */
int nc, nl, i /* counters */
:
:
:
}
A character buffer is defined as int c
and the function getchar() reads the next
character from the standard input. The
line is delimited by '\n', EOF or a maximum
number of characters which we will call LIMIT.
The statement which will read a line of input
based on these stipulations is:
a for (i=0;(c= getchar() != EOF || c != '\n'
&& c == LIMIT); i++)
b i = 0;
while (( c = getchar()) != '\n' && c != EOF && i++ < LIMIT)
c do { c = getchar(); i++;
} while ( c != '\n' && c != EOF || i < LIMIT );
Select one:
That is correct!
You've got it on the 2nd try.
You've got it finally!
Think about the boundary conditions.
There are only AND's; no OR's.
Please study the logic in the above statements.
FORGET2
The correct choice is b ! Choice a has poor logic,
choice c is very close, however, the || should be changed to && .
The character counter nc should be
incremented for each character read,
except for the character ___?
Answer here:
That is correct!
You've got it on the 2nd try.
You've got it finally!
Sorry, try again.
The name is 3 characters long.
The End of File character is not counted in the file size.
FORGET3
The correct answer is EOF .
If do { ... } while ( ... ); is the main
control loop for the process, the condition
INSIDE the while ( ... ) stating that c
does not equal EOF is written as:
Answer here:
That is correct!
You've got it on the 2nd try.
You've got it finally!
Sorry, try again.
No, the answer is an expression: c != ___
Please think about EOF.
FORGET4
The correct choice is c != EOF .
When we piece together all the parts we discussed so far,
the main control sequence becomes as listed above:
do { i = 0;
while (( c = getchar()) != '\n' &&
c != EOF && ++i < LIMIT)
nc += 1; /* increment char. counter */
if ( c != EOF)
nc += 1; /* new line also a character */
nl++; /* increment the line counter */
} while ( c != EOF); /* as long as not EOF */