csci 130 pseudocode. structure theorem any program can be created by using the following 3 control...

24
CSCI 130 Pseudocode

Upload: fabiola-limbrick

Post on 01-Apr-2015

214 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: CSCI 130 Pseudocode. Structure Theorem Any program can be created by using the following 3 control structures: –sequence –selection (IF-THEN-ELSE) –iteration

CSCI 130

Pseudocode

Page 2: CSCI 130 Pseudocode. Structure Theorem Any program can be created by using the following 3 control structures: –sequence –selection (IF-THEN-ELSE) –iteration

Structure Theorem

• Any program can be created by using the following 3 control structures:– sequence– selection (IF-THEN-ELSE)– iteration (DOWHILE)

Page 3: CSCI 130 Pseudocode. Structure Theorem Any program can be created by using the following 3 control structures: –sequence –selection (IF-THEN-ELSE) –iteration

What is Pseudocode?

• English like statements depicting the flow of logic in a computer program– 1. Simple english– 2. One instruction per line– 3. Keywords and Indentation - clarity– 4. Top to bottom - one entry, one exit– 5. Modules

Page 4: CSCI 130 Pseudocode. Structure Theorem Any program can be created by using the following 3 control structures: –sequence –selection (IF-THEN-ELSE) –iteration

Key Words

• Receive info: READ, GET

• Display info: DISPLAY, PUT, OUTPUT

• Decision: IF…THEN…ELSE

• Iteration: DOWHILE

Page 5: CSCI 130 Pseudocode. Structure Theorem Any program can be created by using the following 3 control structures: –sequence –selection (IF-THEN-ELSE) –iteration

Sequence Example

• Write pseudocode for a program which will input the radius of a circle, and which will output the area

Page 6: CSCI 130 Pseudocode. Structure Theorem Any program can be created by using the following 3 control structures: –sequence –selection (IF-THEN-ELSE) –iteration

Sequence Solution

GET radius

area = pi * radius * radius

OUTPUT area

Page 7: CSCI 130 Pseudocode. Structure Theorem Any program can be created by using the following 3 control structures: –sequence –selection (IF-THEN-ELSE) –iteration

Selection 1 Example

• Write the pseudocode for a program that will accept a number that is less than 100, and will output the number only if it meets that condition

Page 8: CSCI 130 Pseudocode. Structure Theorem Any program can be created by using the following 3 control structures: –sequence –selection (IF-THEN-ELSE) –iteration

Selection 1 Solution

GET x

IF x < 100

OUTPUT x

END IF

Page 9: CSCI 130 Pseudocode. Structure Theorem Any program can be created by using the following 3 control structures: –sequence –selection (IF-THEN-ELSE) –iteration

Selection 2 Example

• Write the pseudocode for a program that will determine the net income given the gross income of an employee. Determine the tax according to the following table: 0 - 10000 10% 10001 - 20000 15% 20001 - 50000 20% Over 50000 30%

Page 10: CSCI 130 Pseudocode. Structure Theorem Any program can be created by using the following 3 control structures: –sequence –selection (IF-THEN-ELSE) –iteration

Selection 2 Solution

GET grossIncome; IF grossIncome < 10001 THEN netIncome = grossIncome * .1 ELSE IF grossIncome < 20001 THEN netIncome = grossIncome * .15 ELSE IF grossIncome < 50001 THEN netIncome = grossIncome * .2 ELSE netIncome = grossIncome * .3 END IF END IF END IF DISPLAY netIncome

Page 11: CSCI 130 Pseudocode. Structure Theorem Any program can be created by using the following 3 control structures: –sequence –selection (IF-THEN-ELSE) –iteration

Iteration

• Counted DOWHILE– number of iterations is known in advance

• DOWHILE– number of iterations is not known in advance

• sentinel

• end of file

Page 12: CSCI 130 Pseudocode. Structure Theorem Any program can be created by using the following 3 control structures: –sequence –selection (IF-THEN-ELSE) –iteration

Counted DOWHILE Example

• Write the pseudocode for a program which will read in 10 numbers and output the sum and average

Page 13: CSCI 130 Pseudocode. Structure Theorem Any program can be created by using the following 3 control structures: –sequence –selection (IF-THEN-ELSE) –iteration

Counted DOWHILE solution

