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

Validation checks:

 The following checks may be carried out:
1.      Presence Check à the program checks that all items of data are present. For example, the customer account number could not be omitted on a sales order.
2.      Character Count à some times such as a customer code or product code are of fixed length.
3.      Format Check à this ensures that certain fields are in the same format.
4.      Range check à this checks that a data (field) lays within a particular range.
5.      Check Digit àan additional digit used to validate the accuracy of large numeric data.
6.      File Lookup à if the validation program has access to master files, then input data can be validated by reference to those files. For example a stock file could be looked up to verify that a given product code exists.
7.      Batch Header Checks a computers will calculate any control totals, which have been manually calculated, and the totals compared. This will include the total number of records in the batch, and any hash totals that have been calculated.

Database Management System (DBMS)


The Data Base Management System (DBMS) is an application program that provides an interface between the operating system and the user in order to make access to the data as simple as possible. It has several other functions as well, and these are descried below.
1.      Data Storage, retrieval and update: The DBMS must allow users to store retrieve and update information as easily as possible, without having to be aware of the internal structure of the database.
2.      Creation and maintenance of the data directory.
3.      Managing the facilities for sharing the database: The DBMS has to ensure that problems do not arise when two people simultaneously access a record and try to update it.
4.      Backup and recovery. The DBMS must provide the ability to recover the database in the event of system failure.
5.      Security. The DBMS must handle password allocation and checking, and the ‘view’ of the database that a given user is allowed.

Difference between WHILE …DO & REPEAT…….UNTIL Loop Structure


  WHILE …DO                

1.   The condition is tested before the body is executed.
2.   It is possible that the body may never be executed.(this occurs if the condition is true on the first test)
3.   The loop is exited when the condition is false.

REPEAT…….UNTIL
1.      The condition is tested after the body is executed.
2.      The body is always executed at least once.
3.      The loop is exited when the condition is true.


Procedures and modules:


The program can thus be broken down into modules; and the preferred way of writing a program is to code each of these tasks as a separate procedure. The main program will then reflect the top-level solution and call each of these procedures in turn
Points to remember:
  • Each procedure is given a name, and has the same structure as a program.
  • The procedure is called from the main procedure simply by writing its name.
  • Any procedure that is called must be written before it is called; that is, it must be placed physically above the statements in the main program.