Presentation on theme: "CSCI 130 Pseudocode. Structure Theorem Any program can be created by using the following 3 control structures: –sequence –selection (IF-THEN-ELSE) –iteration."— Presentation 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: % % % Over %
Selection 2 Solution GET grossIncome; IF grossIncome < THEN netIncome = grossIncome *.1 ELSE IF grossIncome < THEN netIncome = grossIncome *.15 ELSE IF grossIncome < THEN netIncome = grossIncome *.2 ELSE netIncome = grossIncome *.3 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.
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