totalSum = 0 average = 0 DOWHILE counter = 1 to 10 read num totalSum = totalSum + num ENDO average = totalSum / 10 OUTPUT totalSum, average

Page 14: CSCI 130 Pseudocode. Structure Theorem Any program can be created by using the following 3 control structures: –sequence –selection (IF-THEN-ELSE) –iteration

DOWHILE example 1

• Write the pseudocode for a program that will allow the user to input numbers. The signal (sentinel) from the user that they are done is that they will enter 999. The program will then output the sum of the numbers.

Page 15: CSCI 130 Pseudocode. Structure Theorem Any program can be created by using the following 3 control structures: –sequence –selection (IF-THEN-ELSE) –iteration

DOWHILE solution 1

total = 0

READ num

DOWHILE (num not= 999)

total = total + num

READ num

ENDO

OUTPUT total

Page 16: CSCI 130 Pseudocode. Structure Theorem Any program can be created by using the following 3 control structures: –sequence –selection (IF-THEN-ELSE) –iteration

DOWHILE example 2

• Write the pseudocode for a program that will read in records from a file. The program will extract the first name, last name, and salary from the file. It will then output the first name, last name, and salary of each employee. At the end of the program, the total payroll is output.

Page 17: CSCI 130 Pseudocode. Structure Theorem Any program can be created by using the following 3 control structures: –sequence –selection (IF-THEN-ELSE) –iteration

DOWHILE solution 2

totalPayroll = 0 DOWHILE records exist READ inputRecord EXTRACT firstName, lastName, salary WRITE firstName, lastName, salary totalPayroll = totalPayroll + salary ENDO WRITE totalPayroll

Page 18: CSCI 130 Pseudocode. Structure Theorem Any program can be created by using the following 3 control structures: –sequence –selection (IF-THEN-ELSE) –iteration

Control Structures can be nested

• Decision structures within decision structures

• DOWHILES within DOWHILES

• Decisions structures within DOWHILES

• DOWHILES within Decision structures

• etc.

Page 19: CSCI 130 Pseudocode. Structure Theorem Any program can be created by using the following 3 control structures: –sequence –selection (IF-THEN-ELSE) –iteration

Putting it all together

• Write the pseudocode for a program which will accept a number grade as input, and which will output a message stating whether the student passed or failed. In addition, after each grade is done, ask the user if they want to continue. If they answer ‘YES’, request another grade, otherwise end the program.

Page 20: CSCI 130 Pseudocode. Structure Theorem Any program can be created by using the following 3 control structures: –sequence –selection (IF-THEN-ELSE) –iteration

Solution

answer = ‘YES’

DOWHILE answer = ‘YES’

GET grade

IF grade >= 60

OUTPUT ‘Student Passed’

ELSE

OUTPUT ‘Student Failed’

END IF

GET answer

ENDO

Page 21: CSCI 130 Pseudocode. Structure Theorem Any program can be created by using the following 3 control structures: –sequence –selection (IF-THEN-ELSE) –iteration

Modules

• Subtasks

• Increases Readability

• Easier Development/Maintenance

• Program may have Modules (functions, paragraphs) but pseudocode does not necessarily show this (complex functions will be broken out in pseudocode)

• Module call matches module name EXACTLY

Page 22: CSCI 130 Pseudocode. Structure Theorem Any program can be created by using the following 3 control structures: –sequence –selection (IF-THEN-ELSE) –iteration

Module Example

• Write the pseudocode for a program that will accept a number in inches, and which will output that number converted to feet and yards

Page 23: CSCI 130 Pseudocode. Structure Theorem Any program can be created by using the following 3 control structures: –sequence –selection (IF-THEN-ELSE) –iteration

Module Solution

GET inches ConvertToFeet ConvertToYards

Modules:

ConvertToFeet feet = inches / 12 OUTPUT feet

ConvertToYards yards = inches / 36 OUTPUT yards

Page 24: CSCI 130 Pseudocode. Structure Theorem Any program can be created by using the following 3 control structures: –sequence –selection (IF-THEN-ELSE) –iteration

Module Solution 2

GET inches feet = ConvertToFeet(inches) yards = ConvertToYards(inches) OUTPUT feet OUTPUT yards

Modules:

ConvertToFeet(inputInches) feet = inputInches / 12

ConvertToYards(inputInches) yards = inputInches / 36