Tìm nhiệt độ trung bình• Một chương trình yêu cầu nhập trên màn hình nhiệt độ cao nhất và thấp nhất trong ngày.. Sau đó tính và hiện trên màn hình nhiệt độ trung bình.. Ví dụ 3 Tính t
Trang 1DATA STRUCTURE AND ALGORITHM
FLOWCHARTS
CẤU TRÚC DỮ LIỆU VÀ GIẢI THUẬT
Dr Dao Nam Anh
Trang 2Resource - Reference
Slides of Achmad Yani (Electrical Engineering
Department, Gadjah Mada University), and Rasime
Uyguroğlu (Electrical & Electronic Eng.Dept,
Eastern Mediterranean University) edit by Dao Nam
Anh
Major Reference:
“Algorithms” Princeton University, 2011,
Addison Wesley
• Cấu trúc dữ liệu và giải thuật, Đinh Mạnh Tường.
Trang 4The three basic control structures
Các thành phần cơ bản
Trang 51 Sequence – Nối tiếp
Statemement a
Statemement b
Statemement c
Trang 62 Selection – Lựa chọn
Condition p?
Statemement a Statemement b
Trang 9Ví dụ 1 Cộng 3 số
Một chương trình A cần đọc 3 con số, cộng chúng
với nhau và in ra kết quả
Trang 10• Defining diagram – Thiết kế sơ đồ
Input
Đầu vào
Processing Thực hiện
Output Kết quả
Số thứ 1
Số thứ 2
Số thứ 3
Đọc 2 con số Cộng vào
In tổng
Tổng
Trang 11Solution Algorithm – Giải thuật
Add numbers to total
ReadNumber1Number2Number3
Print total
Start
Stop
Trang 12Ví dụ 2 Tìm nhiệt độ trung bình
• Một chương trình yêu cầu nhập trên
màn hình nhiệt độ cao nhất và thấp
nhất trong ngày Sau đó tính và hiện
trên màn hình nhiệt độ trung bình.
• A program is required to prompt the terminal operator for the maximum
and minimum temperature readings on a particular day, and calculate
and display to the screen the average temperature, calculated by
(maximum temperature + minimum temperature)/2.
Trang 14Solution Algorithm
Avg_temp= (Max_temp-Min_temp)/2
ReadMax_tempMin_temp
Print Avg_temp
Start
Stop
Trang 15Ví dụ 3 Tính thời gian dọn vườn
• Một chương trình yêu cầu nhập chiều dài,
chiều rộng mảnh đất, chiều dài, chiều rộng
ngôi nhà xây trên đó Sau đó tính và hiện trên
màn hình thời gian cần thiết để cắt cỏ xung
quanh ngôi nhà, theo tốc độ cắt cỏ m2/1h cho
trước.
• A program is required to read from the screen the lenght and widht of a
rectangular house block, and the lenght and width of the rectangular house that
has been built on the block The algorithm should then compute and display the
mowing time required to cut the grass around the house, at the rate of two square
metres per minute.
Trang 18Flowchart and the selection control structure
Lựa chọn
Trang 20Null ELSE statement
Trang 21Combined IF statement
Student =P/T ANDGender =
F ?
Increment Female_part_time_count
T
F
Trang 22Nested IF statement
Increment
Counter_A
Record Code =`A‘ ?
Increment Counter_B
Increment Counter_C
Increment Error_counter
Record Code =`A‘ ?
Record Code =`A‘ ?
Trang 23Ví dụ 4 Đọc ký tự
ký tự, sau đó sắp xếp theo thứ tự, và
hiện lên màn hình
• Design an algorithm that will prompt a terminal
operator for three characters, accept those
characters as input, sort them into ascending
sequence and output them to the screen.
Trang 24Char_1Char_2Char_3
Trang 25Solution Algorithm ?
Trang 26Case Structure
Case Of variable
Value 1 Value 2 Value 3 Value 4
Trang 28Flowchart and Array
Mảng
thi môn toán cao cấp, tính điểm trung
bình, sau đó hiện lên màn hình tất cả các
điểm, đồng thời hiện điểm trung bình
• Design a program that will prompt for and receive
18 examination scores from a mathematics test,
compute the class average, and display all the
scores and the class average to the screen.
Trang 29• Defining diagram
Lưu các điểm vào mảngTính trung bình
Hiển thị các điểmHiển thị điểm trung bình
18 điểm thiĐiểm trung bình
Trang 30Control Structures required Cấu trúc mảng và vòng lặp
2 Index – xác định vị trí các phần tử của mảng
Trang 31I <= 18 ?
Display average
Trang 32Flowchart and Module
Thiết kế chương trình, nhập 3 chữ cái, và sắp xếp,
Hiển thị lên màn hình.
Chương trình cư tiếp tục nếu vẫn còn tiếp tục nhập
các chữ cái dạng `XXX`
Design a solution algorithm that will prompt a terminal operator for three
characters, accept those characters as input, sort them into ascending
sequence and output them to the screen The algorithm is to continue to
read characters until ´XXX`is entered.
Trang 33Char_1Char_2Char_3
Trang 35Start
PromptFor characters
Sort_
Three_
characters
Outpurcharacters
Get characters
PromptFor characters
Getcharacters
Stop
Characters NOT = xxx ?
F
T
Trang 37Bài tập: Tính lương
Một chương trình yêu cầu nhập mã số nhân viên,
lương giờ, số giờ đã làm trong tuần Sau đó kiểm
tra lương giờ, số giờ Nếu đúng sẽ tính lương của
tuần, và hiển thị.
A program is required by a company to read an employee‘s number,
pay rate and the number of hours worked in a week The
program is then to validate the pay rate and the hours worked
fields and, if valid, compute the employee‘s weekly pay and
print it along with the input data
Trang 38Bài tập: Tính lương
Kiểm tra: Số giờ làm việc tối đa trong 1
tuần là 60 giờ Nếu lương giờ nằm ngoài
phạm vi cho phép, chương trình báo lỗi
và không tính.
Tính lương tuần: Là tích của lương giờ với
số giờ.
Nếu số giờ lớn hơn 35: làm ngoài giờ,
được tính thêm ½ mức lương giờ
Trang 39Emp_noPay_rateHrs_workedEmp_weekly_payError_message
Trang 41Solution Algorithm
Trang 43Input_
Fields = T
Stop
Trang 44Bài tập 1
Thiết kế chương trình nhập các cạnh hình
chữ nhật Tính và hiển thị diện tích hình
chữ nhật
Trang 45Bài tập 2
• Viết chương trình giải phương trình bậc hai
• Gợi ý: tính Delta d = sqrt ( ), và hai
nghiệm là: x1 = (–b + d)/2a, x2 = (–b – d)/2a
Trang 47Bài tập 2
• Viết chương trình giải phương trình bậc hai
• Gợi ý: tính Delta d = sqrt ( ), và hai
nghiệm là: x1 = (–b + d)/2a, x2 = (–b – d)/2a
Trang 49Bài tập 3
và hiển thị số đó
• Write an algorithm that reads two values, determines the largest value
and prints the largest value with an identifying message.
ALGORITHM
MAX ← VALUE1 else
MAX ← VALUE2 endif
Trang 51Bài tập 3
đó
• Write an algorithm that reads two values, determines the largest value
and prints the largest value with an identifying message.
ALGORITHM
MAX ← VALUE1 else
MAX ← VALUE2 endif
Trang 52Bài tập 4
• Chương trình nhập 3 số, tìm số lớn nhất và
hiển thị số đó
Trang 53MAX ←VALUE2
is VALUE1>VALUE2
“The largest value is”,
MAX
Trang 54Bài tập 4
• Chương trình nhập 3 số, tìm số lớn nhất và
hiển thị số đó
Trang 56MAX ←VALUE3
is VALUE1>VALUE2
is VALUE1>VALUE3
is VALUE2>VALUE3
MAX ←VALUE3
MAX ←VALUE2
“The largest value is”,
MAX
Trang 57Discussion – Câu hỏi
• https://sites.google.com/site/daona
manhedu/data-structure-algorithm