Sie sind auf Seite 1von 18

CHAPTER 1

BASIC PROGRAMMING CONCEPTS


OBJECTIVES:
At the end of this chapter the students should be able to: 1. 2. 3. 4. Define a computer program. Identify a programming language. Differentiate the types of programming languages. Discuss the evolution of the C++ programming language. 5. Identify an algorithm and its methods of representation. 6. Write algorithms through flowcharting.

2
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 off represented by binary numbers 1 and 0, respectively. Assembly language is composed of instruction codes stated in mnemonic forms (ADD for add, SUB for subtract, etc.). To convert these program codes into machine language codes, an assembler is 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 compiler or an interpreter to run programs. Compilers translate human readable programs in to machine language before they can be executed, while an interpreter program executes highlevel 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

1.2 The Evolution of the C++ Programming Language


C++ is an extension of the C programming language, which acquired its ideas from BCPL 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 then used 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 those times, 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.

4
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. 2. 3. 4. Input: Zero or more quantities are externally supplied. Output: At least one quantity is produced. Definiteness: Each instruction must be clear and unambiguous. Finiteness: The algorithm must terminate after performing a finite number of steps 5. Effectiveness: Both the criteria of definiteness and effectiveness must be satisfied wherein every instruction must be sufficiently basic that it can be carried 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 than pseudocodes. 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.

Figure 1.1 Common Flowcharting Symbols The Terminal Symbol This 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.

6
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.

Start End
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.

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 the flowchart. 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 be using 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:

8
Operator + * / % == != < > <= Operation arithmetic ADDITION arithmetic SUBTRACTION arithmetic MULTIPLICATION arithmetic DIVISION arithmetic MODULO Description returns the sum returns the difference returns the product returns the quotient 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 LESS THAN OR returns TRUE if the left operand EQUAL TO is less than or equal to the right operand, otherwise returns FALSE relational GREATER THAN returns TRUE if the left operand OR EQUAL TO is greater than or equal to the right 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 operations Table 1.1 Basic Operators

>=

|| && ! = ()

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.2 Sample 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 stored into 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 terminal symbol.

10
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.

11
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, is a shorthand process that increments the value of NUM by 1, which means add 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.

12

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.

13

PRACTICE EXERCISE #1
NAME: __________________________ COURSE/YR-SEC: ________________ DATE: ____________________ 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.

14
NAME: __________________________ COURSE/YR-SEC: ________________ DATE: ____________________ INSTRUCTOR:_____________

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

4. Print the odd numbers from 99 to 1.

15
NAME: __________________________ COURSE/YR-SEC: ________________ DATE: ____________________ 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.

16
NAME: __________________________ COURSE/YR-SEC: ________________ DATE: ____________________ 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.

17
NAME: __________________________ COURSE/YR-SEC: ________________ DATE: ____________________ 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).

18
10. The cost of a certain item depends on a quantity ordered as given by the following table: QUANTITY ORDERED 1-99 100-199 200-299 300 or more COST/ITEM P 5.95 5.75 5.50 5.10

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

Das könnte Ihnen auch gefallen