Anda di halaman 1dari 2

Transcript of illustrative problems

Example A The initial value of the radius of a circle is equal to one unit and
each succeeding radius is one unit greater than the value before it. Draw a
flowchart to compute the area of the circle starting with R=1.0 up to R=5.0,
then print out each radius and the corresponding area of the circle. The
algorithm for satisfying the requirements of the stated problem could be as
follows:
Step 1: Initialize the value of R to 1 and the value of PI to 3.1416.
Step 2: Computer area by multiplying PI by the square of R.
Step 3: Print out the value of R and the computed area.
Step 4: Increment the value of R by 1.
Step 5: Test if R is less than or equal to 5.
Step 6: If R is less than or equal to 5, loop back and repeat step 2 through 5.
However, if R is greater than 5, stop processing.
Example B Given a set of 5 numbers that include both positive and negative
data values. Draw a flowchart to read in these values one at a time and
count the number of positive values (including zero values) and the number
of negative values found in the set. After the required values are determined,
print out these counted values. Example C: Draw a flowchart which will read
and print the names and individual scores of 100 students for a particular
examination. Also, determine their average score, and print it out. The
algorithm of such a problem may be summarized as follows:
Step 1: Set up two counters and initialize their values to zero (one counter,
NNEG, is used for counting the number of negative values and the other,
NPOS, for counting positive values).
Step 2: Input the first data value, NO.
Step 3: Test and determine if the value read is positive or negative ( The
decision symbol is used and in it is written the question, NO<0?).
Step 4: If the value of NO is negative (or less than zero), increment the NNEG
counter by one. However, if it is positive (or equal to or greater than zero),
increment the NPOS counter.
Step 5: Determine the number of values encountered (NVAL) by adding the
two counters (NNEG and NPOS).
Step 6: Test is NVAL is equal to 5.
Step 7: If the value of NVAL is not equal to 5, repeat steps 2 through 5.
However, if it is equal to 5, print out the current value stored in these

counters (NNEG and NPOS). The algorithm for this particular requirements
could be listed as follows;
Step 1: Initialize the counter (CTR) and accumulator (SUM) to zero
Step 2: Read in the student's name and score.
Step 3: Print out the student's name and score.
Step 4: Accumulate the data value (SCORE) into the sum.
Step 5: Test if the desired amount of data (100) has been reached.
Step 6: If the value of counter (CTR) is less than 100, repeat step 2, through
5. However, if the desired amount of data has been reached (100), compute
the average score by dividing the sum by the value of counter.
Step 7: Print out the average score. STEPS IN LOOP CONTROL 1.
INITIALIZATION - the value of counter used is initially set equal to zero (or
one). This process is always done outside the loop.
2. TEST FOR LIMIT CONDITIONS - before logic flow gets out of a loop, a loopterminating condition must first be satisfied. The process of testing is usually
found either at the beginning or at the end of a loop.
3. INCREMENTATION - after each loop executed, 1 is added to the counter,
Thus the counter reflects the number of times the operation has been
performed. Note that the process of incrementation as always done within
the loop.

Anda mungkin juga menyukai