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

Post on 01-Apr-2015

214 Views

Category:

Documents

1 Downloads

Preview:

Click to see full reader

TRANSCRIPT

CSCI 130

Pseudocode

Structure Theorem

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

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

Key Words

• Receive info: READ, GET

• Display info: DISPLAY, PUT, OUTPUT

• Decision: IF…THEN…ELSE

• Iteration: DOWHILE

Sequence Example

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

Sequence Solution

GET radius

area = pi * radius * radius

OUTPUT area

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

Selection 1 Solution

GET x

IF x < 100

OUTPUT x

END IF

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%

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

Iteration

• Counted DOWHILE– number of iterations is known in advance

• DOWHILE– number of iterations is not known in advance

• sentinel

• end of file

Counted DOWHILE Example

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

Counted DOWHILE solution

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

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.

DOWHILE solution 1

total = 0

READ num

DOWHILE (num not= 999)

total = total + num

READ num

ENDO

OUTPUT total

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.

DOWHILE solution 2

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

Control Structures can be nested

• Decision structures within decision structures

• DOWHILES within DOWHILES

• Decisions structures within DOWHILES

• DOWHILES within Decision structures

• etc.

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.

Solution

answer = ‘YES’

DOWHILE answer = ‘YES’

GET grade

IF grade >= 60

OUTPUT ‘Student Passed’

ELSE

OUTPUT ‘Student Failed’

END IF

GET answer

ENDO

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

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

Module Solution

GET inches ConvertToFeet ConvertToYards

Modules:

ConvertToFeet feet = inches / 12 OUTPUT feet

ConvertToYards yards = inches / 36 OUTPUT yards

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

top related