SME 1013 Programming for Engineers Programming Concepts by: Mohamed Hussein compliments to: Assoc. Prof. Dr. Mohd Shafiek Yaacob Stages of Program Development Logic errors Input? Formula? Special Cases? Output? Step 1 Step 2 Branching? Looping? Stop? Fortran? C/C++? BASIC? Java? Syntax errors Devide by Zero? Formula? Special Cases? Output? Program Concept Algorithm Development Program Coding Debuging Experienced programmers spend up to 90% of their time working on the logic of their program, not on the coding 1
What are these? Building blocks of the game of TETRIS Building Blocks of a Program Sequential Executions - instructions are performed one after the other Branching Operations - a decision is made to perform one block of instructions or another. Looping Operations - a block of instructions is repeated Only these three basic building blocks are needed to develop a solution to a problem. 2
Some Definitions Algorithm - a step-by-step procedure for finding the solution to a problem. - the idea of how a problem can be solved. Pseudocode - a free way or style of how an algorithm is written. - a hybrid mixture of a particular programming language and English. Flowchart - a way to describe algorithms graphically. - requires correct use of symbols. Common Flowchart Symbols or stop computation Input or output subprocess direction of flow connection branching looping 3
Sequential Execution of Instructions Sequential instructions are executed one after the other. The computer begins with the first instruction. The result from current and previous calculations may be used in the subsequent calculation. The instructions must be executed in correct order. Example 1.1 Construct an algorithm and a flowchart to compute the weight w of a hollow sphere of outside diameter d, wall thickness t, and density ρ. 4
Solution 1.1 (Algorithm) 1. Compute the inner and outer radii r o = d/2 and r i = d/2 t 2. Compute the volume v = 4π(r o3 r i3 )/3 3. Compute the weight w = ρv Solution 1.1 (Flowchart) r o = d/2 r i = d/2 t v = 4π(r o3 r i3 )/3 w = ρv stop 5
Example 1.2 Construct an algorithm and a flowchart to compute the weight w of a hollow sphere of outside diameter d, wall thickness t, and density ρ. Allow for input of the values of diameter d, wall thickness t, and density ρ. Also, allow for output of the values of volume v and weight w. Solution 1.2 (Algorithm) 1. Input diameter, thickness, and density 2. Compute the inner and outer radii 3. Compute the volume 4. Compute the weight 5. volume and weight 6
Solution 1.2 Flowchart Input d, t, ρ r o = d/2 r i = d/2 t v = 4π(r o3 r i3 )/3 w = ρv v, w stop Branching Operations A branch is a point in the program where the computer will make a decision about which set of instructions to be executed next, A decision is made depending on the answer of a posed question. The question must be formulated such that it has a simple answer and has only one possible outcome (e.g. yes or no).? 7
Example 1.5 Construct an algorithm and a flowchart to determine if a point (x, y) lies within a circle of radius r centered at the origin. If the point lies within the circle, print out a message and the distance, z, of that point from the center of the circle. Solution 1.5 (Algorithm) 1. Input the radius r and the (x, y) coordinate 2. Compute the distance z = (x 2 + y 2 ) 1/2 of the point (x, y) from the origin. 3. Is z < r? If yes, then print inside and print z If no, then print outside End of branch 8
Solution 1.5 (Flowchart) Input r, x, y z = (x 2 + y 2 ) 1/2 yes inside, z z < r? no outside Stop Nesting Branching Operations A nested branch is used when the computer needs to choose between more than two alternatives.? Example 1.6 Construct a flowchart to see if an integer n is negative, positive, or zero.? 9
Solution 1.6 (Flowchart) Input n no n < 0? yes no n = 0? yes Negative Positive Zero Stop Loops A counted loop repeats the executions for a predetermine number of times. A conditional loop repeats the executions until a condition is satisfied. Instructions Count? Condition? yes no 10
Counted Loops Most widely used in computer programming. Loop control variable LCV is used to control the loop. The loop stops when the LCV exceeds stop value Number of iterations is predetermine finished value stop value step value body of loop LCV name loop back Example 1.8 Construct a flowchart to print out the numbers from 1 to 100 and their squares. 11
Solution 1.8 (Flowchart) =1 stop=100 step=1 LCV LCV, LCV 2 Stop Example 1.9 Construct a flowchart to print out the even numbers from 1 to 100 and their squares. 12
Solution 1.9 (Flowchart) =2 stop=100 step=2 LCV LCV, LCV 2 Stop Conditional Loops The most common type is termed the while loop. A set of instructions is repeated while some condition is true. yes Finished? no body of loop 13
Example 1.10 Construct a flowchart to read in a series of numbers and keep track of the running total and the number of data items. Stop reading in the numbers when one of them has a value of zero (sentinel value). Then compute the average of all the numbers and report it. Solution 1.10 (Flowchart) SUM=0 COUNT=0 Input X no AVG=SUM/COUNT X 0? yes SUM=SUM+X COUNT=COUNT+1 AVG, COUNT stop 14
Nested Loops A counted loop is placed within another counted loop or a combination of different loops within each other. Example 1.11 Construct an algorithm and flowchart to create a 10 by 10 multiplication table such as 1 x 1 = 1, 1 x 2 = 2, and so forth. Solution 1.11 (Flowchart) Stop =1 stop=10 step=1 LCV1 =1 stop=10 step=1 LCV2 Product = LCV1 x LCV2 LCV1, LCV2, Product 15
Procedures A large program is often broken into several smaller components known as procedures or modules. Modules or procedures may be reuse again in the same program or in another program. Subprocess Example 1/13 Construct a flowchart to read an integer and evaluate its factorial. Use the subprocess to calculate the factorial. n! = 1 x 2 x x (n-1) x n 16
Solution 1/13 (Flowchart) Input n Y=Factorial(n) n, Y Factorial Y = 1 =1 stop=n step=1 LCV Y = Y x LCV Stop Return 17