chapter 1 basic programming concepts

Upload: rochelle-ann

Post on 08-Aug-2018

223 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/22/2019 CHAPTER 1 Basic Programming Concepts

    1/18

    OBJECTIVES:

    At the end of this chapter the students should be able to:

    1. Define a computer program.

    2. Identify a programming language.3. Differentiate the types of programming languages.

    4. Discuss the evolution of the C++ programming

    language.

    5. Identify an algorithm and its methods of

    representation.

    6. Write algorithms through flowcharting.

    CHAPTER 1BASIC

    PROGRAMMING

    CONCEPTS

  • 8/22/2019 CHAPTER 1 Basic Programming Concepts

    2/18

    Welcome to C++. But before we explore the beauty of C++ programming, let us

    first discuss the basic concepts of programming.

    1.1 Programming Languages

    A program is a set of instructions that tells the computer what to do. In writing

    such instructions, a programmer, a person who writes programs, needs a third party

    between him and the computer, a programming language. There are two types of

    programming languages: low-level languages and high-level languages.

    Low-level languages require programmers to have a good understanding of the

    underlying hardware. Two different and incompatible processors understand only

    its own low-level language making it machine dependent. Low-level languages

    include machine languages and assembly language. Machine languages are at the

    lowest level defined by a combination of circuits that can either be on or offrepresented by binary numbers 1 and 0, respectively. Assembly language iscomposed of instruction codes stated in mnemonic forms (ADD for add, SUB for

    subtract, etc.). To convert these program codes into machine language codes, an

    assembleris being used.

    High-level languages are the more convenient programming language mainly

    because they are quasi-English and do not require the programmer to know the

    actual computer hardware. These languages use either a compileror an interpreter

    to run programs. Compilers translate human readable programs in to machine

    language before they can be executed, while an interpreterprogram executes high-

    level language programs directly without the need for compilation. High-level

    languages include:

    FORTRAN FORmula TRANslator

    COBOL COmmon Business-Oriented Language

    PL/1 Programming Language 1

    BASIC Beginners All-purpose Symbolic Instruction Code

    Pascal

    C & C++

    Java

    2

  • 8/22/2019 CHAPTER 1 Basic Programming Concepts

    3/18

    1.2 The Evolution of the C++ Programming Language

    C++ is an extension of the C programming language, which acquired its ideas fromBCPL and B.

    BCPL and B are typeless languages, compared with C and C++ which provides

    a variety of data types. Martin Richards developed BCPL in 1967 whose objective

    is for operating systems and compilers development. Using a DEC PDP-7

    computer, Ken Thomson developed B in 1970 at the Bell Laboratories. It was thenused to create early versions of the UNIX operating system.

    From B comes C, which was developed in 1972 by Dennis Ritchie also at Bell

    Laboratories using a DEC PDP-11 computer. Not until the publication of The C

    Programming Language book by Kernighan and Ritchie himself in 1978, has the

    language became very popular. In addition to its gaining popularity during thosetimes, the American National Standard Institute (ANSI) moved for the

    standardization of C (ANSI C) in 1983, and was later approved late in 1988.

    Modern compilers support this standardization.

    In 1990, Bjarne Stroustrup developed C++ also at Bell Laboratories. Evolving

    from the C language, C++ provides capabilities for object-oriented programming.

    However, being a hybrid language, C++ allows C-like programming (i.e.

    structured), object-oriented programming, or both.

    1.3 ALGORITHMS

    An algorithm is a well-defined procedure that takes some value (or set of values)

    as input and produces some value (or set of values) as output. It can also be

    defined as a set of instructions, which if followed, accomplish a particular task.

    Some of the advantages in writing algorithms before actual program coding are as

    follows:

    1. Algorithms are language independent.

    2. Problems can be analyzed and interpreted in a more efficient way.

    3. Algorithms can serve as a guide during system development, debugging

    and analysis.4. Program maintenance becomes easier.

    5. Algorithms help in program documentation.

    3

  • 8/22/2019 CHAPTER 1 Basic Programming Concepts

    4/18

    However, there are also some disadvantages, such as:

    1. Given a complex problem at hand, the algorithm can be equally

    complicated.2. There are cases where the algorithm needs to be rewritten completely.

    For an algorithm to be suitable for computer use, it must possess the following

    criteria:

    1. Input: Zero or more quantities are externally supplied.

    2. Output: At least one quantity is produced.

    3. Definiteness: Each instruction must be clear and unambiguous.

    4. Finiteness: The algorithm must terminate after performing a finite numberof steps

    5. Effectiveness: Both the criteria of definiteness and effectiveness must be

    satisfied wherein every instruction must be sufficiently basic that it can becarried out.

    There are two common methods of representing an algorithm: pseudocode and

    flowchart. A pseudocode is an algorithm presented in an informal language. A

    flowchart is a diagram or a blueprint that shows a sequence of operations to be

    performed in solving a computational problem.

    1.4 FLOWCHARTS

    In this section, we will be presenting algorithms using flowcharts rather thanpseudocodes. Flowcharts are more preferred for introduction due to its simplicity,

    with pseudocodes for advanced users.

    There are two types of flowcharts: system and program flowchart. A system

    flowchart defines the major phases of a particular processing system, as well as the

    various media used. A program flowchart shows a sequence of operations the

    computer is to perform to solve a specific programming problem. The latter will be

    the focus of the discussion of this book.

    ANSI defined a set of standard flowcharting symbols presented in Figure 1.1.

    4

  • 8/22/2019 CHAPTER 1 Basic Programming Concepts

    5/18

    Figure 1.1

    Common Flowcharting Symbols

    The Terminal SymbolThis symbol indicates the start and end of a flowchart. It

    usually contains the word START or END.

    The Input/Output Symbol This symbol indicates that some form of input or

    output is performed.

    The Process Symbol Drawn with a rectangle, this box an shows action or

    processing of operations.

    The Decision/Branching Symbol This diamond symbol shows that a branch to

    one of two or three options is possible.

    The On-Page Connector This symbol is used to connect a part of a flowchart to

    another situated on the same page. It uses a common identifier, such as a letter,

    showing the exit and entry point of the flowchart.

    The Off-Page Connector This symbol is used to connect a part of a flowchart to

    another situated on another page. Like the on-page connector, it uses a common

    identifier showing the exit and entry point of the flowchart.

    The Preparation Symbol This hexagonal symbol is normally used for

    initialization.

    5

  • 8/22/2019 CHAPTER 1 Basic Programming Concepts

    6/18

    The Flowline Symbols These arrowhead lines indicates a direction of flow or

    sequence of flowchart operation.

    In writing or designing flowcharts, it is important to follow certain guidelines.

    These are as follows:

    1. List all necessary requirements in a logical order.

    2. Ensure that the flowchart starts and ends with a terminal symbol.

    3. The flowchart must be unambiguously presented.

    a. Only one flow line is used with a terminal symbol.

    b. Only one flow line should come out from a process symbol.

    c. Only one flow line should enter a decision symbol, with two or

    three flow lines coming out of it.

    The flow of the procedure must normally be presented from left to right

    or top to bottom.

    4. The description of a computational process or a data must be written

    briefly, but clearly.5. Use connector symbols when the flowchart becomes complex to avoid

    intersection of flow lines.6. Test the flowchart using some simple test data.

    Flowchart 1.1 shows a very simple example of a program flowchart.

    Start

    End

    6

  • 8/22/2019 CHAPTER 1 Basic Programming Concepts

    7/18

    Flowchart 1.1

    Sample Flowchart that Prints Welcome to Flowcharting

    Flowchart 1.1 has three flowcharting symbols: terminal, flowline, and

    input/output. It starts with a terminal symbol signifying the START of theflowchart. A flowline from the START flows to an input/output symbol, which

    indicates that Welcome to Flowcharting will be printed, and finally a flowline to

    another terminal symbol to show the end of the flowchart.

    1.4.1 Basic Operators

    Since this section is a preliminary to program development in C++ we will beusing the same set of operators that C++ uses in manipulating data. Table 1.1

    shows a summary of the basic operators we will use in writing flowcharts:

    7

  • 8/22/2019 CHAPTER 1 Basic Programming Concepts

    8/18

    Operator Operation Description

    + arithmetic ADDITION returns the sum

    - arithmetic SUBTRACTION returns the difference

    * arithmetic MULTIPLICATION returns the product/ arithmetic DIVISION returns the quotient

    % arithmetic MODULO returns the remainder in

    arithmetic division

    == relational EQUAL TO returns TRUE if the left operand

    is equal to the right operand,otherwise returns FALSE

    != relational NOT EQUAL TO returns TRUE if the left operand

    is not equal to the right operand,

    otherwise returns FALSE

    < relational LESS THAN returns TRUE if the left operand

    is less than the right operand,

    otherwise returns FALSE

    > relational GREATER THAN returns TRUE if the left operand

    is greater than the right operand,

    otherwise returns FALSE

    = relational GREATER THAN

    OR EQUAL TO

    returns TRUE if the left operand

    is greater than or equal to theright operand, otherwise returns

    FALSE

    || logical OR returns FALSE if both the left and

    right operands are FALSE,

    otherwise returns TRUE

    && logical AND returns TRUE if both the left and

    right operands are TRUE,

    otherwise returns FALSE

    ! logical NOT returns TRUE if the right operand

    is FALSE, otherwise returns

    FALSE.

    = assignment assigns a right hand value to a left

    hand variable

    ( ) grouping symbol groups a set of operationsTable 1.1

    Basic Operators

    8

  • 8/22/2019 CHAPTER 1 Basic Programming Concepts

    9/18

    1.4.2 Basic Flowcharting Patterns

    The following are flowcharting patterns that we will discuss in this section:

    1. Sequential Pattern The flow executes in sequence.

    For example, write a program that will read in two numbers and output

    their sum and average.

    Flowchart 1.2Sample Sequential Pattern

    The first process involves a terminal symbol indicating the start of

    flowchart. We then read in (input) two numbers and store them to

    variables NUM1 and NUM2.

    Next are processes involving two operations, i.e., addition and division.

    The first process involves the adding of the two variables and storing the

    result into the variable SUM, which was then divided by 2 and was storedinto the variable AVERAGE.

    We will then output (write) the results, i.e., the contents of SUM and

    AVERAGE, and finally terminate the flowchart with the end terminalsymbol.

    9

  • 8/22/2019 CHAPTER 1 Basic Programming Concepts

    10/18

    2. Selection Pattern The execution flow depends on a certain

    condition. This condition normally involves an operation that may

    result to either TRUE or FALSE.

    For example, write a program that will read in an integer and output

    the word POSITIVE if it is a positive integer, and NEGATIVE if

    otherwise.

    Flowchart 1.3

    Sample Selection Flowchart

    Just like our first example, we start and end our flowchart with a terminal

    symbol. However, the flowchart above allows the flowchart to decide

    whether to output the word POSITIVE or NEGATIVE depending on the

    result of the decision or branching symbol, i.e. if INTEGER is greater than

    or equal to 0 - output POSITIVE, otherwise - output NEGATIVE.

    10

  • 8/22/2019 CHAPTER 1 Basic Programming Concepts

    11/18

    3. Loop Pattern The execution flow is performed a certain number

    of times depending on some condition.

    For example, draw a flowchart that will print the numbers 1-10.

    Flowchart 1.4

    Sample Loop Pattern

    In the example above, we initialize a variable NUM to 1. We output this

    value using a input/output symbol. The next step, i.e., NUM=NUM+1, isa shorthand process that increments the value of NUM by 1, which meansadd 1 to the current value of NUM (in the first case 1+1 = 2), and assign

    this value to NUM.

    The next step is to test whether NUM is greater than 10. If this condition

    is true, we then execute the terminal symbol END. Otherwise, we move to

    the on-page connector symbol that instructs the flowchart to move to the

    location pointed to by the connector. This is the part that illustrates a loop

    pattern.

    11

  • 8/22/2019 CHAPTER 1 Basic Programming Concepts

    12/18

    SUMMARY

    In this chapter we discussed the basics of computer programming: define a

    program, know what programming languages are, what are its types. C++ being

    the focus was described briefly by stating how the programming language evolved.

    Algorithm development was introduced thru flowcharting to develop the logical

    thinking of the students.

    12

  • 8/22/2019 CHAPTER 1 Basic Programming Concepts

    13/18

    PRACTICE EXERCISE #1

    NAME: __________________________ DATE: ____________________

    COURSE/YR-SEC: ________________ INSTRUCTOR: ____________

    DIRECTION: Draw a flowchart that will perform the following instructions.

    1. Input an integer. Print DIVISIBLE if the number is divisible by 2,

    otherwise, print NOT DIVISIBLE.

    2. Input three numbers and print only the highest number.

    13

  • 8/22/2019 CHAPTER 1 Basic Programming Concepts

    14/18

    NAME: __________________________ DATE: ____________________

    COURSE/YR-SEC: ________________ INSTRUCTOR:_____________

    3. Input three numbers and output the numbers in ascending order.

    4. Print the odd numbers from 99 to 1.

    14

  • 8/22/2019 CHAPTER 1 Basic Programming Concepts

    15/18

    NAME: __________________________ DATE: ____________________

    COURSE/YR-SEC: ________________ INSTRUCTOR: ____________

    5. Print the numbers divisible by 5 from 10 to 1000.

    6. Print the numbers divisible by three but not divisible by 9 from 3 to 99.

    15

  • 8/22/2019 CHAPTER 1 Basic Programming Concepts

    16/18

    NAME: __________________________ DATE: ____________________

    COURSE/YR-SEC: ________________ INSTRUCTOR: ____________

    7. Print the following series of numbers: 1, 1, 2, 3, 5, , 89.

    8. A triangle is equilateral if all three sides are equal, isosceles if only two of

    its sides have the same length and scalene if no two sides have the same

    length. Draw a flowchart to input the lengths of the three sides of a

    triangle and print whether the triangle is equilateral, isosceles or scalene.

    16

  • 8/22/2019 CHAPTER 1 Basic Programming Concepts

    17/18

    NAME: __________________________ DATE: ____________________

    COURSE/YR-SEC: ________________ INSTRUCTOR: ____________

    9. A young man agrees to work for a company at a very modest salary of

    P100.00 per week with a condition that his salary will double each week.

    How much is his weekly salary and earnings at the end of six months

    (assume that each that there are four weeks per month).

    17

  • 8/22/2019 CHAPTER 1 Basic Programming Concepts

    18/18

    10. The cost of a certain item depends on a quantity ordered as given by the

    following table:

    QUANTITY ORDERED COST/ITEM1-99 P 5.95

    100-199 5.75

    200-299 5.50

    300 or more 5.10

    Draw a flowchart to input the quantity ordered and output the total cost.

    18