Flowchart, Pseudocode और Decomposition
Algorithm दिखाने के तरीके
Algorithm आसान steps में लिख सकते हैं (जैसे पिछले lesson में), पर दो standard रूप ज़्यादा साफ़ हैं: flowchart (picture) और pseudocode (structured English)।
Flowchart
- Standard shapes और arrows वाला diagram
- Flow एक नज़र में दिखता है
- छोटे algorithms के लिए अच्छा
- बड़े programs में बदलना मुश्किल
Pseudocode
- IF, WHILE, PRINT जैसे keywords वाला text
- असली code के करीब
- बदलना आसान
- कोई standard picture नहीं
Flowchart symbols
| Symbol | नाम | किसलिए |
|---|---|---|
| ⬭ Oval (गोल किनारे) | Terminator | Start / Stop |
| ▱ Parallelogram | Input / Output | Input लेना, output print करना |
| ▭ Rectangle | Process | Calculation या assignment, जैसे sum = a + b |
| ◇ Diamond | Decision | Yes/No प्रश्न, दो रास्ते |
| → Arrow | Flow line | Steps का क्रम दिखाता है |
| ○ छोटा गोला | Connector | लंबे flowchart के हिस्से जोड़ता है |
Flowchart: sequence
Steps एक के बाद एक, ऊपर से नीचे चलते हैं। Example: simple interest SI = P × R × T ÷ 100।
Flowchart: selection (if-else)
Diamond दो में से एक रास्ता चुनता है। Example: marks ≥ 33 हो तो "Pass", नहीं तो "Fail"।
Flowchart: repetition (loop)
Arrow पिछले step पर वापस जाता है ताकि condition true रहने तक steps दोहराए जाएँ। Example: 1 से N तक के numbers का जोड़।
n = int(input('Enter N: '))
total = 0
i = 1
while i <= n:
total = total + i
i = i + 1
print('Sum =', total)Pseudocode
Pseudocode ("नकली code") algorithm को capital letters के simple keywords — INPUT, COMPUTE, PRINT, IF … THEN … ELSE, WHILE, FOR — और blocks दिखाने के लिए indentation से लिखता है।
INPUT marks
IF marks >= 90 THEN
PRINT "Grade A"
ELSE IF marks >= 60 THEN
PRINT "Grade B"
ELSE
PRINT "Grade C"
END IFINPUT N
SET fact = 1
FOR i = 1 TO N
fact = fact * i
END FOR
PRINT factहर line में एक action लिखें, IF और loops की body indent करें, और Python के colon या brackets की चिंता न करें। कोई भी इसे पढ़ सके।
Flow of control
Steps जिस क्रम में चलते हैं उसे flow of control कहते हैं। हर algorithm सिर्फ़ तीन building blocks से बनता है:
Decomposition
Decomposition यानी बड़ी problem को छोटे हिस्सों में तोड़ना। हर हिस्सा अलग से solve होता है (अक्सर अलग team member से) और फिर हिस्से जोड़े जाते हैं। School result system ऐसे decompose हो सकता है:
📁 result-system/ ├── read_marks # हर student के marks input ├── total_and_percent # calculations ├── grade # A1, A2, B1 ... ├── rank # students sort └── print_report_card # output
Python में हर sub-problem एक function बनता है:
# Decomposition: one big task split into small functions
def read_marks():
return [78, 85, 92, 66, 81] # sample marks
def total_of(marks):
return sum(marks)
def grade_of(percent):
if percent >= 90: return 'A1'
if percent >= 80: return 'A2'
if percent >= 70: return 'B1'
return 'B2'
marks = read_marks()
t = total_of(marks)
p = t / len(marks)
print('Total:', t, '| Percent:', p, '| Grade:', grade_of(p))सारांश और practice
- Flowchart symbols: oval (start/stop), parallelogram (I/O), rectangle (process), diamond (decision), arrows (flow)।
- Pseudocode INPUT, IF, WHILE, PRINT जैसे keywords और indentation use करता है।
- Flow of control: sequence, selection, repetition।
- Decomposition बड़ी problem को छोटी problems में बाँटता है।
Flowchart बनाएँ और pseudocode लिखें: (1) किसी number का पहाड़ा print करना, (2) number positive, negative या zero है।
Hint: (1) में 1 से 10 तक loop; (2) में दो decisions: N > 0, फिर N < 0।
