-->
Showing posts with label Algorithm. Show all posts
Showing posts with label Algorithm. Show all posts

Algorithm concept

 Systems Design:

In this phase we start design of proposed new system.It describes desired features and operations in detail, including screen layouts, business rules, process diagrams, pseudo code and other documentation. We can use two designing methods namely logical (what is required for IS) and physical (how to achieve requirements). At first we design logical and then it is converted into physical design. A prototype should be developed during the logical design phase if possible. The detailed design phase modifies the logical design and produces a final detailed design, which includes technology choices, specifies a system architecture, meets all system goals for performance, and still has all of the application functionality and behavior specified in the logical design. 

           This design can carry following activities:

  • Defining precisely the required system output

  • Determining the data requirement for producing the output

  • Determining the medium and format of files and databases

  • Devising processing methods and use of software to produce output

  • Determine the methods of data capture and data input

  • Designing Input forms

  • Designing Codification Schemes

  • Detailed manual procedures

  • Documenting the Design

We can use several tools while designing. They are: Algorithm, Flowchart, Pseudocode, etc. Let’s see in detail about these all designing tools.



1.Algorithm:

                                                   This is the first step or a tool which we apply while designing the system. It says about sequence of finite steps written in certain order manually by programmers to carry out job. While writing algorithm, we follow or use general English words which we use in our daily life. There is no particular rule for that. Mostly, it uses 4 units namely inputs, processes, storing and output. Let’s see an example to be clear.

General features of Algorithm:

·         Clear specification of input

·         Uses variables

·         Provides clear steps, processes, controls and specifications.

·         Uses subroutines.

Rules for algorithms:

1.  Finiteness

2.  Input if needed with processes should be shown

3.  Very simple language should be used

4.  The writer while writing algorithm should be keep in mind about program to be written after this.


Examples:-

1.An algorithm to get sum of two numbers:

Step 1: start

Step 2: get two numbers say a,b

Step 3: let sum=a+b

Step: 4 print sum

Step 5 stop

2.Algorithm to find square root of a number
Step1: Start
Step 2: Input  number as n
Step 3: Use sqrt function to find square root and store it in a
Step 4: Display a
Step 5: Stop
3.Algorithm to find volume of cube

Step1: Start

Step 2: Input  length as l

Step 3: Use pow function to find cube and store it in v(we can also use let, v=cube of l)

Step 4: Display v

Step 5: Stop

4.Algorithm to display area and perimeter of a rectangle 
Step1: Start
Step 2: Input length as l and breadth as b
Step 3: Use a=l*b and p=2(l+b)
Step 4: Display a and p
Step 5: Stop
5.Step1: Start

Step 2: Input initial velocity , time and acceleration as  u,t,a

Step 3: Use s=u*t+0.5*a*t*t

Step 4: Display s

Step 5: Stop

6.Algorithm to Calculate area and circumference of a circle

Step1: Start

Step 2: Input radius as r

Step 3: Use a=3.14*r*r  and c=2*3.14*r

Step 4: Display a and c

Step 5: Stop

7.Algorithm to convert temperature from Centigrade into Fahrenheit 

Step1: Start

Step 2: Input temperature in centigrade as c

Step 3: Use f=1.8*c+32

Step 4: Display f

Step 5: Stop

8.Algorithm to calculate sum of two distances and the distance is measured in terms of feet and inches


Step1: Start

Step 2: Input feet1,feet2,inch1,inch2

Step 3: Use feet3=feet1+feet2 and inch3=inch1+inch2

Step 4: newfeet=inch3/12+feet3  and newinch=inch3 % 12

Step 5: Display newfeet and newinch

Step6: Stop

9.Algorithm to enter number of days and convert it into years, months and days


Step1: Start

Step 2: Input days

Step 3: Divide days by 365 and store it as year(y)

Step 4: Remaining days(rd)  by finding remainder of days divide by 365

Step 5: For month(m) divide remaining days (rd) by 30 and take integer part.

Step 6: For days(d) divide  rd by 30 and find remainder.

Step 6: Display y , m , d 

Step 7 : Stop

-------------------------------------------------------------------------

some examples of control structures:


1.Algorithm to find area and circumference of  a real circle

Step1: Start

Step 2: Input  radius as r

Step 3: Check, is r>0?

3.1 If yes, calculate area and circumference and display it.

3.2 If no, display “This is not a real circle”.

Step 4: Stop

2.Algorithm to check odd or even

Step1: Start

Step 2: Input  number as num

Step 3: Calculate remainder by dividing num by 2

Step 4: Check is rem equal to zero?

4.1 If yes, Display “given number is even”

