Download - Review Constructs
-
8/8/2019 Review Constructs
1/25
1
DT211-2
ProgrammingRevision
Algorithms, Pseudocode Top-down
Refinementwith loopingRef Deitel & Deitel
-
8/8/2019 Review Constructs
2/25
2
Pseudocode -Revision
Develop pseudocode algorithms for each of the following:Input two numbers and determine and display which (if either) is
the larger of the two numbers.Input the first number from the keyboard
Input the second number from the keyboard
if the first number is greater than the second numberPrint firstNumber
else
if the second number is greater than the first number
Print secondNumber
ElsePrint a message stating that the numbers are equal
endif
endif
-
8/8/2019 Review Constructs
3/25
3
Pseudocode -Revision
Obtain a series of positive numbers from the keyboard,and determine and display the
sum of the numbers.
Assume that the user types the sentinel value -1 to
indicate end of data entry.Use the while statement.
sum=0
Input a value from the keyboard
While the input value is not equal to -1add the number to sum
input the next number
Print sum
-
8/8/2019 Review Constructs
4/25
4
Pseudocode for Powers of 2
Write pseudocode that keeps printing themultiples (powers) of the integer 2, namely 2, 4,8, 16, 32, 64, and so on. The loop should notshould not terminate (i.e., you should create aninfinite loop). Implement this program andevaluate the outcome of execution.
-
8/8/2019 Review Constructs
5/25
5
Pseudocode for Powers of 2
Allow program to run infinitelyOutput in form
n 2^n
1 22 4
3 8
-
8/8/2019 Review Constructs
6/25
6
Code for Powers of 2
-
8/8/2019 Review Constructs
7/25
7
Top-down Stepwise Refinement
This typically consists of three phases of pseudoceddevelopment.
Top-
a single statement that conveys the programs overall
function. First refinement-
divides the top task into smaller tasks into the general orderthat they must occur.
Second refinement- This identifies storage and variables and their initialisation. It
defines the control structures for managing the processinglogic including the termination and timing of any outputs.
More complex programs will require further refinements.
-
8/8/2019 Review Constructs
8/25
8
Top-down Stepwise RefinementExample
Requirement spec: A class of students took aquiz. The grades (int 0 to 100) are available forinput on an input stream terminated by asentiner of -1. Determine the average grade for
the class.Top Refinement:
Determine the class average for the quiz
Second RefinementInitialize variablesInput, sum up and count the quiz gradesCalculate and print the class average
-
8/8/2019 Review Constructs
9/25
9
Top-down Stepwise Refinement ExampleThird refinement:
Initialize total to zeroInitialize counter to zero
input the first grade (possibly the sentinel)While the user has not as yet entered the sentinel
Add this grade into the running totalAdd one to the grade counterInput the next grade (possibly the sentinel)
endwhile
if the counter is not equal to zeroSet the average to the total divided by the counterPrint the average
elsePrint No grades were entered
endif
-
8/8/2019 Review Constructs
10/25
10
Stepwise RefinementSalesperson earnings
One large chemical company pays itssalespeople on a commission basis. Thesalespeople receive $200 per week plus 9% oftheir gross sales for that week. For example, asalesperson who sells $5000 worth ofchemicals in a week receives $200 plus 9% of$5000, or a total of $650. Develop a program
that will input each salespersons gross salesfor last week and will calculate and display thatsalesperson's earnings. Process onesalesperson's figures at a time.
-
8/8/2019 Review Constructs
11/25
11
Salesperson earningsSample I/O dialogue
Enter sales in dollars (-1 to end): 5000.00Salary is: $650.00
Enter sales in dollars (-1 to end): 1234.56
Salary is: $311.11Enter sales in dollars (-1 to end): 1088.89
Salary is: $298.00
Enter sales in dollars (-1 to end): -1
-
8/8/2019 Review Constructs
12/25
12
Salesperson earningsTop-down refinement
2)Top:For an arbitrary number of salespeople,
determine each salespersons earnings for the
last weekFirst refinement:
Input the salespersons sales for the week,
calculate and print the salespersons wagesfor the week,
then process the next salesperson
-
8/8/2019 Review Constructs
13/25
13
Salesperson earningsTop-down refinement
Second refinement:
input the first salespersons sales in dollars
while the sentinel value (-1) has not been entered for the sales
calculate the salespersons wages for the week
print the salespersons wages for the week
input the next salespersons sales in dollars
endWhile
-
8/8/2019 Review Constructs
14/25
14
Salesperson earnings code#include #include int main( void )
{float sales; /* gross weekly sales */float wage; /* commissioned earnings */
/* get first sales */printf( "Enter sales in dollars (-1 to end): " );
scanf( "%f", &sales );
/* loop until sentinel value read from user */while ( sales >= 0.0 ) {
wage = 200.0 + 0.09 * sales; /* calculate wage *//* display salary */
printf( "Salary is: $%.2f\n\n", wage );/* prompt for next sales */printf( "Enter sales in dollars (-1 to end): " );scanf( "%f", &sales );
} /* end while */getch();
return 0; /* indicate successful termination */
-
8/8/2019 Review Constructs
15/25
15
Counter Controlled Looping withfor statement
The general format of the for statement is for ( expression1; expression2; expression3)
statement
where expression1 initializes the loop-control variable,
expression2 is the loop-continuation condition, andexpression3 increments the control variable.
In most cases, the for statement can be represented with
an equivalent while statement as follows:expression1;
while ( expression2 ) {
statement
expression3;}
-
8/8/2019 Review Constructs
16/25
16
do while statement
In the while statement, the loop-continuationcondition is tested at the beginning of the loop
before the body of the loop is performed.
The dowhile statement tests the loop-continuation
condition afterthe loop body is performed.
Therefore, the loop body will be executed at least
once.
-
8/8/2019 Review Constructs
17/25
17
do while statementUse dowhile statement to print the numbers from 1
to 10.The control variable counter is preincremented in theloop-continuation test.
-
8/8/2019 Review Constructs
18/25
18
for ( expression1; expression2; expression3 ) statement
The three expressions in the for statement are optional.Ifexpression2 is omitted, C assumes that the condition is
true, thus creating an infinite loop.
One may omit expression1 if the control variable is
initialized elsewhere in the program.
expression3 may be omitted if the increment is calculated
by statements in the body of the for statement or if no
increment is needed.
-
8/8/2019 Review Constructs
19/25
19
for ( expression1; expression2; expression3 )
statement
The increment express
ion in thefor s
tatement acts
like as
tand-alone C statement at the end of the body of the for.
The expressions
counter = counter + 1counter += 1
++counter
counter++
are all equivalent in the increment part of the for
statement.
preferred form iscounter++because the incrementing
occurs after the loop body is executed and seems more
natural.
-
8/8/2019 Review Constructs
20/25
20
Notes on forfor ( expression1; expression2; expression3 ) statement
The initialization, loop-continuation condition andincrement can contain arithmetic expressions. For
example, when x = 2 and y = 10, the statementfor ( j = x; j
-
8/8/2019 Review Constructs
21/25
21
for Examplesfor ( expression1; expression2; expression3 ) statement
The following examplesshow methods of varying the controlvariable in a for statement. Vary the control variable from 1 to 100 in increments of1.
for ( i = 1; i = 1; i-- )
Vary the control variable from 7 to 77 in steps of7.for ( i = 7; i = 2; i-= 2 )
Vary the control variable over the following sequence of values:2, 5, 8, 11,14, 17.for ( j = 2; j = 0; j -= 11 )
-
8/8/2019 Review Constructs
22/25
22
For Examples Factorial n, n!
The factorial of a positive integern (written n! andpronounced n factorial) is equal to the product of thepositive integers from 1 to n.
N! = 1x2x3x..(n-1)xn
Develop a program that evaluates the factorials of theintegers from 1 to n and list them in tabular format.
n Factorial of n
1 1
2 23 6
4 24
5 120
-
8/8/2019 Review Constructs
23/25
23
For Examples Factorial n, n!
Top Print a table for factorial of the first n integersFirst refinement:
Input n,
display table column headerfor each number starting at 1 up to n,
calculate its factorial and display number and its
factorialendFor
-
8/8/2019 Review Constructs
24/25
24
For Examples Factorial n, n!
Second refinement:Declare variable n and counter x,
print k, tab, k!
for x=1 to ncalcualte k!
print k, k!
endFor
-
8/8/2019 Review Constructs
25/25
25
For Examples Factorial n, n!
Third refinement:Declare variable n and counter k,
print n, tab, n!
for k=1 to n
/* calculate x!
for j=1 to k
factorialK=factorialK * k
/* factorialK *= factorialKendfor
print k, factorialK!
endFor