Friday 30 October 2015

STRUCTURED PROGRAMMING


  • Designing and solving a programming problem using sequence, selection and iteration control instructions are called as structured programming
  • It employs a top-down design model, in which the program is divided into modules 

Principles of Structured Programming


  • At all times and under all circumstances, the programmer must keep the program within his intellectual grasp 
  • The well known methods for achieving this : 
    • Top-down design and construction 
    • Limited control structures 
    • Limited scope of data structures 
  • Major elements of structured programming 
    • Sequence control structure 
    • Selection control structure 
    • Iteration control structure 

Rules of Structured Programming


  • Code no more than one statement per line. 
  • Divide programs into functions. 
  • Functions should perform only one task. 
  • Variable names should be meaningful. 
  • Use of global variables should be avoided. 
  • All variables should be explicitly declared and given an initial value. 
  • Hierarchical data structures should be used to keep the data and program structured. 
  • Each function must have an initial comment 
  • Compound conditional statements should be limited to avoid confusion. 
  • Indentation should be consistent. 
  • When used in functions, the use of { and } should be on their own lines to demark blocks of code. 
  • Upper case should be used for defined static constants. 
  • For all keywords and variables, use lower case. 

Advantages of Structured Programming


  • Easy to write 
  • Easy to debug 
  • Easy to understand 
  • Easy to change

Tuesday 27 October 2015

BASIC CONTROL STRUCTURE

  • Control structures are the basic entities of a structured programming language
  • The mechanism that allow us to control the flow of execution are called control structures. 
  • There are 4 main categories of control structures :
    • Sequence
    • Selection
    • Iteration
    • Branching

SEQUENCE



  • Do one instruction the the next and the next
  • It just executes them in the given sequence or in order listed
  • Most lines of the code in the program belongs to this category.

e.g :
statement 1 is executed first, followed by statement 2, then finally statement 3.


SELECTION


        
  • Selection structures are used to perform ‘decision making‘ and then branch the program flow based on the outcome of decision making
  • Implemented in C/C++ with If, If Else and Switch statements
  • If and If Else statements are 2 way branching statements 
  • Switch is a multi branching statement.
e.g : If - then - else
if (condition) then
   Statement 1;
else
   Statement 2;
  • Condition is a boolean expression 
  • Evaluates either True / False
  • If True - Statement 1 will be executed
  • Otherwise - Statement 2 will be executed
  • S1 & S2 may be single statement or group of statements


ITERATION


  • Iterative construct means that some statements will be executed multiple times until some condition is met
  • Such construct implements a loop structure in which action is executed multiple times, as long as some condition is true 
  • In C, iterative constructs can be implemented using while, do-while, or for loop statements
  • While is an entry controlled loop. Statement inside braces are allowed to execute only if condition inside while is TRUE.
  • Do while is an exit controlled loop.
  • For statement is an entry controlled loop. 
  • The difference between while and for is in the number of repetitions.
  • The for loop is used when an action is to be executed for a predefined number of times. 
  • The while loop is used when the number of repetitions is not predefined.

e.g :


BRANCHING


  • A control structure that allows the flow execution to jump to a different part of the program.
  • This category is rarely used in modular structured programming.

e.g : Go - To Statement


  • It provides an unconditional jump from the 'goto' to a labelled statement in the same function.
  • Use of go-to statement is highly discouraged in any programming language because it makes difficult to trace the control flow of a program, making the program hard to understand and hard to modify. 
  • Any program that uses a go-to can be rewritten to avoid them.


Saturday 24 October 2015

PRACTICE WITH FLOWCHARTS

  1. Draw a flowchart for making a telephone call
  2. Draw a flowchart to calculate C=A*B
  3. Draw a flowchart to find out the smallest of 2 numbers
  4. Draw a flowchart for adding integers from 1 to 100
  5. Draw a flowchart to reverse a given number
  6. Draw a flowchart to find out the largest of 3 numbers
  7. Draw a flowchart to find the factorial of a number
  8. Draw a flowchart to calculate the simple interest
  9. Draw a flowchart to find the roots of a quadratic equation
  10. Draw a flowchart to find FCF of 2 numbers
  11. Draw a flowchart to print each 3 digit even number and its square(i.e., from 100 to 998)
  12. Draw a flowchart to find all integer divisors of a given number
  13. Draw a flowchart for preparing a bill of sales taking item, quantity as its input.
  14. Draw a flowchart to find the area of a triangle for the given 3 sides of triangle.  




PROGRAM DESIGN TOOL - FLOWCHARTS

  • A flowchart is a symbolic representation of the step by step solution of a given problem.
  • It indicates the flow of the entire process, the sequence of data input, operations, computations, decisions, results and other relevant information pertaining to a particular problem.
  • 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.
  • There are 2 types of flowcharts
    • System flowcharts - showing controls at a physical or resource level
    • Program flowchart - showing the controls in a program within a system