4.2 If no, Display “given number is odd”

Step 5: Stop

3.Algorithm to check whether the given number is positive, negative or zero.

Step1: Start

Step 2: Input a number as num

Step 3: Check that, is num greater than zero?

3.1 If yes, Display “ given number is positive”

3.2 If no, goto step 4

Step 4: Check that, is num less than zero?

4.1 If yes, Display “ given number is negative”

4.2 If no, Display “given number is zero”

Step 5: Stop

4.Algorithm to check loss or gain

Step1: Start

Step 2: Input sp and cp

Step 3: Check, is sp greater than cp?

3.1 If yes, calculate gain=sp-cp and display gain

3.2 If no, calculate loss=cp-sp and display loss.

Step 4: Stop

5.Algorithm to find the largest and smallest among three different numbers

Step1: Start

Step 2: Input  three different numbers as a,b and c

Step 3:  Check, is a>b and a>c?

3.1 If yes, Display “First number is largest”

3.2 If no, goto step 4

Step 4: Check is b>a and b>c?

4.1 If yes, Display “Second number is largest”

4.2 If no, Display “ Third number is largest”

Step 5: Check is a<b and a<c?

5.1 If yes, Display “First number is smallest”

5.2 If no, goto step 6

Step 6: Check is b<a and b<c?

6.1 If yes, Display “Second number is smallest”

6.2 If no, Display “Third number is smallest”

Step 7: Stop

6.Algorithm to find middle number among three different numbers

Step1: Start

Step 2: Input three numbers as a,b and c

Step 3: Check, is a<b AND a>c OR a>b AND a<c?

3.1 If yes, Display “First number is middle”

3.2 If no, go to step 4

Step 4: Check is b<a AND b>c OR b>a AND b<c?

4.1 If yes, Display “Second number is middle”

4.2 If no, Display “Third number is middle”

Step 5: Stop

7.Algorithm to calculate and display real roots of quadratic equation

Step1: Start

Step 2: Input the value of a , b and c

Step 3: Check, is (b2-4ac)>0?

3.1 If yes, use formula quadratic formula and display the roots

3.2 If no, Display “Sorry, we can not calculate real root”

Step 4: Stop

8.Algorithm to find commission amount

Step1: Start

Step 2: Input sales amount(sa)

Step 3: Check, is sa<1000?

3.1 If yes, calculate and print 5% commission.

3.2 If no, go to step 4

Step 4: Check, is sa>1001 and sa<2000?

4.1 If yes, calculate and print 10% commission.

4.2 If no, calculate and print 12% commission.

Step 5 : Stop

9.Algorithm for menu driven program

Step1: Start

Step 2: Input length(l) and breadth(b)

Step 3: Display menu and Enter your choice

Step 4: Check the user’s choice.

4.1 If the user enters (a) calculate the area of the rectangle and display.

4.2 If the user enters (b) calculate the perimeter and display.

4.3 If the user enters (c) exit from the program.

Step 5 : Stop

10Algorithm to that takes a number less than 10 and displays its multiplication table

Step1: Start

Step 2: Input any number(n)

Step 3: Check, is n<10?

3.1 If yes, 

for(i=1;i<=10;i++)

{

Print num*i

}

3.2 If no,

Print “Number is greater than 10”

Step 4 : Stop

--------------------------------------------------------------------------------------

Loop related algorithms:

----------------------------------

1.Algorithm to calculate and display : 1     3    5  …. to 10th terms

Step1: Start

Step 2: Set i = 0 and a=1

Step 3: Check, is i<10?

3.1 If yes, display ‘a’ and calculate a=a+2 and goto step 4

3.2 If no, go to step 5.

STep 4: Calculate i=i+1 and go to step 3

Step 5: Stop

2.Algorithm to display a square series of first ‘n’ natural numbers.


Step1: Start

Step 2: Input a number as n

Step 3: Set i=1

Step 4: Check, is i<=n?

4.1 If yes, Calculate sq=i*i ; Display “ sq ” and goto step 5

4.2 If no, goto step 6

Step 5: Calculate i=i+1 and go to step 4

Step 6: Stop

3.Algorithm to calculate the factorial of any number given by the user

Step1: Start

Step 2: Input a number as n

Step 3: Set f=1,i=1

Step 4: Check, is i <=n?

4.1 If yes, Calculate f=f*i and goto step 5

3.2 If no, goto step 6

Step 5: Calculate i=i+1 and go to step 4

Step 6: Display f

Step 7: Stop

4.Algorithm to calculate and display the multiplication table of a number

Step1: Start

Step 2: Input a number as n

Step 3: Set i=1

Step 4: Check, is i <=10?

