Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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:

1 CSCI 130 Pseudocode

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

3 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

4 Key Words Receive info: READ, GET Display info: DISPLAY, PUT, OUTPUT Decision: IF…THEN…ELSE Iteration: DOWHILE

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

6 Sequence Solution GET radius area = pi * radius * radius OUTPUT area

7 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

8 Selection 1 Solution GET x IF x < 100 OUTPUT x END IF

9 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 %

10 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

11 Iteration Counted DOWHILE –number of iterations is known in advance DOWHILE –number of iterations is not known in advance sentinel end of file

12 Counted DOWHILE Example Write the pseudocode for a program which will read in 10 numbers and output the sum and average

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

14 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.

15 DOWHILE solution 1 total = 0 READ num DOWHILE (num not= 999) total = total + num READ num ENDO OUTPUT total

16 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.

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

18 Control Structures can be nested Decision structures within decision structures DOWHILES within DOWHILES Decisions structures within DOWHILES DOWHILES within Decision structures etc.

19 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.

20 Solution answer = ‘YES’ DOWHILE answer = ‘YES’ GET grade IF grade >= 60 OUTPUT ‘Student Passed’ ELSE OUTPUT ‘Student Failed’ END IF GET answer ENDO

21 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

22 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

23 Module Solution GET inches ConvertToFeet ConvertToYards Modules: ConvertToFeet feet = inches / 12 OUTPUT feet ConvertToYards yards = inches / 36 OUTPUT yards

24 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


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

Similar presentations


Ads by Google