Professional Documents
Culture Documents
Pseudo Code
Pseudo Code
Pseudo code
1 2
START
PROGRAM isodd program isOdd
Create variable Choice n ← get input from keyboard
Ask the user for a number
if n % 2 = 0 then
READ INPUT into Choice
print “No”
IF Choice is even THEN
PRINT "No" else
ELSE print “Yes”
PRINT "Yes" end if
ENDIF end program
END
7. Làm thế nào để viết pseudocode?
/*
objective: function is used to check
whether a number is prime or not
input: a positive integer (n)
output: true or false
● Thụt lề các câu lệnh và khối lệnh bởi nó sẽ giúp người
*/
đọc hiểu được cơ chế thực thi và kiểm soát. function isPrime (int n)
● Sử dụng quy ước đặt tên thích hợp: uppercase cho các int i
hằng số, camelcase cho các biến số, … for i ← 2 to n - 1 do
if n % 2 = 0 then
● Có thể sử dụng các cấu trúc tiêu chuẩn như: if-else, return false
end if
switch-case, for, while, do-while, … như cách chúng ta
i←i+1
sử dụng trong lập trình. end for
return true
7. Làm thế nào để viết pseudocode?
/*
objective: function is used to check whether a
number is prime or not
input: a positive integer (n)
● Đừng khiến pseudocode trở nên phức tạp
output: true or false
như những dòng code hoàn chỉnh.
*/
● Không nên viết nhiều đoạn code trên cùng public static boolean isPrime (int n)
một dòng. {
int i;
● Không nên sử dụng quá nhiều thuật ngữ for (i=0; i<n; i++) {
chuyên ngành bởi mục đích của pseudocode if (n % 2 == 0) return false;
chính là cách truyền đạt dễ đọc, dễ hiểu. } return true;
}
8. Thực hành đọc và viết pseudocode
function funct (int[] A)
int i, j Hãy đọc đoạn pseudocode và cho biết:
boolean flag a) Chức năng (objective)
for i ← 0 to n - 1 do b) Đầu vào (input)
flag ← false c) Đầu ra (output)
for j ← 0 to n - i - 1 do
if A[j] > A[j+1] then
swap A[j] and A[j+1]
Đáp án
swap = true
a) objective: sắp xếp các phần tử trong mảng theo
end if
j←j+1 thứ tự tăng dần.
end for b) input: mảng các số nguyên
if flag = false then c) output: mảng sắp xếp tăng dần
break for loop
end if
i←i+1
end for
return A
8. Thực hành đọc và viết pseudocode
Hãy viết một chương trình dưới dạng mã giả (pseudocode) để tìm ra sinh viên có điểm trung bình cao nhất.
program getTopStudent
// calculate avg
Student[] list ← get student list from file
for each student in list do
student.average = sum of points / totals of subjects
end for
Đừng khiến pseudocode
// find student has highest avg
int n = list.length trở nên phức tạp như
Student top = list[0] những dòng code hoàn
for i ← 1 to n - 1 do chỉnh !!!
if list[i].average > top.average then
top = list[i]
end if
i←i+1
end for
print top
end program
The End