COMPUTER SCIENCE TUTORIALS

 C LANGUAGE NOTES:


Q. WHAT IS Algorithm explain its features?

Ans:   

A sequential solution of any program that written in human language, called algorithm.“Algorithm" is a formally defined a step by step procedure for performing some   calculation. If a procedure is formally defined, then it must be implemented using a programming language.

            The algorithm gives logic of the program, that is, a step-by-step description of how to arrive at a solution. Algorithm provides a blueprint to write a program to solve a particular problem in finite number of steps. Algorithms are mainly used to achieve software re-use

  • it is a procedure for performing some calculations.
  • it gives the logic of the program i.e. a step by step description of how to arrive at a solution.
  • Algorithm provides a blue print for writing a program to solve a particular problem.

a well defined algorithm always provides an answer and it guaranteed to terminate.

Features of Algorithms:

            An algorithm exhibits three key features:

v  Sequence

v  Decision

v  Repetition

1)      Sequence: Sequence means that each step of the algorithm is executed in the specified order. That means, the algorithm performed the steps in a purely sequential order. The following algorithm shows to add two numbers.

Step 1: Input the first number as A

Step 2: Input the second number as B

Step 3: sum=A+B

Step 4: Print sum

Step 5: End

 

2)      Decision: Decision statements are used when the outcome of the process depends on some condition. For example, if X=Y, then print “Equal”. Hence, the general form of the ‘if’ construct can be given as

if  condition then process

            A decision statement can also be stated in the following manner:

            if  condition

                 then process1

            else

               process2

The above form is commonly known as the ‘if-else’ construct. Here, if the condition is true then “process1” is executed, else “process2” is executed. An algorithm to check the equality of two numbers is written as follows:

Step 1: Input the first number as A

Step 2: Input the second number as B

Step 3: if A=B

            then print “Equal”

            else

             print “Not equal”

Step 4: End

3)      Repetition: Repetition involves executing one or more steps for a number of times. This can be implemented by using the constructs such as the while, do-while and for loops. These loops execute one or more steps until some condition is true. The following algorithm demonstrates that prints the first 10 natural numbers.

Step 1: [initialize] set I=0, N=0

Step 2: Repeat steps 2 & 3 while I<=N

Step 3: print I

         Set I=I+1

Step 4: End

Examples:

Addition of two numbers.(Sequence Algorithm)

 

Step1:- Input the first number as A.

Step2:- Input the second number as B.

Step 3:- set sum=A+B;

Step 4:- print sum;

Step5:- end.

Finding Biggest number (Decision Algorithm)

Step1:- Input the first number as A.

Step2:- Input the second number as B.

Step3:- if A>B then print A

Else if A<B then print B

Else print “both are equal”.

Step4:- end.

Sum of numbers upto N (Repetition Algorithm)

Step1:- input N.

Step2:- set I=1, sum=0

Step3:- Repeat Step4 & Step 5 while(I<=N)

Step4:- set sum=sum+I

Step5:- set I=I+1;

Step6:- print sum

Step7:- end

Q. Write about Flowchart?

A flowchart is a diagrammatic representation that illustrates the sequence of operations to be performed to get the solution of a problem. Flowcharts are generally drawn in the early stages of formulating computer solutions. Flowcharts facilitate communication between programmers and business people. These flowcharts play a vital role in the programming of a problem and are quite helpful in understanding the logic of complicated and lengthy problems. Once the flowchart is drawn, it becomes easy to write the program in any high level language. Often we see how flowcharts are helpful in explaining the program to others. Hence, it is correct to say that a flowchart is a must for the better documentation of a complex program.

1. Graphical representation of any program is called flowchart.
2. There are some standard graphics that are used in flowchart as following:

Examples of flowcharts in programming

Draw a flowchart to add two numbers entered by user.

Flowchart to add two numbers in programming

 

 

Draw flowchart to find the largest among three different numbers entered by user.

Flowchart to find largest among three numbers

Draw a flowchart to find all the roots of a quadratic equation ax2+bx+c=0

Flowchart of roots of quadratic equation

 

Draw a flowchart to find the Fibonacci series till term≤1000.

Flowchart of Fibonacci sequence in programming

Though, flowcharts are useful in efficient coding, debugging and analysis of a program, drawing flowchart in very complicated in case of complex programs and often ignored


Comments

Popular posts from this blog

JAVA NOTES

JAVA NOTES