Total Pageviews

February 15, 2012

How to Write Algorithms?


Things to remember………..
 
Read the question properly and identify major tasks to be performed.
Use four step method to develop up a sketch for algorithm.
If wont able to handle the constructs in it. Just write in English language 
statement, but logically.
 
Four steps method
 
1.      Initializing variables.
2.      Defining the process involved in the situation e.g. what construct is require in it  repeat….until, if …then…else, while…..do.
3.      Do the calculations involved in algorithm.
4.      Display the output.
* Don’t forget to check its worth through Dry run
 

Example 1
 
Q. A credit card company keeps its customer accounts details in a master file. When a customer wants to purchase an item in a shop the customer card is swiped through a card reader. The company's computer first checks to see if the card is stolen. If it is not stolen the computer checks to see if the amount of the purchase is less than or equal to the amount of money that the customer is allowed to spend. If so on authorization code is sent to the shop and the purchase is completed, otherwise the request is cancelled. Design an algorithm to achieve this
 
High lighted points


1-if the card is stolen.


2-if the amount of the purchase is less than or equal to the amount of  money that the customer is allowed to spend


3- Authorization code is sent to the shop


4- Otherwise the request is cancelled
 
Algorithm:-
 
Read Credit card number
If Card is stolen then
Print “sorry card is stolen”
Else
               get user balance
                               If amount purchased<= balance or Allow money
                                              Send Authorization code to shop
                               Else
                                              Print”request Cancelled”
                               End if
End if
 
Example 2
 
Example2

A user wishes to withdraw a money from a cash dispensing machine, sometimes known as an ATM. Write an Algorithm for the processing done by the system.
 
*Note here your knowledge about using ATM machine is major aspect. Convert it in step and algorithm will be generated.
 
Check card validity (or expiry)
Get pin
Display option to enter pin code
               If pin is valid then
                               Access user account and information
                               Ask for amount to withdraw through menu
                                              If amount entered is <= balance then
                                                            Update user account and provide cash
                                                            Print receipt
                                              Else
                                                             Print “sorry its not in your limit”
                                              End if
               Else
               Print “sorry invalid pin code”
               End if

2 comments:

  1. please explain the algorithms in detail and also give example with assigning variables.

    ReplyDelete
  2. Please expand & explain with more methodes

    ReplyDelete