slug
type
status
category
summary
date
tags
password
icon
Here are 40 executable pseudocode examples based on the content of the "Pseudocode Guide for Teachers" document:
1. Variable Declaration and Assignment
2. Input and Output
3. Arithmetic Operations
4. If-Else Statement
5. Case Statement
6. While Loop
7. Repeat-Until Loop
8. For Loop
9. Array Declaration and Access
10. Procedure Definition and Call
11. Function Definition and Use
12. File Handling - Text File
13. File Handling - Write to File
14. String Manipulation
15. Nested If Statement
16. Nested Loops
17. Dynamic Array Access
18. Random Number Generation
19. Record Data Type
20. Basic Object-Oriented Example
21. Simple Inheritance
22. Boolean Operations
23. File Copy
24. Arithmetic Modulus
25. Array with Loop
26. BREAKPOINT
27. String Concatenation
28. Numeric Casting
29. Pass by Reference Procedure
30. Passing Parameters by Value
31. Bubble Sort Algorithm
32. Factorial Calculation
33. Linear Search
34. Prime Number Check
35. Fibonacci Sequence
36. Merge Two Arrays
37. String Reverse
38. Calculate Average
39. Sum of Digits
40. Find Largest Number in Array
All in one:
click the left triangle
// 1. Variable Declaration and Assignment
DECLARE x : INTEGER
DECLARE y : REAL
x ← 10
y ← 5.5
OUTPUT "x=" & x
OUTPUT "y=" & y
// 2. Input and Output
DECLARE name : STRING
INPUT name
OUTPUT "Hello, ", name
// 3. Arithmetic Operations
DECLARE a : INTEGER
DECLARE b : INTEGER
DECLARE sum : INTEGER
a ← 5
b ← 10
sum ← a + b
OUTPUT "The sum is ", sum
// 4. If-Else Statement
DECLARE score : INTEGER
INPUT score
IF score >= 50 THEN
OUTPUT "Pass"
ELSE
OUTPUT "Fail"
ENDIF
// 5. Case Statement
DECLARE grade : CHAR
INPUT grade
CASE OF grade
'A' : OUTPUT "Excellent"
'B' : OUTPUT "Good"
'C' : OUTPUT "Average"
OTHERWISE : OUTPUT "Invalid grade"
ENDCASE
// 6. While Loop
DECLARE counter : INTEGER
counter ← 1
WHILE counter <= 5
OUTPUT counter
counter ← counter + 1
ENDWHILE
// 7. Repeat-Until Loop
DECLARE password : STRING
REPEAT
OUTPUT "Enter password:"
INPUT password
UNTIL password = "1234"
// 8. For Loop
DECLARE i : INTEGER
FOR i ← 1 TO 10
OUTPUT i
NEXT i
// 9. Array Declaration and Access
DECLARE numbers : ARRAY[1:5] OF INTEGER
numbers[1] ← 10
numbers[2] ← 20
OUTPUT numbers[1]
OUTPUT numbers[2]
// 10. Procedure Definition and Call
PROCEDURE Greet()
OUTPUT "Hello, World!"
ENDPROCEDURE
CALL Greet()
// 11. Function Definition and Use
FUNCTION Square(x : INTEGER) RETURNS INTEGER
RETURN x * x
ENDFUNCTION
OUTPUT Square(5)
// 12. File Handling - Text File
DECLARE line : STRING
OPENFILE "data.txt" FOR READ
WHILE NOT EOF("data.txt")
READFILE "data.txt", line
OUTPUT line
ENDWHILE
CLOSEFILE "data.txt"
// 13. File Handling - Write to File
DECLARE data : STRING
data ← "Sample Text"
OPENFILE "output.txt" FOR WRITE
WRITEFILE "output.txt", data
CLOSEFILE "output.txt"
// 14. String Manipulation
DECLARE str : STRING
str ← "Hello"
OUTPUT LENGTH(str)
// 15. Nested If Statement
DECLARE age : INTEGER
INPUT age
IF age >= 18 THEN
OUTPUT "Adult"
IF age >= 65 THEN
OUTPUT "Senior"
ENDIF
ELSE
OUTPUT "Minor"
ENDIF
// 16. Nested Loops
DECLARE i : INTEGER
DECLARE j : INTEGER
FOR i ← 1 TO 3
FOR j ← 1 TO 2
OUTPUT "i:", i, " j:", j
NEXT j
NEXT i
// 17. Dynamic Array Access
DECLARE nums : ARRAY[1:5] OF INTEGER
DECLARE i : INTEGER
FOR i ← 1 TO 5
nums[i] ← i * 10
NEXT i
FOR i ← 1 TO 5
OUTPUT nums[i]
NEXT i
// 18. Random Number Generation
DECLARE randomValue : REAL
randomValue ← RAND(10)
OUTPUT "Random value: ", randomValue
// 19. Record Data Type
TYPE Student
DECLARE name : STRING
DECLARE age : INTEGER
ENDTYPE
DECLARE student1 : Student
student1.name ← "John"
student1.age ← 20
OUTPUT student1.name
OUTPUT student1.age
// 20. Basic Object-Oriented Example
CLASS Animal
PRIVATE name : STRING
PUBLIC PROCEDURE NEW(animalName : STRING)
name ← animalName
OUTPUT name
ENDPROCEDURE
ENDCLASS
myAnimal1 ← NEW Animal("Lion")
myAnimal2 ← NEW Animal("Tiger")
// 21. Simple Inheritance
CLASS Dog INHERITS Animal
PRIVATE breed : STRING
PUBLIC PROCEDURE NEW(animalName : STRING, breedName : STRING)
SUPER.NEW(animalName)
breed ← breedName
OUTPUT breed
ENDPROCEDURE
PUBLIC FUNCTION GetBreed() : STRING
RETURN breed
ENDFUNCTION
ENDCLASS
myDog ← NEW Dog("TaiDi", "泰迪")
OUTPUT myDog.GetName()
// 22. Boolean Operations
DECLARE a : BOOLEAN
DECLARE b : BOOLEAN
a ← TRUE
b ← FALSE
IF a AND b THEN
OUTPUT "Both true"
ELSE
OUTPUT "Not both true"
ENDIF
// 23. File Copy
DECLARE line : STRING
OPENFILE "source.txt" FOR READ
OPENFILE "destination.txt" FOR WRITE
WHILE NOT EOF("source.txt")
READFILE "source.txt", line
WRITEFILE "destination.txt", line
ENDWHILE
CLOSEFILE "source.txt"
CLOSEFILE "destination.txt"
// 24. Arithmetic Modulus
DECLARE result : INTEGER
result ← 10 MOD 3
OUTPUT result
// 25. Array with Loop
DECLARE nums : ARRAY[1:3] OF INTEGER
DECLARE i : INTEGER
FOR i ← 1 TO 3
nums[i] ← i * 2
OUTPUT nums[i]
NEXT i
// 26. BREAKPOINT
DECLARE i : INTEGER
FOR i ← 1 TO 10
IF i = 5 THEN
OUTPUT "Breaking loop"
BREAKPOINT
ENDIF
OUTPUT i
NEXT i
// 27. String Concatenation
DECLARE fullName : STRING
fullName ← "John" & " " & "Doe"
OUTPUT fullName
// 28. Numeric Casting
DECLARE num : REAL
num ← 5.75
OUTPUT INT(num)
// 29. Pass by Reference Procedure
DECLARE Temp : INTEGER
PROCEDURE SWAP (BYREF X : INTEGER, BYREF Y : INTEGER)
Temp ← X
X ← Y
Y ← Temp
ENDPROCEDURE
DECLARE A, B : INTEGER
A ← 5
B ← 10
CALL SWAP(A, B)
OUTPUT A
OUTPUT B
// 30. Passing Parameters by Value
PROCEDURE INCREMENT (BYVAL N : INTEGER)
N ← N + 1
ENDPROCEDURE
DECLARE M : INTEGER
M ← 5
CALL INCREMENT(M)
OUTPUT M
// 31. Bubble Sort Algorithm
DECLARE nums : ARRAY[1:5] OF INTEGER
nums[1] ← 5
nums[2] ← 2
nums[3] ← 9
nums[4] ← 1
nums[5] ← 6
FOR i ← 1 TO 4
FOR j ← 1 TO 5 - i
IF nums[j] > nums[j+1] THEN
temp ← nums[j]
nums[j] ← nums[j+1]
nums[j+1] ← temp
ENDIF
NEXT j
NEXT i
FOR i ← 1 TO 5
OUTPUT nums[i]
NEXT i
// 32. Factorial Calculation
FUNCTION Factorial(n : INTEGER) RETURNS INTEGER
IF n = 0 THEN
RETURN 1
ELSE
RETURN n * Factorial(n - 1)
ENDIF
ENDFUNCTION
DECLARE number : INTEGER
number ← 5
OUTPUT "Factorial of ", number, " is ", Factorial(number)
// 33. Linear Search
DECLARE nums : ARRAY[1:5] OF INTEGER
DECLARE target, i : INTEGER
nums[1] ← 10
nums[2] ← 20
nums[3] ← 30
nums[4] ← 40
nums[5] ← 50
INPUT target
FOR i ← 1 TO 5
IF nums[i] = target THEN
OUTPUT "Found at index ", i
ENDIF
NEXT i
// 34. Prime Number Check
FUNCTION IsPrime(num : INTEGER) RETURNS BOOLEAN
IF num <= 1 THEN
RETURN FALSE
ENDIF
FOR i ← 2 TO (num-1)
IF num MOD i = 0 THEN
RETURN FALSE
ENDIF
NEXT i
RETURN TRUE
ENDFUNCTION
DECLARE number : INTEGER
INPUT number
IF IsPrime(number) THEN
OUTPUT number, " is a prime number"
ELSE
OUTPUT number, " is not a prime number"
ENDIF
// 35. Fibonacci Sequence
DECLARE n, i, a, b, temp : INTEGER
INPUT n
a ← 0
b ← 1
OUTPUT a
FOR i ← 1 TO n - 1
OUTPUT b
temp ← b
b ← a + b
a ← temp
NEXT i
// 36. Merge Two Arrays
DECLARE array1 : ARRAY[1:3] OF INTEGER
DECLARE array2 : ARRAY[1:3] OF INTEGER
DECLARE mergedArray : ARRAY[1:6] OF INTEGER
array1[1] ← 1
array1[2] ← 2
array1[3] ← 3
array2[1] ← 4
array2[2] ← 5
array2[3] ← 6
FOR i ← 1 TO 3
mergedArray[i] ← array1[i]
mergedArray[i+3] ← array2[i]
NEXT i
FOR i ← 1 TO 6
OUTPUT mergedArray[i]
NEXT i
// 37. String Reverse
FUNCTION ReverseString(s : STRING) RETURNS STRING
DECLARE reversed : STRING
reversed ← ""
FOR i ← LENGTH(s) TO 1 STEP -1
reversed ← reversed & MID(s, i, 1)
NEXT i
RETURN reversed
ENDFUNCTION
DECLARE inputStr : STRING
INPUT inputStr
OUTPUT "Reversed: ", ReverseString(inputStr)
// 38. Calculate Average
DECLARE nums : ARRAY[1:5] OF INTEGER
DECLARE sum, average : REAL
nums[1] ← 10
nums[2] ← 20
nums[3] ← 30
nums[4] ← 40
nums[5] ← 50
sum ← 0
FOR i ← 1 TO 5
sum ← sum + nums[i]
NEXT i
average ← sum / 5
OUTPUT "Average: ", average
// 39. Sum of Digits
FUNCTION SumOfDigits(number : INTEGER) RETURNS INTEGER
DECLARE sum, digit : INTEGER
sum ← 0
WHILE number > 0
digit ← number MOD 10
sum ← sum + digit
number ← number DIV 10
ENDWHILE
RETURN sum
ENDFUNCTION
DECLARE num : INTEGER
INPUT num
OUTPUT "Sum of digits: ", SumOfDigits(num)
// 40. Find Largest Number in Array
FUNCTION FindLargest(nums : ARRAY OF INTEGER) RETURNS INTEGER
DECLARE max, i : INTEGER
max ← nums[1]
FOR i ← 2 TO LENGTH(nums)
IF nums[i] > max THEN
max ← nums[i]
ENDIF
NEXT i
RETURN max
ENDFUNCTION
DECLARE numbers : ARRAY[1:5] OF INTEGER
numbers[1] ← 3
numbers[2] ← 8
numbers[3] ← 1
numbers[4] ← 6
numbers[5] ← 4
OUTPUT "Largest number: ", FindLargest(numbers)
- 作者:现代数学启蒙
- 链接:https://www.math1234567.com/pseudocdequestions
- 声明:本文采用 CC BY-NC-SA 4.0 许可协议,转载请注明出处。
相关文章