Chapter One Introduction to Programming 1-1 Algorithm and Flowchart Algorithm is a step-by-step procedure for calculation. More precisely, algorithm is an effective method expressed as a finite list of well-defined instructions for calculating a function. Starting from an initial state, the instructions describe a computation that, when executed, will proceed through a finite number of well-defined successive states, eventually producing "output" and terminating at a final ending state. Flowchart is a graphical representation of an algorithm. These flowcharts play a vital role in the programming of a problem and are quite helpful in understanding the logic of complicated and lengthy problems. Once the flowchart is drawn, it becomes easy to write the program in any high level language. Often we see how flowcharts are helpful in explaining the program to others. Hence, it is correct to say that a flowchart is a must for the better documentation of a complex program. Flowcharts are usually drawn using some standard symbols; however, Start or end of the program Computational steps or processing function of a program Input or output operation Decision making and branching Connector or joining of two parts of program 1
The following are some guidelines in flowcharting: a. In drawing a proper flowchart, all necessary requirements should be listed out in logical order. b. The flowchart should be clear, neat and easy to follow. c. The usual direction of the flow of a procedure or system is from left to right or top to bottom. d. Only one flow line should come out from a process symbol. or e. Only one flow line should enter a decision symbol, but two or three flow lines, one for each possible answer, should leave the decision symbol. f. Only one flow line is used in conjunction with terminal symbol. h. If the flowchart becomes complex, it is better to use connector symbols to reduce the number of flow lines. Avoid the intersection of flow lines if you want to make it more effective and better way of communication. i. Ensure that the flowchart has a logical start and finish. j. It is useful to test the validity of the flowchart by passing through it with a simple test data. 2
Ex 1-1: Write an algorithm and draw the flowchart for finding the sum of two numbers Algorithm: Input: two numbers x and y Output: the sum of x and y Steps: 1. input x 2. input y 3. sum = x + y 4. output sum START Input x Input y Sum = x + y Output Sum END Q1-1: Write an algorithm and draw the flowchart for finding the area of a rectangle. 1-2 C++ Programming Language Programming is a core activity in the process of performing tasks or solving problems with the aid of a computer. [problem or task specification] - COMPUTER - [solution or completed task] Unfortunately things are not (yet) that simple. In particular, the "specification" cannot be given to the computer using natural language. Moreover, it cannot (yet) just be a description of the problem or task, but has to contain information about how the problem is to be solved or the task is to be executed. Hence we need programming languages. There are many different programming languages, but all of them can be classified into: "High-level" programming languages: These are languages whose syntax is relatively close to natural language. Low-level languages: These include many technical references to the 0's and 1's of the computer. 3
C++ is one of the high-level languages; it was developed at Bell Laboratories in the early 1980's, and is based on the C language. C++ is intended as an incremental improvement of C. Most of C is a subset of C++, so that most C programs can be compiled using a C++ compiler. 1-3 Introduction to Programming Ex 1-2: Printing line of text. Line 1: // A first program in C+ begins with //, indicating that the remainder of the line is a comment which describes the purpose of the program. A comment beginning with // is called a single-line comment because it terminates at the end of the current line. Note: You also may use C s style in which a comment (possibly containing many lines) begins with /* and ends with */ /* A first program in C++ Date 2 Oct. 2012 */ Line: #include <iostream.h> notifies the preprocessor to include in the program the contents of the input/output stream header <iostream.h>. This header must be included for any program that outputs data to the screen or inputs data from the keyboard using C++ s stream input/output. 4
The main function: int main ( ) is a part of every C++ program. The parentheses after main indicate that main is a program building block called a function. The left brace, {, must begin the body of every function. A corresponding right brace, }, must end each function s body. An Output Statement cout << Welcome to C++!\n"; instructs the computer to perform an action, to print the string of characters contained between the double quotation marks. The entire line, including cout, the << operator, the string "Welcome to C++!\n" and the semicolon ( ; ), is called a statement. Note: Every C++ statement must end with a semicolon, omitting the semicolon at the end of a C++ statement is a syntax error. Executing the last statement will print the string ( Welcome to C++!) on the screen. You noticed that \n has not been printed, it called a escape sequence. The escape sequence \n means newline. It causes the cursor (i.e., the current screen-position indicator) to move to the beginning of the next line on the screen. Some common escape sequences are listed below: The return Statement return 0; // indicate that program ended successfully is one of several means we ll use to exit a function. The right brace, }, indicates the end of function main. 5
* The string Welcome to C++! can be printed by a different way: Ex 1-3: Adding two integer numbers. This program uses the input stream object cin to obtain two integers typed by a user at the keyboard, computes the sum of these values and outputs the result using cout. 6
Variable Declarations The identifiers integer1, integer2 and sum are the names of variables. A variable is a location in the computer s memory where a value can be stored for use by a program. These declarations specify that the variables integer1, integer2 and sum are data of type int, meaning that these variables will hold integer values. Note: 1. We could have declared all three variables in one declaration by using a comma-separated list as follows: 2. Declarations of variables can be placed almost anywhere in a program, but they must appear before their corresponding variables are used in the program, for example: Displaying the Result The statement above displays the character string Sum is followed by the numerical value of variable sum followed by endl, the name endl is an abbreviation for end line. Note: Calculations can also be performed in output statements: 7
1-4 Arithmetic Operators Integer division yields an integer quotient; for example, the expression 7 / 4 evaluates to 1. C++ provides the modulus operator, %, that yields the remainder after integer division. The modulus operator can be used only with integer operands; for example, 7 % 4 yields 3. Parentheses are used in C++ expressions in the same manner as in algebraic expressions. For example, to multiply a times the quantity b + c we write: a * ( b + c ) C++ applies the operators in arithmetic expressions in a precise order determined by these rules of operator precedence, which are generally the same as those in algebra: 8
Ex 1-4: Determine arithmetic mean (average) of five terms. Note: If the parentheses are omitted, we obtain a + b + c + d + e / 5, which evaluates as: Ex 1-5: Convert the following algebraic expression to C++ expression and indicate the order in which C++ applies the operators. Q1-2: Convert the second-degree polynomial y = ax 2 + bx + c to C++ expression and indicate the order in which C++ applies the operators. Ex 1-6: Write a C++ program to calculate the sum and average of three integer numbers. 9
Note: The variables sum and average are data of type float, meaning that these variables will hold floating point values. Ex 1-7: Write a C++ program to calculate the circumference of a circle. Note: Constant variables can be declared either: const int S=179; const float PI=3.14; or #define S 179 #define PI 3.14 10