Presentation is loading. Please wait.

Presentation is loading. Please wait.

| MSC 8102:PROGRAMMING CONCEPTS By Vincent Omwenga, PhD. 1.

Similar presentations


Presentation on theme: "| MSC 8102:PROGRAMMING CONCEPTS By Vincent Omwenga, PhD. 1."— Presentation transcript:

1 | MSC 8102:PROGRAMMING CONCEPTS By Vincent Omwenga, PhD. 1

2 | Topic 1: Introduction to Structured Programming Concepts Agenda To describe basic problem-solving techniques. To develop algorithms through the process of top-down/stepwise refinement. To write a basic Pseudo code To describe the Syntax used in C Explain the Semantics used in C Describe the various Data types Differentiate between an expressions and a Statements Write a basic Input and output statements 2

3 | 3 Introduction Before writing a program: – Have a thorough understanding of the problem – Carefully plan an approach for solving it While writing a program: – Know what “building blocks” are available – Use good programming principles Carry out a problem analysis – This involves understanding the problem and clarifying: What data is given or input (available for use when the problem-solving takes place) What results are required What assumptions are safe to make What constraints must be obeyed

4 | Basic problem-solving techniques Top Down Design Model/Approach – In top-down model, an overview of the system is formulated, without going into detail for any part of it. – Each part of the system is then refined in more details. – Each new part may then be refined again, defining it in yet more details until the entire specification is detailed enough to validate the model. – It basically refers to successive refinement of the problem (task) into sub problems (subtasks). – Refinement is applied until we reach to the stage where the subtasks can be directly carried out. 15-4

5 | Top Down Design subtask1subtask2 subtask3 Main Task

6 | Basic problem-solving techniques Bottom-up Design Approach – In bottom-up design individual parts of the system are specified in details. – The parts are then linked together to form larger components, which are in turn linked until a complete system is formed. – Object-oriented languages such as C++ or JAVA use bottom-up approach where each object is identified first. 15-6

7 | Bottom up Design

8 | 8 Algorithms Computing problems – All can be solved by executing a series of actions in a specific order Algorithm: It is a finite set of instructions which, if followed accomplish a particular task. – It is basically used to describe a problem solving method suitable for implementation as a computer program. – Algorithm is independent of the machine and language used for implementation.

9 | 9 Characteristics of an Algorithm Input – Zero or more quantities are supplied externally Output – At least one quantity is produced Definiteness – Each instruction is clear & unambiguous Finiteness – It terminates after finite steps Effectiveness – Each instruction is simple to be carried out manually. An “unambiguous specification of a method” Is characterized by: – Ordered sequence of well-defined, effective operations that, when executed, will produce a result after “terminating” within a finite no of steps

10 | 10 Pseudocode – Artificial, informal language that helps us develop algorithms – Similar to everyday English – Not actually executed on computers – Helps us “think out” a program before writing it Easy to convert into a corresponding C++ program Consists only of executable statements

11 | Pseudocode & Algorithm Example 1: Write an algorithm to determine a student’s final grade and indicate whether it is passing or failing. The final grade is calculated as the average of four marks.

12 | Pseudocode & Algorithm Pseudocode: Input a set of 4 marks Calculate their average by summing and dividing by 4 if average is below 60 Print “FAIL” else Print “PASS”

13 | Pseudocode & Algorithm Detailed Algorithm Step 1: Input M1,M2,M3,M4 Step 2: GRADE  (M1+M2+M3+M4)/4 Step 3: if (GRADE < 60) then Print “FAIL” else Print “PASS” endif

14 | The Flowchart A Flowchart is another algorithm but graphical. – shows logic solution – emphasizes individual steps and their interconnections – A flowchart must have a start and stop – A steps in a flowchart must connect. Can’t leave a step “hanging” with no connection. e.g. control flow from one action to the next

15 | Flowchart Symbols General Used Symbols

16 | Example PRINT “PASS” Step 1: Input M1,M2,M3,M4 Step 2: GRADE  (M1+M2+M3+M4)/4 Step 3: if (GRADE < 60) then Print “FAIL” else Print “PASS” endif START Input M1,M2,M3,M4 GRADE  (M1+M2+M3+M4)/4 IS GRADE<6 0 PRINT “FAIL” STOP Y N

17 | Example 2 Write an algorithm and draw a flowchart to convert the length in feet to centimeter. Pseudocode: Input the length in feet (Lft) Calculate the length in cm (Lcm) by multiplying LFT with 30 Print length in cm (LCM)

18 | Example 2 Algorithm Step 1: Input Lft Step 2: Lcm  Lft x 30 Step 3: Print Lcm START Input Lft Lcm  Lft x 30 Print Lcm STOP Flowchart

19 | Example 3 Write an algorithm and draw a flowchart that will read the two sides of a rectangle and calculate its area. Pseudocode Input the width (W) and Length (L) of a rectangle Calculate the area (A) by multiplying L with W Print A

20 | Example 3 Algorithm Step 1: Input W,L Step 2: A  L x W Step 3: Print A START Input W, L A  L x W Print A STOP

21 | Example 4 Write an algorithm and draw a flowchart that will calculate the roots of a quadratic equation Hint: d = sqrt ( ), and the roots are: x1 = (–b + d)/2a and x2 = (–b – d)/2a

22 | Example 4 Pseudocode: Input the coefficients (a, b, c) of the quadratic equation Calculate d Calculate x1 Calculate x2 Print x1 and x2

23 | Example 4 Algorithm: Step 1: Input a, b, c Step 2: d  sqrt ( ) Step 3: x1  (–b + d) / (2 x a) Step 4: x2  (–b – d) / (2 x a) Step 5: Print x1, x2 START Input a, b, c d  sqrt(b x b – 4 x a x c) Print x 1,x 2 STOP x 1  (–b + d) / (2 x a) X 2  (–b – d) / (2 x a)


Download ppt "| MSC 8102:PROGRAMMING CONCEPTS By Vincent Omwenga, PhD. 1."

Similar presentations


Ads by Google