Unit2Introducti

Download Report

Transcript Unit2Introducti

UNIT 2. Introduction to Computer Programming

UNIT 2. Introduction to Computer Programming

Algorithm & Flowcharting

COM E 211: Basic Computer Programming

Algorithm & Flowcharting

Algorithm

 A list of instruction that carries out a process step by step 

Flowchart

 A diagram representing the logical sequence in which a combination of steps or operations is to be performed   Is usually a visual representation of an algorithm Types of Flowchart:  Program Flowchart • Describes graphically in detail the logical operations & steps within a program  System Flowchart • A graphic representation of procedures involved in converting data or input to data in output form.

COM E 211: Basic Computer Programming

 Advantages of Flowchart  Language independent  Enforces user to give attention to significant matters  Provides an alternative to the usual narrative description  Easier to understand  Limitations  Do not represent a programming language  Can’t be viewed as a natural means of communication  Certain details often require a long sequence of interconnected symbols  Does not convey why a given set of operations is made  Do not highlight the important detail since each step receives as much attention in a flowchart as any other.

COM E 211: Basic Computer Programming

Flowcharting Symbols

 Parallelogram --- Input / Output  Rectangle --- Processing  Diamond --- Decision  Small Circle --- On-page Connector  Pentagon --- Off-page Connector  Arrow Heads --- Flow Direction Indicators  Horizontal / Vertical Lines --- Flow Lines  Oval --- Terminal COM E 211: Basic Computer Programming

Examples for Sequential

 The radius of a circle is equal to 1 unit. Make an algorithm and draw a flowchart to compute the corresponding area of the circle and print out the value of the radius and the area.

 Given three(3) numbers A,B and C. Make an algorithm and draw a flowchart to compute and print out the sum, the average and the product of these values.

COM E 211: Basic Computer Programming

Examples for Branching

 The ABC manufacturing company plans to give a year end bonus to each of its employees. Make an algorithm and draw a flowchart which will compute the bonus of an employee. Consider the following criteria: If the employee’s monthly salary is less than 1,000, the bonus is 50% of the salary. If the salary is grater than 1,000, the bonus is 1,000. Print out the name and corresponding bonus of the employee.

 Given two (2) numbers, x and y. Make an algorithm and draw a flowchart to determine the difference between x and y. If x-y is negative, compute R=x+y; if x-y is zero, compute R=2x+2y; and if x-y is positive, compute R=x*y. Ptint out the values, x, y and R.

COM E 211: Basic Computer Programming

Loops and Counter

Looping

 Used when it is desired to make the same calculation of more than one set of data. It consists of repeating a program, or a section of program and substituting new data for each repetition.

Counter

 Is a setup in a program loop to keep track of a number of times the program segment is repeated. The program can then be terminated after the completion of a predetermined number of passes.

 Steps in Loop Control  Initialization  The value of counter is initially set equal to zero or one.

 Test for limit condition  Before the logic flow gets out of a loop, a loop terminating condition must first be satisfied.

 Incrementation  Often each loop is executed, 1 is added to the counter. Thus counter reflects the number of times the operation has been performed.

COM E 211: Basic Computer Programming

Examples for Looping

 The initial value of the radius of a circle is equal to one unit and each succeeding radius is one unit greater than the values before it. Make an algorithm and draw a flowchart to compute the area of the circle starting with R=1.0 up to R=5.0, then print out each radius and corresponding area of the circle.

 Given a set of five (5) numbers that includes both positive and negative data values. Make an algorithm and draw a flowchart to read in these values one at a time and count the number of positive values (including zero value) and the number of negative values found in the set. After the required values are determined, print out these counted values.

COM E 211: Basic Computer Programming