What is an algorithm and how do you write one?
संक्षिप्त उत्तर
An algorithm is a precise, step-by-step set of instructions for solving a problem. It can be written in plain English, as pseudocode or as a flowchart, and then turned into a program. Good algorithms use three building blocks: sequence (steps in order), selection (if/else decisions) and iteration (loops).
स्टेप-बाय-स्टेप
- Decide the inputs and the expected output.
- Break the problem into small steps in the right order.
- Use selection for decisions: IF ... THEN ... ELSE.
- Use iteration to repeat steps: FOR or WHILE loops.
- Trace through the algorithm with test data to check it works.
हल किया गया उदाहरण
Pseudocode: find the largest of a list of numbers
- largest ← first number
- FOR each number in the list
- IF number > largest THEN largest ← number
- ENDFOR
- OUTPUT largest
आम गलतियाँ
- Writing vague steps such as "work out the answer".
- Forgetting to initialise a variable before a loop.
- Loops that never end because the condition never becomes false.
परीक्षा से पहले कमियाँ दूर करने का सबसे तेज़ तरीका है ट्यूटर के साथ वन-टू-वन क्लास। पहली क्लास मुफ़्त है।
कंप्यूटर विज्ञान ट्यूटर खोजें