Program Implementation ITnotes

  • Upload
    cee-em

  • View
    220

  • Download
    0

Embed Size (px)

Citation preview

  • 8/13/2019 Program Implementation ITnotes

    1/32

    Where are we?We looked at problem solving

    Writing algorithms

    Drawing flowchartsTrace table

    Now

    Use of a programming language to introduce theprocess of writing short programs.

  • 8/13/2019 Program Implementation ITnotes

    2/32

    Ms. Douglas and Mr. L:ucas

  • 8/13/2019 Program Implementation ITnotes

    3/32

    ProgrammingProgramming is the art of writing the solution to a

    problem using a language that a computer canunderstand.

    A programmer instructs the computer how to solve aproblem since it cannot come up with a solution all by

    itself. (The person that write programs)

  • 8/13/2019 Program Implementation ITnotes

    4/32

    Computer ProgramA computer program is a set of instructions that tells a

    computer what to do and how to do it.

    These instructions are converted into a sequence of

    numeric codes called machine codewhich is storedin memory.

    CPU interprets the code in order to carry out the

    instructions of the program

  • 8/13/2019 Program Implementation ITnotes

    5/32

    Types of Programming languageLow-level language

    High-level language

  • 8/13/2019 Program Implementation ITnotes

    6/32

    Low-level LanguageLanguage is machine dependent. Other words, codes

    written can only be understood by the particularcomputer or processor used to write the code.

    Types of low-level language:

    First generation language

    Second generation language

  • 8/13/2019 Program Implementation ITnotes

    7/32

    First generationlanguage

    Second generationlanguage

    It is called machine language. It usedthe digits 0 and 1 to make up binarycode.

    It is called assembly language. It hasthe same structure and commands asmachine language but instead ofusing binary to write codes it usedequivalent assembly language code.E.g. 10110000 01100001 : add A, B

    Advantage:Code runs very fast and efficientlybecause it is directly executed by theCPU

    Advantage:Can be easily converted to machinecode by a program called anassembler

    Disadvantage:Programmer may become confused

    with the massive amount of 0s and 1sin the program.It is machine dependent.

    Disadvantage:Still difficult to understand comparedto the high-level language.It is machine dependent.

  • 8/13/2019 Program Implementation ITnotes

    8/32

    High-Level LanguagesHigh level language are notmachine dependent.

    Therefore programs written on one computer can beused on another similar computer.

    Keywords used are similar to English and easier towrite

    Types of high-level language

    Third generation languages (3GLs)Fourth generation languages (4GLs)

    Fifth generation language (5GLs)

  • 8/13/2019 Program Implementation ITnotes

    9/32

    3GLs 4GLs 5GLsThese languages areconverted to machine

    code. E.g. FORTRAN,BASIC, C

    Non-procedural: programssuch as COBOL are written

    to provide easy ways ofdesigning screens andreports and using databases.They contain commands toread and process the dataand place the results in

    report-form on the page

    Very high level languages.5GLs are non-procedural

    languages meaning that theprogrammer states the goalto be achieved but not thesteps required in order toachieve the goal. E.g. Prolog

    Advantage:Can use English-typewords to write programcode, making it easier tocreate.

    Advantage:Useful for generating reports

    Advantage:Computers will be able tocommunicate in naturalspoken language with theirusers

    Disadvantage:Programsalso have to be convertedto machine language(binary).

    Disadvantage:Can become very wordy.

    Disadvantage:Very complex to design,programmer must be highlytrained.

  • 8/13/2019 Program Implementation ITnotes

    10/32

    Third Generation Language FORTRAN (Formula Translation) - First high-level language, used to

    express mathematical formulae, Scientific problem and engineerproblem.

    BASIC (Beginners All-purpose Symbolic Instruction Code) - Very easyto learn language that was creating teach programming.

    COBOL (Common Business Oriented Language) A language used incommercialsand business.

    PASCAL ( named after the 17thcentury Mathematician Blaise Pascal) use for teachingand alternative to BASIC

  • 8/13/2019 Program Implementation ITnotes

    11/32

    Third Generation Language C A language used mostly to write Operating

    Systems,Database Management Software andScientificApplication.

    C++ - An Object Oriented Programming Languagethat was originally an extension of C.

    PROLOG (Programming Logic) A language used todevelop artificial intelligence.

  • 8/13/2019 Program Implementation ITnotes

    12/32

    Review Questions1) Explain difference between low-level languages and

    high level languages.

    2) State the generations of computer languages.3) State the programming language that is associatedwith:

    a. Artificial intelligence applications

    b. Business applicationsc. Operating systems

    d. Scientific Problems

  • 8/13/2019 Program Implementation ITnotes

    13/32

    Answers1. Low-level languages are machine dependent and not

    easy to understand by humans whereas, high levellanguages must be converted to machine languageand are easier to understand by humans

    2. First, second, third, fourth, fifth

    3. Prolog; COBOL; C or C++; Fortran

  • 8/13/2019 Program Implementation ITnotes

    14/32

    The sequence of steps associated with

    implementing a program Computer program is developed through the following

    steps:1) Defining the problem

    2) Analyzing the problem

    3) Developing an algorithm or method

    4) Write the computer program which implements thealgorithm

    5) Testing and debugging the program6) Documenting the program

    7) Maintain the program

  • 8/13/2019 Program Implementation ITnotes

    15/32

    Defining the Problem Problem must be well defined so that there is no doubt

    as to what is the solution

    Must be clearly written so that it can be interpreted tohave only one meaning.

    Eg. - calculate the sum of two numbers

    - compute the average of a class- grade the scores of students in a class

  • 8/13/2019 Program Implementation ITnotes

    16/32

    Analyzing the problem The process of finding out the operation is analyzing

    Analyzing the problem involves:

    Interpreting and understanding the problemDetermine:

    input needs

    processing required

    output requiredcommands and programming

    constructs required

  • 8/13/2019 Program Implementation ITnotes

    17/32

    Developing AlgorithmAlgorithm: set of steps which when followed can lead

    to the solution of a problem

    Can be written using pseudo code

    Pseudo code is a language consisting of English like

    statement used to represent the steps in an algorithm.

    Flow Chart: a pictorial representation of an algorithm.

  • 8/13/2019 Program Implementation ITnotes

    18/32

    Programming Terms and Concept

    Source Code: original text of a program written in highlevel language

    The actual programming language statements is calledthe source code.

    Object Code: output from a compiler or assembler.

    The source code must be translated to machinelanguage.

    Machine language is the object code

  • 8/13/2019 Program Implementation ITnotes

    19/32

    Programming Terms and Concept

    Compile or interpret: mean to change a program ortranslate a program from high level language to machinecode. (translators)

    Two type of translator:

    Interpreter- translates or converts programmingcodes line by line

    Compliertranslates or converts all theprogramming codes at one time

  • 8/13/2019 Program Implementation ITnotes

    20/32

    Programming Terms Dry Run: manually checking through the steps in a

    program. Testing the program.

    Debugging: correcting errors in the source codes of aprogram.

    Logics: sequential order and comparison within the

    program.

    Logic Error: results that come from sequence instructions,faulty comparison and selection

  • 8/13/2019 Program Implementation ITnotes

    21/32

    Programming Terms

    Syntax is the very precise way in which the statement in aprogram must be written in order to be understand. Therefore , The language of the programming language is the syntax

    of the language.

    Syntax Error: results from not adhering to the rules of aprogramming language.

    Run Time Error: errors occur while the program compilesor runs.

  • 8/13/2019 Program Implementation ITnotes

    22/32

    Variable DeclarationVariable hold areas or spaces for data which a

    program might use or manipulate.Variables are givennames, which can assign values such asnumber andtext. For example, age: integer.

    Variable store values of a given type. Some data typeare:

    Integer to store integer or whole number. Real to store real or functional numbers.

    Character a single character such as a letter.

    String collection of character such as a word.

  • 8/13/2019 Program Implementation ITnotes

    23/32

    Variable DeclarationThis is how you declare variables in pseudo CodeInput Age, Cost, GradeAge= 23Cost 56.50Grade = A

    This is how you declare variables in PascalVarAge :=integer;Cost := real;Grade:=char; Age

    Assign Initial Values: Cost

    Age:= 23; GradeCost:=56.50;Grade:=A;

    56.50

    A

    23

  • 8/13/2019 Program Implementation ITnotes

    24/32

    Variable Declaration

    To declare a variable means to create the variable bygiving it a name and give it a data type (integer, realetc.

    This is how you declare constants in pseudo Code

    Input Price = 50, ItemName = cake

    This is how you declare variables in Pascal and assigninginitial values:

    Const

    Price :=50;

    Cost := Cake;

  • 8/13/2019 Program Implementation ITnotes

    25/32

  • 8/13/2019 Program Implementation ITnotes

    26/32

    control structures Conditional branching:

    if-then if-then-else,

    Program grade; Var grade :integer;

    Begin Writeln(Enter your information technology grade); Read(grade); If (grade >= 50)

    Then Writeln(Pass accepted for cxc)

    Else Writeln( failed, try harder next time)

    End

  • 8/13/2019 Program Implementation ITnotes

    27/32

    control structures Loops: While

    ProgramwhileLoop;

    Var a:integer;

    Begin a:= 40;

    While a 0 DO a=a-5;

    Writeln( The value of variable a is: , a);

    End

  • 8/13/2019 Program Implementation ITnotes

    28/32

    control structures Loops: Repeat

    ProgramrepeatLoop;

    Var a:integer;

    Begin a:= 40;

    Repeat a=a-5;

    Writeln( The value of variable a is: , a);

    Until a=0;

  • 8/13/2019 Program Implementation ITnotes

    29/32

    control structures Loops: For

    ProgramforLoop;

    Var a:integer;

    Begin a:= 40;

    For m=1 to 8 a=a-5;

    Writeln( The value of variable a is: , a);

    End;

  • 8/13/2019 Program Implementation ITnotes

    30/32

    Array Idimensional array

    Is a set of data of the same type grouped together andreferred to by a single name.

    23 33 56 41 79 38

    Position1

    Position2

    Position3

    P

    osition4

    Position5

    Position6

    AGE[6]

    VARAGE: ARRAY[1..6] OF INTERGER;

  • 8/13/2019 Program Implementation ITnotes

    31/32

  • 8/13/2019 Program Implementation ITnotes

    32/32

    Reading from Array Reading from Array Program WriteArray;

    Var i: integer; day: array[1..7] of real;

    Begin Day[1]:= Sunday; Day[2]:= monday; Day[3]:= tuesday; Day[4]:= wednesday; Day[5]:= thursday; Day[6]:= friday; Day[7]:= saturday;

    Begin

    writeln(which day of theweek is you favorite day);

    Read(i) skipLine(3);

    writeln(Your favorite dayis, day[1]);

    End;

    This line reads data to variable