FLOWCHART SYMBOLS


ADVANTAGES OF A FLOWCHART



  • Communication - Flowcharts are better way of communicating the logic of a system to all concerned or involved.
  • Effective analysis - With the help of flowchart, problem can be analysed in more effective way therefore reducing cost and wastage of time.
  • Proper documentation - Program flowcharts serve as a good program documentation, which is needed for various purposes, making things more efficient.
  • Efficient Coding - The flowcharts act as a guide or blueprint during the systems analysis and program development phase.
  • Proper Debugging - The flowchart helps in debugging process.
  • Efficient Program Maintenance - The maintenance of operating program becomes easy with the help of flowchart. It helps the programmer to put efforts more efficiently on that part

DISADVANTAGES OF A FLOWCHART


  • Complex logic - Sometimes, the program logic is quite complicated. In that case, flowchart becomes complex and clumsy. This will become a pain for the user, resulting in a waste of time and money trying to correct the problem
  • Alterations and Modifications - If alterations are required the flowchart may require re-drawing completely. This will usually waste valuable time.
  • Reproduction - As the flowchart symbols cannot be typed, reproduction of flowchart becomes a problem.








Wednesday 21 October 2015

PRACTICE WITH ALGORITHMS

  1. Write an algorithm for making a telephone call
  2. Write an algorithm for the average of 3 numbers
  3. Write an algorithm to find out the discriminant of a quadratic equation
  4. Write an algorithm for preparing a bill of sales
  5. Write an algorithm to find the sum of all natural numbers between 1 and n
  6. Write an algorithm to find the largest of 3 integers
  7. Write an algorithm to find simple interest
  8. Write an algorithm to find greatest of 2 numbers
  9. Write an algorithm to find the greatest of 3 numbers
  10. Write an algorithm to find the factorial of a number
  11. Write an algorithm to find the LCM of 2 numbers
  12. Write an algorithm to reverse a number
  13. Write an algorithm to find all integer divisors of a given number
  14. Given 3 numbers a, b, a the sides of a triangle. Check whether with the 3 sides given, it forms on Isosceles or equilateral Isosceles triangle


PROGRAM DESIGN TOOL - ALGORITHM

  • An algorithm is a Characteristics of an algorithm
    • Well defined set of steps for solving a problem.
    • Logical concise list of procedures for solving a problem
    • Complete unambiguous finite number of logical steps for solving any problem on the computer

  • Characteristics of an algorithm 

    • Precision  -  the steps are precisely defined.
    • Uniqueness - results of each step are uniquely defined and only depend on the input and the result of the preceding steps.
    • Finiteness - the algorithm stops after a finite number of instructions are executed.
    • Input - the algorithm receives input.
    • Output - the algorithm produces output.
    • Generality - the algorithm applies to a set of inputs.

  • Advantages of an algorithm

    • Reduced complexity. 
    • Increased flexibility. 
    • Ease of understanding

  • Disadvantages of an algorithm

    • Multiple function calls are expensive.
    • Inefficient when there is lots of branching from one state.
    • Requires large amount of space as the each function state needs to be stored on system stack.

Sunday 18 October 2015

UNIT 1 : INTRODUCTION

COMPUTATION

  • The term Computation refers to the process of arriving at output from a given problem. 
  • It should be logical and the process should follow a step by step approach to arrive at a meaningful output or result. 
  • To achieve this, the basic computation model should be followed. 

STEPS IN PROBLEM SOLVING   

  • The steps in problem solving are called as Program Development Life Cycle (PDLC).
  • It is shown in the below diagram.


 


  • Problem Definition

    • Identify & define the problem
    • It must be stated clearly, accurately & precisely
    • It should not be ambiguous

  • Problem Analysis 

    • The following should be stated clearly 
      • Input specification
      • Output specification
      • The processing steps required to convert given inputs to outputs.

  • Designing A Problem

    • Designing the solution for the problem 
    • Obtained by using the program design tools 
      • Algorithms - step by step procedure of solving a problem 
      • Flowcharts - graphical representation of the algorithm. 
    • The following ways are also followed 
      • Top-down design approach 
        • The problem is divided into sub-problems and further dividing them into smaller sub-problems until it is small enough to solve the problem individually. 
        • Allows the programmer to build a solution in a step-wise manner 
      • Bottom-up design 
      • Modular design

  • Coding

    • Writing the instructions in a particular language to solve a problem 
    • Data items, variables & constants are defined

  • Testing A Program

    • Testing is done to verify the quality of the program (completeness, correctness, reliability & maintainability)
    • Testing can be done at the following levels
      • Unit testing
      • Program testing
      • Verification testing
      • Validation testing

  • Maintaining A Program

    • Modifications based on user requirements
    • Periodic review of the programs & modifications based on user requirements
    • Requires proper documentation
    • Its a continuous activity