CSCI6370: Topics in Computer Science Advanced Topics in Algorithms and Applications Fall Semester, 2002 - PowerPoint PPT Presentation

1 / 21
About This Presentation
Title:

CSCI6370: Topics in Computer Science Advanced Topics in Algorithms and Applications Fall Semester, 2002

Description:

Input the ten quiz grades and count passes and failures. Print a summary of the exam results and ... Refine Print a summary of the exam results and decide if ... – PowerPoint PPT presentation

Number of Views:39
Avg rating:3.0/5.0
Slides: 22
Provided by: XIAODO
Category:

less

Transcript and Presenter's Notes

Title: CSCI6370: Topics in Computer Science Advanced Topics in Algorithms and Applications Fall Semester, 2002


1
Lecture 5 Layers of Control
2
Nested while Loops
  • Problem
  • Multiplying two numbers and outputting
  • the result only if they are both less
  • than 5. (i.e. Start at i1, j1 and go
  • from there)
  • Formulate pseudocode algorithm
  • i 1
  • j 1 1 j
  • j 2 1 j
  • j 3 1 j
  • j 4 1 j
  • i 2
  • j 1 2 j
  • j 2 2 j
  • j 3 2 j
  • j 4 2 j

j 1 While j is less than 5 print out the
result of 1 j j j 1
j 1 While j is less than 5 print out the
result of 2 j j j 1
3
Nested while Loops
  • Formulate pseudocode algorithm
  • i 3
  • j 1 3 j
  • j 2 3 j
  • j 3 3 j
  • j 4 3 j
  • i 4
  • j 1 4 j
  • j 2 4 j
  • j 3 4 j
  • j 4 4 j

j 1 While j is less than 5 print out the
result of 3 j j j 1
j 1 While j is less than 5 print out the
result of 4 j j j 1
4
Formulate Pseudocode Algorithm
  • i 1
  • i 2
  • i 3
  • i 4

j 1 While j is less than 5 print out the
result of 1 j j j 1
i
j 1 While j is less than 5 print out the
result of 2 j j j 1
i 1 While i is less than 5 i i
1
i
j 1 While j is less than 5 print out the
result of 1 j j j 1
i
j 1 While j is less than 5 print out the
result of 3 j j j 1
i
j 1 While j is less than 5 print out the
result of 4 j j j 1
i
5
Pseudocode
i 1 While i is less than 5 i i
1
j 1 While j is less than 5 print out the
result of i j j j 1
6
C Code
7
Nested Control Structures
  • Problem
  • A college has a list of test results (1 pass,
    2 fail) for 10 students. Write a program that
    analyzes the results as follows
  • Input each test result (i.e., a 1 or a 2).
    Display the prompting message Enter result each
    time the program requests another test result.
  • Count the number of test results of each type
  • Display a summary of the test results indicating
    the number of students who passed and the number
    who failed
  • If more than 8 students passed the test, print
    the message Raise tuition.

8
Before Formulate the Algorithm
  • The program must process 10 test results
  • Counter-controlled loop will be used
  • Two counter can be used
  • One for number of passes, one for number of fails
  • Each test result is a number -- either a 1 or a 2
  • If the number is not a 1, we assume that it is a
    2
  • Decide if more than 8 students passed the test.

9
Formulate the Pseudocode Algorithm
  • Top level outline
  • Analyze exam results and decide if tuition should
    be raised
  • First refinement
  • Initialize variables
  • Input the ten quiz grades and count passes and
    failures
  • Print a summary of the exam results and
  • decide if tuition should be raised
  • Refine Initialize variables to
  • Initialize passes to zero
  • Initialize failures to zero
  • Initialize student counter to one

10
Formulate the Pseudocode Algorithm
  • Refine Input the ten quiz grades and count passes
    and failures to
  • While student counter is less than or equal to
    ten Input the next exam result
  • If the student passed
  • Add one to passes
  • else Add one to failures
  • Add one to student counter
  • Refine Print a summary of the exam results and
    decide if tuition should be raised to
  • Print the number of passes
  • Print the number of failures
  • If more than eight students passed Print Raise
    tuition

11
Pseudocode for Test Results Analysis
12
C code for Test Results Analysis
13
C code for Test Results Analysis
14
Assignment Operators
  • Assignment operators abbreviate assignment
    expressions
  • c c 3
  • can be abbreviated as c 3 using the addition
    assignment operator
  • Statements of the form
  • variable variable operator expression
  • can be rewritten as
  • variable operator expression
  • Examples of other assignment operators
  • d - 4 (d d - 4)
  • e 5 (e e 5)
  • f / 3 (f f / 3)
  • g 9 (g g 9)

15
Arithmetic Assignment Operators
16
Increment and Decrement Operators
  • Increment operator ()
  • Can be used instead of c 1
  • Decrement operator (--)
  • Can be used instead of c - 1
  • Pre-increment/Pre-decrement
  • Operator is used before the variable (c or --c)
  • Variable is changed before the expression it is
    in is evaluated
  • Post-increment/Post-decrement
  • Operator is used after the variable (c or c--)
  • Expression executes before the variable is changed

17
Increment and Decrement Operators
  • If c equals 5, then
  • printf( "d", c )
  • Prints 6
  • printf( "d", c )
  • Prints 5
  • In either case, c now has the value of 6
  • When variable not in an expression
  • Preincrementing and postincrementing have the
    same effect
  • c
  • printf( d, c )
  • Has the same effect as
  • c
  • printf( d, c )

18
Increment and Decrement Operators
19
Increment and Decrement Operators
20
Precedence of the Operators
21
In-Class Programming Exercise
Create a program that prints out a triangle with
a user specified width. Example output Enter the
specified width5 X XX XXX XXXX XXXXX XXXX XXX XX
X
Write a Comment
User Comments (0)
About PowerShow.com