4.1 If yes, Calculate and display n*i and goto step 5

3.2 If no, goto step 6

Step 5: Calculate i=i+1 and go to step 4

Step 6: Stop

5.Algorithm to display Fibonacci series having ‘n’ terms

Step 1: Start

Step 2: Set a=0,b=1, i=0

Step 3: Ask the value of ‘n’

Step 4: Is i<n?

4.1 If yes, calculate c=a+b and display “a” . Also set a=b , b=c 

4.2 If no, go to step 6

Step 5: Calculate i=i+1 and go to step 4

Step 6: Stop

6.Algorithm to check if a given number is palindrome or not palindrome

Step 1: Start

Step 2: Set sum=0 and ask to enter any number as n

Step 3: Store a=n

Step 4: Is n !=0?

4.1 If yes, calculate r=n%10 , sum=sum*10+r, n=n\10

4.2 If no, go to step 5

Step 5: Is sum = = a?

5.1 If yes, display “ Palindrome number ”

5.2 If no, display “ Not Palindrome number ”

Step 6: Stop

7.Algorithm to calculate sum of given series

Step 1: Start

Step 2: Set sum=0,i=1

Step 3: Ask the value of n

Step 4: Is i<=n?

4.1 If yes, calculate sum=sum+1/i and go to step 5

4.2 If no, go to step 6

Step 5: Calculate i=i+1 and go to step 4

Step 6: Display sum

Step 7: Stop

8.Algorithm to display given output:

1 2 3 4 5
2 4 6 8 10
3 6 9 12 15

Step 1: Start

Step 2: Set the outer for loop(i) from 1 to 3

Step 3: Set the inner for loop(j) from 1 to 5

Step 4: Calculate and display i*j

Step 5: Stop

9.Algorithm to check given number is prime or not

Step 1: Start

Step 2: Initialize I=1, count=0

Step 3: Input any number as N

Step 4: Is I<=N?

     4.1: If true, Calculate remainder(R) of num divided by I and check, IS R=0?

  If yes, calculate count=count+1 and go to step 5

     4.2: If false, go to step 6

Step 5: Calculate I=I+1 and go to step 4

Step 6: Is count equal to two.

      6.1: If true, Print "Prime"

      6.2: If false, Print "Composite"

Step 7: Stop

10.Step 1: Start

Step 2: Input any number as n

Step 3: Set i=1

Step 4: Is i<=n?

     4.1: If true, set count=0, Use inner loop(j)  as 1 to i

  Calculate remainder(R) of i divided by j and 

  check IS R=0?  If yes, calculate count=count+1 and go to step 5

     4.2: If false, go to step 6

Step 5: Calculate i=i+1 and go to step 4

Step 6: Is count equal to two.

      6.1: If true, Print "Prime"

      6.2: If false, go to step 7

Step 7: Stop

-----------------------------------------------------------------------------------------------

Algorithms related to array:

--------------------------------------

1.Algorithm to input marks of five subjects and display the total marks and average marks.

Ans:-
Algorithm:
step 1: start
step 2:Declare an array marks[5] of integer type
step 3:declare a variable i of integer type
step 4:set total=0
step 4:
        execute the loop from i=0 to 4
        input marks[i]
        total=total+marks[i]
        end of loop
step 5:
        let average=total/5
step 6: print total,average
step 7:stop

2.Algorithm to read the age of 100 persons and count the number of persons in the age group between 50 to 60 years.

Ans:-

step 1: start
step 2:Declare an array age[100]
step 3:declare a variable i of integer type
step 4:set total_age=0
step 5:
        execute the loop from i=0 to 100
        input age[i]
            if age[i]>=50 and age[i]<=60
               total_age=total_age+1
            end of if
        end of loop

step 6: print total_age
step 7:stop
3.Algorithm to input 'n' numbers and display the sum of even and odd numbers respectively.
Ans:-

step 1: start
step 2:Declare an array numbers[100]
step 3:declare a variable i,size_array of integer type
step 4:set sum_even=0,sum_odd=0
step 5:input size_array
step 6:
        execute the loop from i=0 to size_array-1
        input numbers[i]
            let remainder=numbers[i]/2
                if remainder=0               
                    sum_even=sum_even+numbers[i]
                else
                    sum_odd=sum_odd+numbers[i]
                end of if
        end of loop

step 7: print sum_even,sum_odd
step 8:stop
4.Algorithm to input 'n' numbers and find out the greatest, smallest and average number.
Ans:-

