02 algorithm, pseudocode and coding

Upload: ezra-clement

Post on 02-Jun-2018

235 views

Category:

Documents


1 download

TRANSCRIPT

  • 8/10/2019 02 Algorithm, Pseudocode and Coding

    1/90

    COMPUTER AND PROGRAMMINGPSEUDOCODEFLOWCHART

    Algorithm and Programmingsession 2

  • 8/10/2019 02 Algorithm, Pseudocode and Coding

    2/90

    The Computer

  • 8/10/2019 02 Algorithm, Pseudocode and Coding

    3/90

    Physical Description of a

    Computer

    Main components of a Computer

    A computer is a digital electronic machine composed of three main components:Central Processing Unit (CPU), memory, and input/output peripherals.

    The processor carries out instructions which are stored in the memory. Along with theinstructions, data is also stored in memory.

    The processor typically is instructed to manipulate the data in some desired fashion.

    Input/output devices take information from agents external to the machine and provideinformation to those agents.

    Input devices are typically keyboards, disk drives and tape drives.

    Output devices are typically monitors, printers, disk drives and tape drives.

    The computers used in this course are connected in a UNIX system to allow shared

    use of resources and devices

  • 8/10/2019 02 Algorithm, Pseudocode and Coding

    4/90

    Programs

    Computers are very general purpose machines. They can be used toperform a great variety of different tasks:- writing documents, solvingnumerical and mathematical problems, drawing pictures, sending "e-mail" messages to friends etc. However, in order for the computer toperform all these tasks the computer needs to be provided with detailed

    instructions as to how it is to go about the job. A program is a set of instructions which specifies how a computer is to

    perform a particular task. Programs may be very complex. For examplea spreadsheet program can display large amounts of information andperform complex calculations. Whereas a simple program might do

    nothing more than check the clock and print the current date and time onthe screen.

    All computers come with a large number of programs pre-installed andyou can buy new programs for many different tasks.

  • 8/10/2019 02 Algorithm, Pseudocode and Coding

    5/90

    Programming

    As a programmer you can create new programs and add them to theexisting suite of programs so that your computer can tackle new tasks.Your computer will provide a variety of existing programs which allowyou to create, test and modify your own programs and eventually, whenyour program is working correctly to install it for your own use. In this

    course, you will become familiar with some of these program buildingprograms or "programming tools". Programs are always written using aprogramming language, of which C++ are examples.

    Computers follow the instructions in programs literally so if you make amistake in the program the computer will do what it is told (not what you

    intended). This is usually easy to spot in small programs but largeprograms need to be designed and written very carefully to make surethat they work correctly.

    Learning to program requires not just learning a new language but alsolearning how to use it effectively.

  • 8/10/2019 02 Algorithm, Pseudocode and Coding

    6/90

    Programming is a core activity in the process ofperforming tasks or solving problems with the aid of acomputer. An idealised picture is:

    [problem or task specification] - COMPUTER- [solution orcompleted task]

    http://www.doc.ic.ac.uk/~wjk/C++Intro/computer.htmlhttp://www.doc.ic.ac.uk/~wjk/C++Intro/computer.html
  • 8/10/2019 02 Algorithm, Pseudocode and Coding

    7/90

    The Origins of C++

    C++ was developed by Bjarne Stroustrup ofAT&T Bell Laboratories in the early 1980's, and isbased on the C language. The name is a pun -

    "++ means C++ is intended as an incrementalimprovement of C. Most of C is a subset of C++,so that most C programs can be compiled (i.e.converted into a series of low-level instructions

    that the computer can execute directly) using aC++ compiler.

  • 8/10/2019 02 Algorithm, Pseudocode and Coding

    8/90

    ANSI C++

    The American National Standards Institution (ANSI) provides "official"and generally accepted standard definitions of many programminglanguages, including C and C++. Such standards are important. Aprogram written only in ANSI C++ is guaranteed to run on any computerwhose supporting software conforms to the ANSI standard. In otherwords, the standard guarantees that ANSI C++ programs are portable. Inpractice most versions of C++ include ANSI C++ as a core language, butalso include extra machine-dependent features to allow smoothinteraction with different computers' operating systems. These machinedependent features should be used sparingly. Moreover, when parts of a

    C++ program use non-ANSI components of the language, these shouldbe clearly marked, and as far a possible separated from the rest of theprogram, so as to make modification of the program for differentmachines and operating systems as easy as possible.

  • 8/10/2019 02 Algorithm, Pseudocode and Coding

    9/90

    Stages of Programming

  • 8/10/2019 02 Algorithm, Pseudocode and Coding

    10/90

    Creating

    Creating a program involves two main stages: designing a program to achieve the required goals, and physically creating the program.

    Throughout the course we will look at how to design programs and the techniques that canbe used. We will first lookat how the physical program is created once the design stagehas been completed. To create a physical program it has to be entered into the

    computer. To do this, normally a file is opened and the text of the program is typed in.When programming in C++ the name of the file by convention ends with .cpp (full stop,lower case cpp) or .C or .cxx, e.g.

    Example filenames using .cpp - program.cpp, hello.cpp or myfile.cpp Example filenames using .C - program.C, hello.C or myfile.C Example filenames using .cxx - program.cxx, hello.cxx or myfile.cxx

    The part of the filename after the . (dot) is known as the file extension. In general eachprogramming language has a different convention for the file extension. The program isnormally entered using a standard text editor such as nedit, vi or emacs. Having enteredthe program, the contents of the file are saved and you would exit from the text editor.The information held in this file is known as the sou rce code.

  • 8/10/2019 02 Algorithm, Pseudocode and Coding

    11/90

    Compiling

    To Compile and link the source code, useunder Linux GNU compiler, and type as theexample :

    g++ hello.cpp o hello(Enter the correct name of the file in which you entered your program in the creationstep). If there are errors in the program, such as omitting the semi-colon after the coutstatement, the compiler will detect the errors and report them. If errors are indicated theymust be corrected by re-editing the program in the file. The compiler can then be

    invoked again, using the above command, until no errors are reported. As well asindicating errors which will be flagged with the word Error, the compiler may alsoproduce Warnings. These are not errors which must be corrected before the programcan run, but are instead indications from the compiler that it thinks your program orsyntax are suspicious. They do not need to be corrected, but you should alwaysinvestigate how significant they are. Once the compiler has successfully compiled theprogram an executable file named a.out will exist.

  • 8/10/2019 02 Algorithm, Pseudocode and Coding

    12/90

    Running

    To run the program you just type./hello

    at prompt (hello is the filename)

  • 8/10/2019 02 Algorithm, Pseudocode and Coding

    13/90

    The Steps of Creating Program

    1.Type the command in text editor

    2.Save the file with .cpp extension

    3.Go to terminal4.Go to the same directory with the .cpp file

    you've typed (by type cd dirname)

    5.Type g++ filename.cpp -o filenameto compilethe file

    6.To run the program, type ./filename

  • 8/10/2019 02 Algorithm, Pseudocode and Coding

    14/90

    Exercise

  • 8/10/2019 02 Algorithm, Pseudocode and Coding

    15/90

    #include int main(int argc, char *argv[])

    {QCoreApplication a(argc, argv);

    return a.exec();

    } 09/23/07

  • 8/10/2019 02 Algorithm, Pseudocode and Coding

    16/90

    Please try to write thiscommand// Program written September 2014

    // by My Group

    #include

    using namespace std;main()

    {

    cout

  • 8/10/2019 02 Algorithm, Pseudocode and Coding

    17/90

    program

    // program to add two integer values and display result

    #include

    #include

    using namespace std;

    int main(int argc, char *argv[])

    {

    QCoreApplication a(argc, argv);

    //declare variablesint value1, value2, sum;

    // assign values and compute the result

    value1 = 32;

    value2 = 27;

    sum = value1 + value2;

    // display the result

    cout

  • 8/10/2019 02 Algorithm, Pseudocode and Coding

    18/90

    #include

    using namespace std;

    int Tambah(int x,int y){

    cout

  • 8/10/2019 02 Algorithm, Pseudocode and Coding

    19/90

    Building A Program

  • 8/10/2019 02 Algorithm, Pseudocode and Coding

    20/90

    Introduction to Design and Style

    Before writing a program:

    Have a thorough understanding of problemrequirements

    Carefully plan your approach for solving it While writing a program:

    Know what building blocks are available

    Use good programming principlestop-downdesign

  • 8/10/2019 02 Algorithm, Pseudocode and Coding

    21/90

    Algorithms

    All computing problems can be solved by executing a series of actions in

    a specific order

    steps should be independent of computer

    languageAlgorithmA procedure determining theActions to be executed

    Order in which these actions are to be executed

    Program control Specifies the order in which statements are to

    executed

  • 8/10/2019 02 Algorithm, Pseudocode and Coding

    22/90

    Algorithm Development

    1) TotalDistance = EndingPointStartPoint

    2) ElapsedTime = EndHour + (EndMinute / 60)

    + (EndSecond / 3600)3) Speed = TotalDistance / ElapsedTime

  • 8/10/2019 02 Algorithm, Pseudocode and Coding

    23/90

    // Fred Annexstein

    // January 2003

    // Program to Compute velocity of race car

    // Design inserted here

    #include

    using namespace std;

    int main() {

    cout StartPoint;

    cout > EndPoint;

    cout > EndHour >> EndMinute >> EndSecond;

  • 8/10/2019 02 Algorithm, Pseudocode and Coding

    24/90

    int TotalDistance;

    TotalDistance = EndPoint - StartPoint;

    float ElapsedTime;

    ElapsedTime = EndHour + (EndMinute / 60.0)

    + (EndSecond / 3600.0);

    float Speed = Distance / ElapsedTime;cout

  • 8/10/2019 02 Algorithm, Pseudocode and Coding

    25/90

    Building a Program

    Whatever type of problem needs to be solved, acareful thought out plan of attack, called analgorithm, is needed before a computer solution

    can be determined.

    1) Developing the algorithm.

    2) Writing the program.

    3) Documenting the program.

    4) Testing and debugging the program.

  • 8/10/2019 02 Algorithm, Pseudocode and Coding

    26/90

    Building a Program

    1) Developing the algorithm.

    Algorithm: A detailed description of the exactmethods used for solving a particular problem.

    To develop the algorithm, the programmer needsto ask:

    What data has to be fed into the computer?

    What information do I want to get out of the computer?

    Logic: Planning the processing of the program. Itcontains the instructions that cause the input data tobe turned into the desired output data.

  • 8/10/2019 02 Algorithm, Pseudocode and Coding

    27/90

    Building a Program

    A step-by-step program plan is createdduring the planning stage.

    The two major notations for planning

    detailed algorithms:Pseudocode: A verbal shorthand method that

    closely resembles a programming language,but does not have to follow a rigid syntaxstructure.

    Flowchart: Series of visual symbolsrepresenting the logical flow of a program.

  • 8/10/2019 02 Algorithm, Pseudocode and Coding

    28/90

    Building a Program

    Start

    Count Money

    Do youhave more than

    $10.00?Go out

    Go home

    End

    No

    Yes

    1. If money < $10.00 then go homeElse Go out

    2. Count money

    3. Go to number 1

    Pseudocode:Flow chart:

  • 8/10/2019 02 Algorithm, Pseudocode and Coding

    29/90

    Building a Program

    2) Writing the Program

    If analysis and planning have been thoroughlydone, translating the plan into a programming

    language should be a quick and easy task.

    3) Documenting the Program

    During both the algorithm development and

    program writing stages, explanations calleddocumentation are added to the code.

  • 8/10/2019 02 Algorithm, Pseudocode and Coding

    30/90

    Building a Program

    4) Testing and Debugging the Program. The program must be free of syntax errors.

    The program must be free of logic errors.

    The program must be reliable. (produces correct results)

    The program must berobust. (able to detect execution

    errors)

    User Acceptance testing

    Alpha testing: Testing within the company. Beta testing: Testing under a wider set of conditions using

    sophisticated users from outside the company.

  • 8/10/2019 02 Algorithm, Pseudocode and Coding

    31/90

    Types of Programming Errors

    Syntax errorsErrors caught by compiler

    Logic errorsErrors which have their effect at

    execution timeNon-fatal logic errors

    program runs, but has incorrect outputFatal logic errors

    program exits prematurely

  • 8/10/2019 02 Algorithm, Pseudocode and Coding

    32/90

    Pseudocode

  • 8/10/2019 02 Algorithm, Pseudocode and Coding

    33/90

    Pseudo Code

    Pseudo means pretend or false

    Pseudo Code is pretend or false computer code;generic English-like terms that are somewhat

    like computer code Pseudo Code is not as standardized as

    flowcharts, and does not facilitate the breakingdown of problems as well as a flowchart does

  • 8/10/2019 02 Algorithm, Pseudocode and Coding

    34/90

    Pseudocode

    PseudocodeArtificial, informal language used to develop

    algorithms

    Similar to everyday EnglishNot actually executed on computers

    Allows us to think out a program before

    writing the code for itUsually easy to convert into a corresponding

    C++ program

    Consists only of executable statements

    P d d f l

  • 8/10/2019 02 Algorithm, Pseudocode and Coding

    35/90

    Pseudocode for a sampleproblem

    Programming problem: write a program todetermine the speed or velocity of a race car.

    What are the inputs?

    What are the outputs?

    What is the algorithm or process used?

    P d d f l

  • 8/10/2019 02 Algorithm, Pseudocode and Coding

    36/90

    Pseudocode for a sampleproblem

    What are the inputs?Starting point,Ending point,Elapsed time in Hours, Minutes, Seconds

    What are the outputs?Speed of Car

    What does algorithm compute? Total Distance traveled (in miles) Total Elapsed time (in hours) Speed (in miles per hour)

  • 8/10/2019 02 Algorithm, Pseudocode and Coding

    37/90

    Flowchart

  • 8/10/2019 02 Algorithm, Pseudocode and Coding

    38/90

    Flowcharts

    A Flowchart is a VisualRepresentation of an algorithm

    A Flowchart uses easy-to-understandsymbols to represent actions on dataand the flow of data

    Flowcharts aid in breaking down aproblem into simple steps

  • 8/10/2019 02 Algorithm, Pseudocode and Coding

    39/90

    Basic Flowchart Symbols

    ProcessTerminator

    Input/Output

    Decision

    Connector

    Data Flow

    IndicatesThe beginningof Repetition

    Indicatesthe Beginning of

    Sub Program

  • 8/10/2019 02 Algorithm, Pseudocode and Coding

    40/90

    The Basic Flowchart Symbols

    The terminator shows where aspecific piece of an algorithmbeings and ends

    A process is a basic action oractions on data

    A decision is a logical test withtwo possible data pathsresulting from one

    Terminator

    Process

    Decision

  • 8/10/2019 02 Algorithm, Pseudocode and Coding

    41/90

    The Basic Flowchart Symbols

    Input/Output gets data ordisplays information to the userof the algorithm

    A connector connects two ormore data flows into one

    A data flow shows the direction

    of datas movement throughthe algorithm

    Input/Output

    Connector

    Data Flow

  • 8/10/2019 02 Algorithm, Pseudocode and Coding

    42/90

    Programing Construct

    The Three Computer Programming

  • 8/10/2019 02 Algorithm, Pseudocode and Coding

    43/90

    The Three Computer ProgrammingConstructs

    Any problem, regardless of howcomplex, can be broken down into

    three basic CONSTRUCTS

    SEQUENCE

    SELECTIONITERATION

  • 8/10/2019 02 Algorithm, Pseudocode and Coding

    44/90

    Sequence

    Sequence is the most basic of theconstructs

    It is simply performing one step after

    another Each step is followed in a specific

    sequence, hence the name

    Sequence can be thought of as dothis, then do this, then do this

    S

  • 8/10/2019 02 Algorithm, Pseudocode and Coding

    45/90

    Sequences

    A sequence of instructionsthat are executed in theprecise orderthey are written in:

    statement block 1

    statement block 2

    statement block 3

    statement block 1

    statement block 2

    statement block 3

  • 8/10/2019 02 Algorithm, Pseudocode and Coding

    46/90

    Selection / Conditionals

    Selection is the decision-makingconstruct

    It is used to make yes/no ortrue/false decisions logically

    Selection can be thought of as if

    something is true take this action,otherwise take that action

  • 8/10/2019 02 Algorithm, Pseudocode and Coding

    47/90

    Selection / Conditionals

    Select between alternate courses of actiondepending upon the evaluation of a condition

    If ( condition = true )

    statement block 1

    Else

    statement block 2

    End ifstatement

    block 1

    conditionTrue False

    statementblock 2

  • 8/10/2019 02 Algorithm, Pseudocode and Coding

    48/90

    Iteration

    Iteration comes from the word reiterate,which means to repeat

    Iteration is a looping construct Iteration is a combination of decision and

    sequence and can repeat steps

    Iteration can be thought of as whilesomething is true, do this, otherwise stop

  • 8/10/2019 02 Algorithm, Pseudocode and Coding

    49/90

  • 8/10/2019 02 Algorithm, Pseudocode and Coding

    50/90

    Visualizing The Constructs

    By using Flowchart symbols, wecan visualize the three basic

    computer constructsEvery algorithm ever created is

    made up of only these three

    constructs

  • 8/10/2019 02 Algorithm, Pseudocode and Coding

    51/90

    Exercise of Building Program

  • 8/10/2019 02 Algorithm, Pseudocode and Coding

    52/90

    Exercise 1

    Write an algorithm to determine astudents final grade and indicatewhether it is passing or failing. Thefinal grade is calculated as the averageof four marks.

    passing grade is >=50

  • 8/10/2019 02 Algorithm, Pseudocode and Coding

    53/90

  • 8/10/2019 02 Algorithm, Pseudocode and Coding

    54/90

    Solution of Exercise 1

    Detailed AlgorithmStep 1: Input M1,M2,M3,M4

    Step 2: GRADE (M1+M2+M3+M4)/4

    Step 3: if (GRADE < 50) thenPrint FAIL

    else

    Print PASS

    endif

  • 8/10/2019 02 Algorithm, Pseudocode and Coding

    55/90

    Solution of Exercise 1

    PRINTPASS

    Step 1: Input M1,M2,M3,M4Step 2: GRADE (M1+M2+M3+M4)/4Step 3: if (GRADE

  • 8/10/2019 02 Algorithm, Pseudocode and Coding

    56/90

  • 8/10/2019 02 Algorithm, Pseudocode and Coding

    57/90

    Solution of Exercise 2

    Algorithm

    Step 1: Input Lft

    Step 2: Lcm Lft x 30

    Step 3: Print Lcm

    START

    Input

    Lft

    LcmLft x 30

    Print

    Lcm

    STOP

    Flowchart

    Print Lcm

  • 8/10/2019 02 Algorithm, Pseudocode and Coding

    58/90

    Exercise 3

    Write an algorithm and draw a

    flowchart that will read the two

    sides of a rectangle andcalculate its area.

  • 8/10/2019 02 Algorithm, Pseudocode and Coding

    59/90

    Solution of Exercise 3

    Pseudocode

    Input the width (W) and Length (L) of arectangle

    Calculate the area (A) by multiplying L with W Print A

  • 8/10/2019 02 Algorithm, Pseudocode and Coding

    60/90

    Solution of Exercise 3

    Algorithm

    Step 1: Input W,L

    Step 2: A L x W Step 3: Print A

    START

    InputW, L

    AL x W

    PrintA

    STOP

    Print A

    E i 4

  • 8/10/2019 02 Algorithm, Pseudocode and Coding

    61/90

    Exercise 4

    Write an algorithm and draw a flowchartthat will calculate the roots of a quadratic

    equation

    Hint: d= sqrt ( ), and the roots

    are: x1= (b+ d)/2a and x2= (bd)/2a

    2ax bx c

    24b ac

  • 8/10/2019 02 Algorithm, Pseudocode and Coding

    62/90

    Solution of Exercise 4

    Pseudocode: Input the coefficients (a, b, c) of the quadratic

    equation

    Calculate d Calculate x1

    Calculate x2

    Print x1 and x2

  • 8/10/2019 02 Algorithm, Pseudocode and Coding

    63/90

    Solution of Exercise 4

    Algorithm: Step 1: Input a, b, c

    Step 2: dsqrt ( )

    Step 3: x1 (b+ d) / (2 x a)

    Step 4: x2 (bd) / (2 x a)

    Step 5: Printx1,x2

    START

    Inputa, b, c

    dsqrt(b x b4 x a xc)

    Printx1,x2

    STOP

    x1(b+ d) / (2 x a)

    X2(bd) / (2 x a)

    4b b a

    Print x1, x2

  • 8/10/2019 02 Algorithm, Pseudocode and Coding

    64/90

    Control Structure

    C t l St t

  • 8/10/2019 02 Algorithm, Pseudocode and Coding

    65/90

    Control Structures

    Sequential execution Statements executed one after the other in the order

    written

    Transfer of control

    When the next statement executed is not the next onein sequence

    Only 3 basic control structures Sequence structure Built into C++. Programs executed sequentially by default.

    Selection structures C++ has three types - if, if/else, and switch

    Repetition structures C++ has three types -while, do/while, and for

    C t l St t

  • 8/10/2019 02 Algorithm, Pseudocode and Coding

    66/90

    Control Structures

    Pseudocode, actual C++ code and Flowchart Graphical representation of an algorithm Rectangle symbol (action symbol)

    Indicates any type of action. Oval symbol indicates beginning or end of a program, or a section of

    code (circles).

    single-entry/single-exit control structures

    Connect exit point of one control structure to entrypoint of the next (control-structure stacking). Makes programs easy to build.

    Th if S l ti St t

  • 8/10/2019 02 Algorithm, Pseudocode and Coding

    67/90

    The if Selection Structure

    Selection structure used to choose among alternative courses of action

    Pseudocode example:If students grade is greater than or equal to 60

    Print Passed If the condition is true print statement executed and program goes on to next

    statement

    If the condition is false print statement is ignored and the program goes onto the

    next statement

    Indenting makes programs easier to read C++ ignores whitespace characters

    Th if S l ti St t

  • 8/10/2019 02 Algorithm, Pseudocode and Coding

    68/90

    The if Selection Structure

    Translation of pseudocode statement into C++:if ( grade >= 60 )cout = 60 print Passed

    A decision can bemade on any

    expression.zero - falsenonzero - trueExample:3 - 4istrue

    Th i / S l ti St t

  • 8/10/2019 02 Algorithm, Pseudocode and Coding

    69/90

    The if/elseSelection Structure

    if Only performs an action if the condition is true

    if/else

    A different action is performed when condition is true andwhen condition is false

    Pseudocodeif students grade is greater than or equal to 60

    print Passed

    else

    print Failed

    C++ codeif ( grade >= 60 )

    cout

  • 8/10/2019 02 Algorithm, Pseudocode and Coding

    70/90

  • 8/10/2019 02 Algorithm, Pseudocode and Coding

    71/90

    P bl

  • 8/10/2019 02 Algorithm, Pseudocode and Coding

    72/90

    Problem (solve the problem !)

    The evaluation criteria are : Student with grade >= 60 is passed, the others

    are failed

    Students grade with score >=90 is A Students grade with score >=80 is B

    Students grade with score >=70 is C

    Students grade with score >=60 is D

    Students grade with score

  • 8/10/2019 02 Algorithm, Pseudocode and Coding

    73/90

    The if/elseSelection Structure

    Nested if/elsestructures Test for multiple cases by placing if/elseselection structures insideif/elseselection structures.

    if students grade is greater than or equal to 90Print A

    else

    if students grade is greater than or equal to 80Print Belse

    if students grade is greater than or equal to 70Print C

    elseif students grade is greater than or equal to 60

    Print Delse

    Print F

    Once a condition is met, the rest of the statements are skipped

    DECISION STRUCTURES

  • 8/10/2019 02 Algorithm, Pseudocode and Coding

    74/90

    DECISION STRUCTURES

    The expression A>B is a logical expression it describes acondi t ion we want to test

    i f A>B is true (i f A is g reater than B) we takethe action on left

    print the value of A

    i f A>B is false (i f A is not greater than B) wetake the action on right

    print the value of B

    DECISION STRUCTURES

  • 8/10/2019 02 Algorithm, Pseudocode and Coding

    75/90

    DECISION STRUCTURES

    isA>B

    Print

    B

    Print

    A

    Y N

    Print A Print B

    IF THEN ELSE STRUCTURE

  • 8/10/2019 02 Algorithm, Pseudocode and Coding

    76/90

    IFTHENELSE STRUCTURE

    The structure is as follows

    If cond i t ion then

    true alternative

    else

    false alternative

    endif

    IF THEN ELSE STRUCTURE

  • 8/10/2019 02 Algorithm, Pseudocode and Coding

    77/90

    IFTHENELSE STRUCTURE

    The algorithm for the flowchart is as follows:If A>B then

    pr in t A

    elsepr in t B

    endif

    isA>B

    Print

    B

    Print

    A

    Y N

    Print A Print B

    Relational Operators

  • 8/10/2019 02 Algorithm, Pseudocode and Coding

    78/90

    Relational Operators

    Relational Operators

    Operator Description

    > Greater than

    < Less than

    = Equal to

    Greater than or equal to

    Less than or equal to

    Not equal to

  • 8/10/2019 02 Algorithm, Pseudocode and Coding

    79/90

    Exercise

  • 8/10/2019 02 Algorithm, Pseudocode and Coding

    80/90

    Solution of Exercise 1

  • 8/10/2019 02 Algorithm, Pseudocode and Coding

    81/90

    Solution of Exercise 1

    ALGORITHMStep 1: InputVALUE1, VALUE2Step 2: if (VALUE1 > VALUE2) then

    MAX VALUE1

    else MAX VALUE2endif

    Step 3: Print The largest value is, MAX

    Solution of Exercise 1

  • 8/10/2019 02 Algorithm, Pseudocode and Coding

    82/90

    Solution of Exercise 1

    MAXVALUE1

    PrintThe largest value is,

    MAX

    STOP

    Y N

    START

    InputVALUE1,VALUE2

    MAXVALUE2

    isVALUE1>VALUE2

    PrintThe Largest Value is ,

    MAX

    NESTED IFS

  • 8/10/2019 02 Algorithm, Pseudocode and Coding

    83/90

    NESTED IFS

    One of the alternatives within an IFTHENELSE statement

    may involve furtherIFTHENELSE statement

    Exercise 2

  • 8/10/2019 02 Algorithm, Pseudocode and Coding

    84/90

    Exercise 2

    Write an algorithm that reads threenumbersand prints the value of the largest number.

  • 8/10/2019 02 Algorithm, Pseudocode and Coding

    85/90

    Solution of Exercise 2

    Step 1: Input N1, N2, N3Step 2: i f (N1>N2) then

    if (N1>N3) thenMAX N1 [N1>N2, N1>N3]

    elseMAX N3 [N3>N1>N2]

    endif

    else

    if (N2>N3) thenMAX N2 [N2>N1, N2>N3]

    else

    MAXN3 [N3>N2>N1]endif

    endif

    Step 3: Print The largest number is, MAX

    Exercise 3

  • 8/10/2019 02 Algorithm, Pseudocode and Coding

    86/90

    Exercise 3

    Flowchart: Draw the flowchart of the aboveAlgorithm.

    Exercise 4

  • 8/10/2019 02 Algorithm, Pseudocode and Coding

    87/90

    Exercise 4

    Write and algorithm and draw a flowchart to

    a) read an employee name (NAME), overtimehours worked (OVERTIME), hours absent

    (ABSENT) andb) determine the bonus payment (PAYMENT).

    Exercise 4

  • 8/10/2019 02 Algorithm, Pseudocode and Coding

    88/90

    Exercise 4

    Bonus Schedule

    OVERTIME(2/3)*ABSENT Bonus Paid

    >40 hours>30 but 40 hours>20 but 30 hours

    >10 but 20 hours 10 hours

    $50$40$30

    $20$10

    Solution of Exercise 4

  • 8/10/2019 02 Algorithm, Pseudocode and Coding

    89/90

    Step 1: InputNAME,OVERTIME,ABSENTStep 2: if (OVERTIME(2/3)*ABSENT > 40) then

    PAYMENT 50else if (OVERTIME(2/3)*ABSENT > 30) then

    PAYMENT 40else if (OVERTIME(2/3)*ABSENT > 20) then

    PAYMENT 30else if (OVERTIME(2/3)*ABSENT > 10) then

    PAYMENT 20else

    PAYMENT10endifStep 3: Print Bonus for, NAME is $, PAYMENT

    Solution of Exercise 4

  • 8/10/2019 02 Algorithm, Pseudocode and Coding

    90/90