You are on page 1of 32

How Computers Solve Problems

Computers also use Algorithms to solve problems, and change data into information Computers can only perform one simple step at a time Complex Human Algorithms must be broken down into simple step-by-step instructions BEFORE they can be translated into computer code

Algorithm
It is the set of instructions to solve any problem. These are the steps followed after one another. These steps for solving a problem, to process some input for obtaining the desired result or output is called ALGORITHM

Characteristics
It should have input Steps that are included in the algorithm should be executable by the computer. Each step must be finite and small and unambiguous Every step of algorithm should yield some result. Should be written in a very simple language Algorithm validation

Algorithm :Temperature Conversion


1. Read the numerical value of C 2. Calculate F using formula F=9/5C+32 3. Write numerical value of C 4. Terminate the process

Algorithm: Average of two numbers


1. Input: Two numbers 2. Add the two numbers 3. Divide the result by 2 4. Return the result by step 2 5. End

Algorithm: Change a numeric grade to a letter grade.


Input: One number 1. if (the number is between 90 and 100, inclusive) then
1.1 Set the grade to A End if

2. if (the number is between 80 and 89, inclusive) then


2.1 Set the grade to B End if

3.if (the number is between 70 and 79, inclusive) then


3.1 Set the grade to C End if

4. if (the number is between 60 and 69, inclusive) then


4.1 Set the grade to D End if

5. If (the number is less than 60) then


5.1 Set the grade to F End if

6. Return the grade


End

Algorithm:
1. 2. 3.

Find largest of 1000 numbers

4.

FindLargest Input: 1000 positive integers Set Largest to 0 Set Counter to 0 while (Counter less than 1000) 3.1 if (the integer is greater than Largest) then 3.1.1 Set Largest to the value of the integer End if 3.2 Increment Counter End while Return Largest End

Algorithm :
1. 2. 3.

Iterative factorial

4.

Factorial Input: A positive integer num Set FactN to 1 Set i to 1 while (i is less than or equal to num) 3.1 Set FactN to FactN x I 3.2 Increment i End while Return FactN End

1. Start 2. Read the roll number and marks of 1 student. This is the highest marks found so far as marks of only one student has been read 3. Is there any more student in the class 4. If yes, then go to step 5 .If not ,then goto step 8 5. Read next roll number and its marks .Compare the marks of this next roll number with the highest marks find so far 6. If marks read in step 5 is greater than the highest marks found so far , then erase the previously noted highest marks , and replace it by the marks read in step 5 .Also replace the roll number, if marks of step 5 are less than the highest marks, then do nothing. 7. Repeat the step 3 through 5 till all the roll numbers of class have been entered. 8. Print the roll number of the student with the highest marks and percentage of the student. 9. Stop

7. Repeat the step 3 through 5 till all the roll numbers of class have been entered. 8. Print the roll number of the student with the highest marks and percentage of the student. 9. Stop

Problem Solving
Problem Solving is the ability to understand what you have, what you want, and creating a set of instructions to change what you have into what you want Good Problem Solving Skills are based on knowledge, experience and logic Good Programmers NEVER make assumptions

Flowcharts and Pseudo Code


A Standard way of describing an algorithm must exist if we expect our solution to be understood by others easily There are two standards in programming:
FLOWCHARTS PSEUDOCODE ACTUAL CODE

Flowcharts
A Flowchart is a Visual Representation of an algorithm A Flowchart uses easy-to-understand symbols to represent actions on data and the flow of data Flowcharts aid in breaking down a problem into simple steps

Advantages
Each symbol denotes the instruction Pictorial representation it becomes easy to understand and explain the problem. It gives clear picture of flow of data in any program It helps the programmer to determine the type of logic control to be used in the program It is easy to detect the error with the help of flow chart It serves as a guiding document for program writing or coding

Limitations
Time consuming and laborious too especially for complex problems Redrawing is even more difficult and time consuming. Difficult to include new step in the existing flowchart If an algorithm has complex branches and loops , flowchart become very difficult to draw.

Pseudo Code
Pseudo means pretend or false Pseudo Code is pretend or false computer code; generic English-like terms that are somewhat like computer code Pseudo Code is not as standardized as flowcharts, and does not facilitate the breaking down of problems as well as a flowchart does

Tips on Writing Good Pseudo Code


Use indention for improved clarity Do not put code in pseudo code make your pseudo code language independent Dont write pseudo code for yourself write it in an unambiguous fashion so that anyone with a reasonable knowledge can understand and implement it

Be consistent
Prefer formulas over English language descriptions

The Three Computer Programming Constructs


Any problem, regardless of how complex, can be broken down into three basic CONSTRUCTS SEQUENCE SELECTION ITERATION

THREE CONSTRUCTS

Three constructs

Flowcharts for three constructs

Pseudocode for three constructs

Start or stop

Process

Input or output

Flowchart Elements

Decision Flow line Connector Off-page connector

Flowchart and the selection control structure

Simple IF statement
T Account_ balance < $300? F

Service_charge = $5

Service_charge = $2

Null ELSE statement

x<0

T X= -x

Combined IF statement
F Student = P/T AND Gender = F? T
Increment Female_part_time_count

Nested IF statement
T
Record Code =`A

F
? T
Record

F
Code =`A

Increment Counter_A

?
Record Code =`A

F ?

Increment Counter_B

T
Increment Error_counter

Increment Counter_C

Case Structure
Case Of variable

Value 1

Value 2

Value 3

Value 4

Statement_a

Statement_b

Statement_c

Statement_d

Summation

More numbers

Product

You might also like