step 1: start
step 2:Declare an array numbers[100]
step 3:declare a variable i,size_array,average
step 4:set greatest=0,smallest=0,sum=0
step 5:input size_array
step 6:
        execute the loop from i=0 to size_array-1
            input numbers[i]  
            sum=sum+numbers[i] 
        end of loop
step 7: let greatest=numbers[0],smallest=numbers[0]
step 8:
        execute the loop from i=0 to size_array-1
            if greatest< numbers[i]  
               greatest=numbers[i]
            else if smallest> numbers[i]  
               smallest=numbers[i]
            end of if
        end of loop
step 9: average=sum/size_array
step 10:print greatest,smallest,average
step 11:stop
5.Algorithm that takes salary of 100 employees and print the salary of the employees in ascending order.
Ans:-
step 1: start
step 2:Declare an array salary[100]
step 3:declare a variable i,j,k of integer type

step 4:
        execute the loop from i=0 to 99
            input salary[i]
        end of loop

step 5:        
        execute the loop from i=0 to 99
            execute the loop from j=i+1 to 99
                    if salary[i]>salary[j]
                        k=salary[i]
                        salary[i]=salary[j]
                        salary[j]=k
                    end of if
            end of loop
        end of loop
step 6:
        execute the loop from i=0 to 99
            print salary[i]
        end of loop
step 7:stop
6.Algorithm to transpose the mxn matrix.
Ans:-

step 1: start
step 2:Declare an array matrix[100][100]
step 3:declare variables row,col
step 4:declare variables i,j of integer type
step 5:input row,col
step 6:
        execute the loop from i=0 to row-1
            execute the loop from j=0 to col-1            
                input matrix[i][j]
            end of loop
        end of loop

step 7:        
         execute the loop from i=0 to col-1
            execute the loop from j=0 to row-1            
                print matrix[j][i]
            end of loop
        end of loop
step 8:stop
7.Algorithm to  calculate the sum of diagonal matrix.
Ans:-

step 1: start
step 2:Declare an array matrix[100][100]
step 3:declare variables row,col
step 4:declare variables i,j of integer type
step 5:set sum=0
step 6:input row,col
step 7:
   if row=col   
        print the addition of diagonal elements is possible
        print enter the elements
         execute the loop from i=0 to row-1
                execute the loop from j=0 to col-1            
                    input matrix[i][j]
                        if i=j
                            sum=sum+matrix[i][j]
                        endif
                end of loop
         end of loop
    else
        print addition of diagonal elements for given matrix can not be done
    endif
step 8:        
        print sum
step 9:stop
8.Algorithm to add two matrices supplying by elements by the user.
Ans:-

step 1: start
step 2:Declare an array matrix1[100][100],matrix2[100][100]
step 3:declare variables row,col
step 4:declare variables i,j of integer type
step 5:input row,col
step 6:
       print enter elements of first matrix
         execute the loop from i=0 to row-1
            execute the loop from j=0 to col-1            
                input matrix1[i][j]
            end of loop
        end of loop
step:7
      print enter elements of second matrix  
        execute the loop from i=0 to row-1
            execute the loop from j=0 to col-1            
                input matrix2[i][j]
            end of loop
        end of loop
step 8:        
         execute the loop from i=0 to col-1
            execute the loop from j=0 to row-1            
                print matrix1[j][i]+matrix2[j][i]
            end of loop
                print goto next line
        end of loop
step 9:stop
------------------------------------------------------------------------------------------------------------------------------------------
Algorithms related to string:
--------------------------------------------------------------
1.Algorithm to find the length of string without using built in function.
ans:-
step 1: start
step 2:Declare a string variable st[100],integer variable i,length
step 3:set length=0
step 4:Input a string for variable st
step 5:
        execute the loop from i=0 to \0 for given string st
            length=length+1
step 6:print length
step 7:stop
2.Algorithm to reverse the given string without using built in function.
ans:-
step 1: start
step 2:Declare a string variable st[100],integer variable i,length
step 3:set length=0
step 4:Input a string for variable st
step 5:
        execute the loop from i=0 to \0 for given string st
            length=length+1
step 6:
         execute the loop from i=length to 0 for given string st
        print st with character
step 7:stop
3.Algorithm to concatenate two given string without using built in function.
ans:-
step 1: start
step 2:Declare two strings variable st[100],st1[100],
step 3:Declare integer variable i,length
step 4:set length=0
step 5:Input strings for variable st,st1
step 6:
        execute the loop from i=0 to \0 for given string st
            length=length+1
step 7:
         execute the loop from i= 0 to \0 for given string st1
          let,st[length]=st1[i]
          let ,length=length+1  
