Modul 1 Complete
Modul 1 Complete
LEESHMA K
ASSISTANT PROFESSOR
DEPT. OF COMPUTER SCIENCE
ST.JOSEPH’S COLLEGE , DEVAGIRI
References:
Step 3: carry out the plan and write the actual code.
Step 4: look back and possibly refactor your solution if it could be better.
Problem solving
Problem solving is a sequential process of analyzing information related to a given situation and generating
appropriate response options.
• In order to solve a problem by the computer , one has to pass through certain stages or steps. They are :
• It is very important to specify exactly the set of inputs that the problem needs to handle.
• Consider edge cases and exceptions: Think about any potential edge cases or exceptions that might arise in the
problem. These can include scenarios where inputs are at their minimum or maximum values, cases where
unexpected or invalid input is provided, or situations where certain conditions may cause the solution to behave
differently.
• Research if necessary: If the problem involves unfamiliar concepts or techniques, consider conducting research to
enhance your understanding.
2. Formulate the problem
• Processing part of the problem
• define the problem in a clear and concise manner
• It involves understanding the requirements, constraints, and desired outcomes, and translating them into a
well-defined problem statement.
• Define the boundaries and scope of the problem.
• Provide examples or sample inputs and expected outputs to illustrate the problem statement.
• Review the problem statement to ensure that it captures all the necessary information accurately and
completely.
3. Develop an algorithm
Develop a precise plan of what we want the computer to do.
A set of sequential steps usually written in ordinary language to solve a given problem is
called Algorithm.
It may be possible to solve a problem in more than one ways, resulting in more than one
algorithm.
Algorithms can solve many, but not all, problems
Many problems can be solved in a reasonable time
Some require heuristic (approximate) approaches to solve them in a reasonable time
Some problems cannot be solved using any algorithm
4. Write the program
• Now we have to transform the algorithm from into a set of instructions that can be
understood by the computer.
• Writing a program is often called “writing code” or “implementing algorithm” . So
the source code is actually the program itself.
• Below is a program that implements our algorithm for finding the average of a set
of grades
5. Test the program
• Once you have a program written that compiles , you need to make sure that it solves the problem that it
A software bug is an error or mistake that causes a computer program to misbehave. These bugs
are generally the result of mistakes made by the programmer either in the design or the source
• In other words, if an error is encountered during the test it can cause malfunction. For example, incorrect
a proper program.
• You should fix as many bugs in your program as you can find.
• To find bugs effectively , you should test your program with many test cases .
• It is also a good idea to have others test your program because they may think up situations or input
• The process of finding and fixing errors in your code is called debugging.
6. Evaluate the solution
• Evaluating the solution is an essential step in problem solving as it helps
determine the effectiveness, feasibility, and suitability of the proposed
solution.
• It involves assessing whether the solution addresses the underlying
problem, meets the desired objectives, and considers various factors such
as cost, resources, and potential risks.
• Once your program produces a result that seems correct , you need to re-consider
the original problem and make sure that the answer is formatted into a proper
• It is important to remember that the computer will only do what you told it to do.
• READ and WRITE are in caps. There are just a few keywords we will use:
• READ, WRITE, IF, ELSE, ENDIF, WHILE, ENDWHILE, REPEAT, UNTIL
3. Indent to show hierarchy
• The ENDIF (or END whatever) always is in line with the IF (or whatever starts the
structure).
• Use any of the END keywords to end multi-line sections (ENDIF, ENDWHILE).
5. Keep statements language independent
Resist the urge to write in whatever language you are most comfortable with. In the
long run, you will save time! There may be special features available in the language
you plan to eventually write the program in; if you are SURE it will be written in that
language, then you can use the features. If not, then avoid using the special features.
Advantages of Pseudo code
• Improves the readability of any approach. It’s one of the best approaches to start implementation
of an algorithm.
• Acts as a bridge between the program and the algorithm or flowchart. Also works as a rough
documentation, so the program of one developer can be understood easily when a pseudo code is
written out. In industries, the approach of documentation is essential. And that’s where a pseudo-
code proves vital.
• The main goal of a pseudo code is to explain what exactly each line of a program should do, hence
making the code construction phase easier for the programmer.
find area of a circle of radius r.
• Inputs to the algorithm: Radius r of the circle.
• Expected output : Area of the circle.
• Algorithm
• Step 1: start
Step 2: Read/input the radius r of the circle.
Step 3 : Area pi*r*r //calculation of area
Step 4 : Print Area.
Step 6 : End
• Inputs to the algorithm: First num1, Second num2
• Expected output : sum of two numbers
• Algorithm
Step 1: start Write an algorithm to read two
numbers and find their sum
Step 2 : read the first num1
Step 3 : read the second num2
Step 4 : Sum ------ num1+num2 //calculation of sum
Step 5 : Print Sum
Step 6 : End
What is a Flowchart?
r*r
Write the algorithm and flow chart to convert temperature from
Fahrenheit to Celsius.
– Sequences
– Branching ( Selection)
– Loop ( Repetition)
Conditional Flow)
• Selection Logic Selection Logic simply involves a number of
conditions or parameters which decides one out of several
written modules. The structures which use these type of
logic are known as Conditional Structures. These structures
can be of three types:
1. simple if
2. If-else
3. If –elseif-else
Simple if
If (condition) then:
[Module A]
[End of If structure]
If (Condition), then:
[Module A]
Else:
[Module B]
[End if structure]
If (condition A), then:
[Module A]
Else if (condition B), then:
[Module B]
..
..
Else if (condition N), then:
[Module N]
[End If structure]
Flow chart of if-else
Write algorithm and draw flow chart to find the greater
number between two numbers.
• Algorithm
• Step 1 : start
• Step 2 : read / Input A and B
READ
• Step 3 : If A greater than B then print A
• Step 4 : If B greater than A then print B
• Step 5 : End
PRIN PRINT
T
Write algorithm and draw flow chart to find the
greater number between three numbers.
Inputs to the algorithm : Temperature in
Fahrenheit.
• Expected output : Temperature in Celsius.
• Algorithm
Step 1 : start
Step 2 : read / Input A ,B and C
Step 3 : If(A>=B) and (A>=C) then print A is the largest.
Step 4 : If(B>=A) and (B>=C) then print B is the largest.
Step 4 : If(C>=A) and (C>=B) then print A is the largest.
Step 5 : End
Example 1
• Write an algorithm to determine a student’s final
grade and indicate whether it is passing or failing. The
final grade is calculated as the average of four marks.
Pseudo code:
1. Start
2. Input a set of 4 marks
3. Calculate their average by summing and dividing by 4
4. if average is below 50 Print
“FAIL”
5. else
Print “PASS”
6. Stop
1. START:
2. Initialize variables:
- mark1, mark2, mark3, mark4: float (input marks obtained in four subjects)
- average: float (to store the calculated average)
- passingGrade: float (define the minimum passing grade, e.g., 50)
3. Input marks for four subjects:
input.
4. Iterate through the array from the second element to the last element:
7. After the loop ends, maxElement will hold the maximum value in the array.
Step 5: f = f * i.
Step 6: Go to step 3.
Step 8: Stop.
Program to find all numbers between 1 to 100 divisible by 4
Algorithm:
Step 1: Start
Step 7: Go to step 3
Step 8: Stop
How does the while loop work?
Step 1. The while loop evaluates the controlling expression.
Step 2. If the controlling expression is false, the body of the while
statement is never executed, and control skips the while statement and
jumps to the next statement in the program.
Step 3. If the controlling expression is true or any non zero value, then
control jump within the body of while loop and execute the statements
in a sequential way. Might be there is only a single statement attached
with a while loop.
Step 4. After the execution of while loop statements, the process is
repeated from step 1.
While Loops
do...while loop
do while loop
• The do while loop is a post tested loop. Using the do-while
loop, we can repeat the execution of several parts of the
statements. The do-while loop is mainly used in the case
where we need to execute the loop at least once. The do-
while loop is mostly used in menu-driven programs where
the termination condition depends upon the end user.