|
CIS162AD -- C Sharp (C#) -- Section 5832
Professor: Dave Hammer dwhammer@cox.net http://members.cox.net/dwhammer/ Student: Patrick Moss patmoss@patmoss.com http://www.patmoss.com/cis162ad/ Worksheet #4 Due Thursday, March 2, 2006
I. Definitions
1. iteration structure (p. 801)
A looping strucure in C#. Iteration enables you
to identify and block together one or more source
statements to be repeated based on a predetermined
condition. Also called repetition.
2. infinite loop (p. 800)
A loop that has no provisions for termination, or a
loop for which the exit test condition never becomes
true.
Placing a semicolon at the end of the conditional
expression can create an (unintentional) infinite
loop situation.
Another possible problem: Comparing fractional
floating point values for equality may fail. For
example, if we increment a fractional value, such
as 0.1 for 1000 times, the sum may not equal 100.0
(to 16 decimal places), so a double-precision loop
exit test such as while (x != 100.0) etc. may never
come true. For this case, it is safer to use a
less than (x < 100.0) or greater than (x > 100.0)
bounds test for the floating point value.
3. MessageBox (p. 261-264)
C# has a predefined class called MessageBox that can
be used to display information to users through its
Show() method member.
As we saw in class, there are some 12 different
combinations of information and user choice icons.
4. nested loop (p. 281-286)
Any statement can be included within the body of a loop,
which means that an inner loop can be nested inside an
outer loop. When this occurs, the inner nested loop is
totally completed before the outside loop is tested for
a second time. We often need nested loops when working
with arrays, especially multidimensional arrays.
5. continue statement (p. 287-288)
The continue statement, like the break, is a form of
jump statement. When a continue is reached, a new
iteration of the nearest enclosing while, do...while,
for, or foreach statement is started. When continue
is reached and executed, the rest of that iteration
of the loop body is not performed. Continue immediately
transfers control straight to the conditional expression
for an evaluation.
6. off-by-one error (p. 803)
A common programmer error in which a loop body is
performed one too many or one too few times.
7. scope (p. 804)
Scope refers to the region in the program in which
a variable exists. For example, if a variable is
declared in a method, it can be used in that method
only. It is out of scope in other methods. Considering
the classic "stack/heap" memory implementation structure,
this is a "stack" variable. It is pushed onto the stack
as a local variable when the method executes. It is popped
off the stack when method execution exits. Also consider
two related Type 3 grammar concepts: visibility and longevity.
8. garbage collection (p. 274)
If a variable is declared inside a block, it dies or becomes
no longer available outside of the enclosing block. When the
variable is a local stack variable, then memory release/reuse
is automatic. However, memory for objects instantiated on
the heap, via a "new" command, is not managed automatically.
Each object is assigned a "reference count". When the object's
reference count goes down to zero, the object can no longer
be used, and its memory can be released. C# performs what is
called "automatic garbage collection" to identify these
unused objects, and to release their heap memory for reuse
by the executing program.
II. Questions or short essay
1. How is a pre-test loop different from a post-test loop?
An iteration (i.e., loop) structure must include the three
basic elements: initialize, test, and increment/decrement.
The test statement can be placed either at the top of the
loop, or at the bottom of the loop.
pre-test loop: The test is placed at the top of the loop.
post-test loop: The test is placed at the bottom of the loop.
Note that a post-test loop will always be executed at least
once.
Some software algorithms can be implemented most easily with
the pre-test loop structure, while other algorithms can be
implemented most easily with the post-test loop structure.
2. How is a sentinel-controlled loop different from a counter-
controlled loop?
When testing for loop completion, there are two possibilities:
a. Test a boolean sentinel control variable (or raise a boolean
sentinel control exit event), or
b. Test a counter data variable for an exit value.
We usually test the sentinel control variable for equality
(such as true or false).
We can test a counter variable for less than, equal to, or
greater than a constant value.
Re: sentinel value
When iterating through a list of values, such as in a table,
we can store a "control value" at the end of the list. Then
when we encounter the control value, we test it as equal to
a constant value, the test is true, and we exit from the loop.
Note that the "control value" often has the same data type
as the data items. Therefore, it must be assigned a unique
value that will not be used by the items of the data set.
For example, if the data values must be positive, then the
"control value" could be zero or a negative value, etc.
For the sentinel test, we can either test the "control value"
directly, or we can use an "if" test of the "control value"
within the loop to set a boolean variable, and then test the
boolean variable in order to control exit from the loop.
Re: Control counter
When iterating through a list of data values, for which we
know the number of items in the list, we can define and use
a control counter. For example, if there are 100 data values
in the list, we can initialize a control counter to 0, and
then increment it by one as we access each successive item
in the list. Then we can test the control counter vs 100.
When the control counter reaches 100, we have processed
the final data value item, and we exit from the loop.
Note that in both cases, we are "mixing apples and oranges",
i.e., we are mixing data and control values together. A much
more sophisticated approach: When accessing sequential values,
such as in an input file or an array, we can allocate a separate
boolean dimension (i.e., control column) to contain a boolean
"data type" of each entry. Then we can test this data type for
each entry to determine if the entry is a data value, or is a
sentinel control value. This is the engineering approach used
for high quality input peripherals, such as an input magnetic
tape drive, where separate data lines and control lines are
provided.
3. Explain the syntax of the for loop. (p. 270-277)
The for loop structure, like any iteration loop structure,
includes the three essential elements: initialize, increment,
and test.
The for loop syntax has the form:
for(initialize; test; update)
{
statement(s);
}
For example, the for loop structure:
for (int i = 1; i < 11; i++)
{ statement(s):
}
will iteratate over successive incremental integer values from
1 through 10.
To implement an "infinite loop" structure, the test condition
can be omitted, such as:
for (int j = 0; ; j++) { }
Note: The classic form of a "for" infinite loop is:
for ( ; ; ) { }
For this case, we include one or more "break" statements within
the loop, in order to break out of the loop, based on conditions
tested for within the body of the loop.
And, multiple values can be initialized in the initialize portion
of the for loop syntax (comma-separated), such as:
for (int j=0, i=0; j<10; j+=3) { }
And, a compound test can be included in the test portion of the
for loop syntax, such as:
for (int j=0, k=10; j<10 && k>0; j+=3) { }
And, multiple statements can be included in the update portion
of the for loop syntax (comma-separated), such as:
for (int j=0; j<10; counter++, j+=3) { }
4. What is the purpose of the foreach statement (p. 277-278)
In classic "set theory", there is no restiction of order
of the items in the set. The set concept is implemented
in C# as a "collection". We can iterate through items of
a collection by using a "foreach" statement. For example,
when applied to a one-dimensional array:
int [] myTable = {2, 4, 6, 8, 10}
foreach (int val in myTable)
{
Console.WriteLine(val);
}
Note that we cannot change values in a collection. The access
to the elements is read-only.
5. Why use a loop?
To iterate through a group of symmetrical items in order to:
a. Process all of the records in an input file.
b. Process all of the values in a table, such as to find the
average, smallest value, largest value, etc. in the table.
c. Search through a table -- to locate a certain value in a
data field.
6. In what situation might a for loop work best?
a. When the number of iterations is known in advance.
b. When pre-testing is appropriate to implement an algorithm.
c. When uniformly spaced integer step values can be used.
III. Programming Exercise
Write a program to calculate the average of all scores entered
between 0 and 100. Use a sentinel-controlled loop variable to
terminate the loop. After values are entered and the average
calculated, test the average to determine whether an A, B, C,
D, or F should be recorded. The scoring rubric is as follows:
A: 90 to 100, B: 80 to 89, C: 70 to 79, D: 60 to 69, F: less
than 60. Turn in your source code for this program.
|