step 8: add the null character \0 at the end of st variable
step 9:print st
step 10:stop
4.
a)Algorithm to find length of string using strlen()
step 1: start
step 2:Declare a string variable st[100],integer variable i,length
step 3:Input a string for variable st
step 4:Find the length using strlen() for string st and store into length   
step 5:print length
step 6:stop
b)Algorithm to find reverse of string using strrev()
step 1: start
step 2:Declare a string variable st[100]
step 3:Input a string for variable st
step 4:reverse the string using strrev() for string st
step 5:print st
step 6:stop
c)Algorithm to know about strcmp():
Algorithm:-
step 1: start
step 2:Declare two strings variable st[100],st1[100]
step 3:Input string for variables st and st1
step 4: compare both strings st and st1 using strcmp() and store its value in k
step 5:
        if k>0
                print st has more ASCII value than st1
        else if k<0
                print st1 has more ASCII value than st
        else
                print both have same value
step 6:stop\
d)Algorithm to know about strcpy()
step 1: start
step 2:Declare strings variable st[100],st1[100]
step 3:Input a string for variable st
step 4:copy the the string st to st1 using strcpy(st1,st)
step 5:print st1
step 6:stop
e)Algorithm to know about strcat()
step 1: start
step 2:Declare strings variable st[100],st1[100]
step 3:Input strings for variable st st1
step 4:merge the the string st with st1 using strcat(st1,st)
step 5:print st1
step 6:stop
5.Algorithm to convert the given line of text into upper case.
ans:-
step 1: start
step 2:Declare strings variable st[100]
step 3:Input a string for variable st
step 4:convert the string into uppercase using strupr(st)
step 5:print st
step 6:stop
6.Algorithm to input line of text and count the number of digit, vowels, consonants, white spaces and other characters.
ans:-

step 1: start
step 2:Declare a string variable st[100],integer variable i
step 3:set total_digit=0,total_vowels=0, total_consonants=0, total_white_spaces=0,total_other=0
step 4:Input a string for variable st
step 5:
        execute the loop from i=0 to \0 for given string st
            if st[i]>0 and st[i]<=9
                total_digit=total_digit+1
            elseif (st[i]>=65 and st[i]<=90) || (st[i]>=97 and st[i]<=122)
                    if st[i]=a or st[i]=e or st[i]=i or st[i]=o or st[i]=u
                            total_vowels=total_vowels+1
                    else
                            total_consonants=total_consonants+1
            elseif st[i]=' '
                    total_white_spaces=total_white_spaces+1
            else
                  total_other=total_other+1
     end of loop  
step 6:print  total_digit,total_vowels,total_consonants, total_white_spaces, total_other
step 7:stop
7.Algorithm to swap two given strings.
ans:-

step 1: start
step 2:Declare strings variable st[100],st1[100] and t[100]
step 3:Input strings for variable st, st1
step 4:print st,st1
step 5: copy the string st to t variable
step 6:copy the st1 to st variable
step 7:copy the t to st1 variable
step 8:print st,st1
step 9:stop
8.Algorithm to check whether the given string is palindrome or not.
ans:-

step 1: start
step 2:Declare strings variable st[100] and t[100]
step 3:Input strings for variable st
step 4: copy the string st to t variable
step 5:reverse the st1
step 6:compare both the strings t and st1 and store its value in k
step 7:
        if k=0
                print it is palindrome
        else
                print it is not palindrome
step 8:stop
9.Algorithm to input the names of 10 students and sort them in alphabetical order. 
ans:-

step 1: start
step 2:Declare string variables st[100][100], t[100],integer variable i,j
step 3:
          execute the loop from i=0 to 9
                input string st[i]
        end of loop   
step 4:
         execute the loop from i=0 to 9
                 execute the loop from j=i+1 to 9
                            compare strings st for location i and j and store the value  k
                                if k>0
                                      copy the string st for location i to t variable
                                       copy the string st for location j to  st for location i
                                        copy the string for t to st for location j
                                end of if
                end of loop
        end of loop
step 5:
          execute the loop from i=0 to 9
                print string st[i]
        end of loop 
step 6: stop
10.Algorithm to  search a string from the given set of strings
ans:-
step 1: start
step 2:Declare string variables st[100][100], t[100],integer variable i,total,flag=0
step 3:input total
step 4:
          execute the loop from i=0 to total-1
                input string st[i]
        end of loop   
step 5:input t(to be searched)
step 6:
         execute the loop from i=0 to total-1
                 
                            compare strings st for location i with string stored in t and store the value in k
                                if k=0
                                     flag=1
                                                exit the loop execution/stop
                                end of if
          end of loop
     
step 7:
          if flag=1
                print string found in location i
            else
                print string not found
            endif
step 8: stop