Lbps
Lbps
Q5.Why Algorithm?
Ans. An algorithm is important in optimizing a computer program according to
the available resources. Ultimately when anyone decide to solve a problem
through better algorithms then searching for the best combination of program
speed and least amount of memory consumption is desired.
Q6. What are the types of algorithm ?
There are 7 Different types of algorithms those are used by computer
programmers.
1.Brute force
Brute force algorithm emphasis on solving problems in the most straight forward
manner. It implies to use basic techniques to solve problems. In short these are
simplest algorithms to be used. The simplicity costs in speed as this algorithm is
comparatively slow in generating results. The best way is to use it with problems
those are having small input size.
2.Divide and conquer
The basic idea of this method is to make programs based on dividing the size of
problems. In each loop cut the problem in parts with constant factor and then
process further in the same fashion. This is a fast algorithm.
3.Decrease and conquer
This method is having the basic idea as above method but the slight difference is
there. It decreases the size of problem in each iteration. In dive and conquer each
iteration cut the problem with a constant factor.
4.Dynamic programming
If you are searching of one efficient fast algorithm then dynamic programming is
here. In this algorithm all focus is made on speed of execution even it costs
memory space. Simply saying in this method space for time is sacrificed. The
execution speed drastically reduces in this algorithm. This method is particularly
useful to solve problems that those have overlapping sub problems.
5.Greedy algorithm
Greedy algorithm is a step based algorithm. In a greedy algorithm we analyze the
problem in each step. Then use the best locally possible optimum solution to this
particular step .Then the process repeats to all steps. It will lead to a globally
optimal solution.
6.Transform and conquer
This algorithm is based on using approaches we already know. The problem is
transformed in to well familiar step based solutions. At last we accumulate the
solution of each step at final step. Thus the name of this algorithm states self
about the working. For example if we need to find the lcm of a number and we
already know how to calculate GCD. This method says calculate LCM with the help
of GCD as we know
LCM ( a , b ) = (a * b) / GCD ( a , b )
7.Backtracking algorithm
In backtracking we generate a solution and test. If this solution is satisfying all
conditions then and then only we go for further subsequent solutions else we will
take a back gear simply saying backtrack and select a different path of finding
solution. This process continues until we get a final solution.
Q6. What are Qualities of a good algorithm ?
Ans.
1. Input and output should be defined precisely.
2. Each step in the algorithm should be clear and unambiguous.
3. Algorithms should be most effective among many different ways to solve a problem.
4. An algorithm shouldn't include computer code. Instead, the algorithm should be written
in such a way that it can be used in different programming languages.
Q6. Write an algorithm to find sum of two numbers ?
Ans.
Step 1: Start
Step 2: Declare variables num1, num2 and sum.
Step 3: Input values num1 and num2.
Step 4: Add num1 and num2 and assign the result to sum.
sum←num1+num2
Step 5: Display sum
Step 6: Stop
Different flowchart shapes have different conventional meanings. The meanings of some of the
more common shapes are as follows:
Terminator
The terminator symbol represents the starting or ending point of the system.
Process
A diamond represents a decision or branching point. Lines coming out from the
diamond indicates different possible situations, leading to different sub-processes.
Data/input
Off-Page Reference
This symbol would contain a letter inside. It indicates that the flow
continues on a matching symbol containing the same letter somewhere else on
a different page.
Delay or Bottleneck
Identifies a delay or a bottleneck.
Flow connection
Lines represent the flow of the sequence and direction of a process.
Q13.Why pseudocode?
Ans.It is a common language for writing statements in English / standard language that can be utilized by
any programming languages through programmer before writing actual codes . Writing pseudocode
allows us to get those brilliant ideas in our mind without having to worry about the syntax of the
programming language. Since it has no specific syntax, it is easy to read and write, and programmers can
comfortably communicate ideas and concepts, even if they are working on different programming
languages.
Ans.Operators :
Operators are used to perform various operation in the computer these
are:-
a.Mathematical - +,-,*,/,%
Used in mathematical calculation
ex. a+b, a-b, a*b etc.
Begin
set i to 1
for each i from 1 to 10
display i
end for loop
End
Ex.2 write an example to display even number
// display even number
Begin
Set I = 1
For each I from 1 to 50
begin
If modulas of I is equal to 0
begin
Display i
End
End for loop
End
Note: Pseudocode does not have a specific syntax so pseudocode can look
different in each.
Q 15.Write a Pseudocode for adding two numbers ?
Ans.
BEGIN
NUMBER s1, s2, sum
OUTPUT("Input number1:")
INPUT s1
OUTPUT("Input number2:")
INPUT s2
sum=s1+s2
OUTPUT sum
END
Conditional Construct
1. If ….else
2. Switch Case
Q.Write a pseudocode to a menu then let the user select any one of
one selection from the menu and display message.
begin
Declare var ans
Repeat
Begin
Output “Food menu”
Output “=========”
Output ”1.Samosa”
Output “2.Burgar”
Output “3.pizza”
Output “4.icecream”
Output “5.exit”
Output “Enter your choce from 1-5”
Accept ans
Switch( ans )
Begin
Case 1 : //begins
Output “you have opted for samosa”
Break
Case 2 :
Output “you have opted for burgar”
Break
Default :
Output “wrong input”
Break
Case 5 :
Output “exit program”
Break
Switch end
End
Until ans !=5
End
End