session1 - basic programming concepts

Upload: narayannppp

Post on 30-May-2018

228 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/14/2019 Session1 - Basic Programming Concepts

    1/23

    Basic Programming Concepts

    Session 1

  • 8/14/2019 Session1 - Basic Programming Concepts

    2/23

    Basic Programming Concepts / 2 of 23

    Session Objectives Discuss algorithms

    Draw flowcharts

    List the symbols used in flowcharts

    Discuss variables

    List the different data types and make use of them in your own Cprograms

    List and explain the different methods used for testing an algorithm

    Test a program using dry run method Understand the structure of a sample C program

    Identify the types of errors that can occur

  • 8/14/2019 Session1 - Basic Programming Concepts

    3/23

    Basic Programming Concepts / 3 of 23

    Basic Approach to Writing a

    Program C# include

    void main()

    {

    char name[15];

    scanf(Enter name : %s, &name);

    printf(Hello %S, name);

    }

    10 Input name$

    20 Print Hello; name$30 END

    BASIC

    JAVAClass Hello

    {public static void main(String args[])

    {

    String name;

    System.in.readln(name);

    System.out.println(hello + name);

    }

    }

    Basic approach to programming

    has remained the same inspite of

    variations in languages. The

    three codes perform the same

    task.

  • 8/14/2019 Session1 - Basic Programming Concepts

    4/23

    Basic Programming Concepts / 4 of 23

    The Programming Approach to

    Solving ProblemsClassroom

    Leaving the

    classroomHead towards

    the staircase

    Go to the

    basement

    Head for the

    cafeteria

    Cafeteria

    Algorithm is a set of steps thatare performed to solve aproblem. The example belowdescribes an algorithm

    These are the steps followed when a

    student wants to go to the cafeteria

    from the classroom.

  • 8/14/2019 Session1 - Basic Programming Concepts

    5/23

    Basic Programming Concepts / 5 of 23

    Solving a ProblemIn order to solve a problem

    Understand the problem clearly

    Gather the relevant information

    Process the information

    Arrive at the solution

  • 8/14/2019 Session1 - Basic Programming Concepts

    6/23

    Basic Programming Concepts / 6 of 23

    PseudocodeIt is not actual code. A method of algorithm - writing which uses

    a standard set of words which makes it resemble code.

    Each pseudocode starts with a BEGIN

    To show some value , the word DISPLAY is used

    The pseudocode finishes with an END

    BEGIN

    DISPLAY Hello World !

    END

  • 8/14/2019 Session1 - Basic Programming Concepts

    7/23Basic Programming Concepts / 7 of 23

    FlowchartsIt is a graphical representation of an algorithm

    START

    DISPLAY Hello World !

    STOP

  • 8/14/2019 Session1 - Basic Programming Concepts

    8/23Basic Programming Concepts / 8 of 23

    The Flowchart SymbolStart or End of the program

    Computational Steps

    Input/Output Instructions

    Decision making

    & Branching

    Flow Line

    Connectors

  • 8/14/2019 Session1 - Basic Programming Concepts

    9/23Basic Programming Concepts / 9 of 23

    Variables

    Data

    Memory

    Each location in the memory is unique

    Variables allow to provide a meaningful name for the location in memory

    15

    Data in memory

    15

  • 8/14/2019 Session1 - Basic Programming Concepts

    10/23Basic Programming Concepts / 10 of 23

    An Example

    A, B and C are variables in the pseudocode

    BEGIN

    DISPlAY Enter 2 numbers

    INPUT A, B

    C = A + BDISPLAY C

    END

    Variable names takes away the need for a programmer to access

    memory locations using their address

    The operating system takes care of allocating space for the variables

    To refer to the value in the memory space, we need to only use the variablename.

  • 8/14/2019 Session1 - Basic Programming Concepts

    11/23Basic Programming Concepts / 11 of 23

    Guidelines to be followed while

    Naming VariablesVariable names should begin with an alphabet

    Proper names should be avoided while naming variables

    The first character can be followed by alphanumeric characters

    A variable name should be meaningful and descriptive

    Confusing letters should be avoided

    Some standard variable naming convention should be followed

    while programming

  • 8/14/2019 Session1 - Basic Programming Concepts

    12/23Basic Programming Concepts / 12 of 23

    Data Types

    Datatype variableName

    A data type describes the kind of data that will fit into a

    variableThe name of the variable is preceded with the data type

    For example, the data type int would precede the namevarName

    int varName

  • 8/14/2019 Session1 - Basic Programming Concepts

    13/23Basic Programming Concepts / 13 of 23

    booleanchardouble

    Basic Data TypesThe basic data types are

    floatint

  • 8/14/2019 Session1 - Basic Programming Concepts

    14/23Basic Programming Concepts / 14 of 23

    Derived Data Types

    intshortshort int

    (Occupies less

    memory placespace than int)

    Derived data typeBasic Data

    types

    Data type

    Modifiers

    int unsigned int(Permits onlypositive numbers)

    unsigned

    int/doublelong/longdouble

    (Occupies more

    space than

    int/double)

    long

  • 8/14/2019 Session1 - Basic Programming Concepts

    15/23

    Basic Programming Concepts / 15 of 23

    Testing the AlgorithmThe three types of testing are

    Dry Run

    Independent

    Inspection

    Structured

    Walkthrough

  • 8/14/2019 Session1 - Basic Programming Concepts

    16/23

    Basic Programming Concepts / 16 of 23

    Dry RunManual way of testing an algorithm for correctness

    The values of the variables are tracked through every line of

    the code

    A table is first created, with as many number of columns as

    the number of variables

    The names of the variables are written in the column headers

    The values of variables are noted down in the table at every stage

    The final result is displayed at the end of the execution of the

    code

  • 8/14/2019 Session1 - Basic Programming Concepts

    17/23

    Basic Programming Concepts / 17 of 23

    An Example of Dry RunStep 1. Start

    Step 2. X = 10

    Step 3. Y = 5

    Step 4. M = 0

    Step 5. M = X+Y+(X*Y)

    Step 6. Y = Y+4

    Step 7. M = M+Y

    Step 8. Display X,Y,M

    Step 9. End

    X Y M

    Initial values 10 5 0

    After Step 5 10 5 65

    After step 6 10 9 65

    After step 7 10 9 74

  • 8/14/2019 Session1 - Basic Programming Concepts

    18/23

    Basic Programming Concepts / 18 of 23

    Independent InspectionAlgorithm is given to

    Person 2

    Person 1 Person 2 (not worked on the logic)

    Returns the algorithm

    to person 1 after using dry run

    for testing

  • 8/14/2019 Session1 - Basic Programming Concepts

    19/23

    Basic Programming Concepts / 19 of 23

    Structured Walkthrough

    Algorithm is

    passed on

    Person 1

    Peers

    The algorithm is accepted totally,

    accepted after minor changes or

    rejected totally

    Peers will give

    suggestions and

    will suggest errors

    or inefficiencies

  • 8/14/2019 Session1 - Basic Programming Concepts

    20/23

    Basic Programming Concepts / 20 of 23

    A Sample C Program# include

    void main()

    {int a, b,c;

    printf(Enter the first number : );

    scanf(%d, &a);

    c=a+b;

    }

    printf(The sum of the numbers is %d, c);

    Program working begins here

    printf(Enter the second number : );

    scanf(%d, &b);

    Variables declared

    Accepting input

    Displaying number

  • 8/14/2019 Session1 - Basic Programming Concepts

    21/23

    Basic Programming Concepts / 21 of 23

    ErrorsAn error is a deviation of a program from the result required

    The three types of errors are:

    Logical errors

    Syntax errors

    Run time errors

  • 8/14/2019 Session1 - Basic Programming Concepts

    22/23

    Basic Programming Concepts / 22 of 23

    Other important programming

    concepts I

    Source CodeO/SCOMPILER

    Object CodeSource Code

    Compiler converts source code into object code, that is understood and

    executed by O/S.

  • 8/14/2019 Session1 - Basic Programming Concepts

    23/23

    Basic Programming Concepts / 23 of 23

    Other important programming

    concepts II

    Source CodeO/SINTERPRETER

    Each instruction in the source code is the interpreter and passed on to

    the O/S , where it is executed.