Wednesday, November 19, 2014

pseudo codes

pseudo codes

                           pseudo code is structured English like way of representing the solution to a problem. it is considered a first draft because pseudo code is like English and has some precision to it , it does not have the very definite precision of a programming language. a computer cannot execute pseudo code. it can be translated in to a variety of programming language

data declaration 


            it is important to define all data used by a program within the pseudo code as the following is one of the fundamental concepts in programming. 
                    program = data + algorithm (logic)
           there are 3 main things to con ceder when declaring data in programs 
       1. data type   - specify the type of value that can be stored.
          2. data usage - whether the data item is used as a variable or a constant
          3. data scope - specify the limit of access within the program


                data type 

1.simple data
       data items which can store single values
                        name : string
                        age    : integer
                        marks: real
2.complex data
      data item which allows you to store collection of values.
                       class marks (10): array of integer
                       student data : record of
                       name            : string
                       age               : integer

               data usage 


     1.variables
                variables are data items of any type whose contents may change in value as the programs executes.
            counter = counter+1
            massage = "hello world"

     2.constants 
               constants are either literal values or data items whose contents do not change as the program executes.
           counter = counter+1
           massage = "hello world"
           pi : real values 3.524

                        data scope 

   1.global 
           data which is accessible by the procedure in which it is defined and by all procedures directly subordinate to that procedure


   2.local
           data which is accessible only by the procedure in which it is defined. 

                   data declaration within a program
      global data:
      pi : real value3.524
      local data:
      counter,total : integer


constructs 
1. sequence
      a saris of instructions executed one after the other
e.g;
     input number of items purchased   
       input price per item
       total cost = number of items purchased * price per item
       display total cost

2. selection
      a selection control structure is the presentation of a condition and the choice between two actions the choice depending on whether the condition is true or false.

e.g;
      IF condition THEN
                command sequence 1
        ELSE
                command sequence 2
        END IF
  
      IF average >50 THEN
                display "pass"
         ELSE
                display "fail"
        END IF
                                                 if the choice is between more than two alternatives then we need a multiple selector.

       CASE statement
                 1 : command sequence 1
                 2 : command sequence 2
      ELSE
                 default command sequence
      END CASE

e.g:
      IF average >= 80 THEN
                              grade :="A"
                          ELSE IF average >= 65 THEN
                                                    grade ="B"
                                               IF average >=50 THEN
                                                                 grade ="C"
                                                                ELSE IF average >=35
                                                                      grade ="S"
                                                                ELSE
                                                                     grade ="F"
                                                            END IF
                                                END IF
                                END IF
                 END IF

3. repetition 
          the repetition control structure can be defined as the presentation of instruction to be performed rapidly as long as the condition is true.
     while loop
              WHILE condition P is true DO 
                           statement block
                    END DO
     repeat / until loop
             REPEAT
                          statement block
                  UNTIL condition P is false 
     for loop
            FOR x = to 10 DO
                          statement block
                 END DO


procedure / function 

      procedure: a selection of program that carries out some well defined operation on data specify by parameter. it can be called from any ware in the program and different parameters can be provided for each call.
      function: the different between a procedure and the function is that the function is that the function will return a single value after called where as a procedure do a particular task but not necessarily return a value.

            important points regarding function / procedures
       
                                 to return a value from a function it should be assigned to the function name. each module can defined its own data. the data item passed into the function at the time of call are called parameters. the parameters may be input or output based on their role. input parameters only carry value into the module. output parameters take a result back to the caller.

No comments:

Post a Comment