CS101: INTRODUCTION TO COMPUTER PROGRAMMING LECTURE-1: INTRODUCTION TO PROBLEM SOLVING - CS002:...

45
CS101: INTRODUCTION TO COMPUTER PROGRAMMING LECTURE-1: INTRODUCTION TO PROBLEM SOLVING - CS002: REVIEW -

Transcript of CS101: INTRODUCTION TO COMPUTER PROGRAMMING LECTURE-1: INTRODUCTION TO PROBLEM SOLVING - CS002:...

Page 1: CS101: INTRODUCTION TO COMPUTER PROGRAMMING LECTURE-1: INTRODUCTION TO PROBLEM SOLVING - CS002: REVIEW -

CS101: INTRODUCTION TO COMPUTER PROGRAMMING

LECTURE-1: INTRODUCTION TO PROBLEM SOLVING

- CS002: REVIEW -

Page 2: CS101: INTRODUCTION TO COMPUTER PROGRAMMING LECTURE-1: INTRODUCTION TO PROBLEM SOLVING - CS002: REVIEW -

Outline

In this chapter you will learn about: Introduction to Problem Solving Software Development Method (SDM)

1. Specification of needs2. Problem analysis3. Design algorithm4. Implementation5. Testing and verification6. Documentation

Page 3: CS101: INTRODUCTION TO COMPUTER PROGRAMMING LECTURE-1: INTRODUCTION TO PROBLEM SOLVING - CS002: REVIEW -

Introduction to Problem Solving Problem solving is the process of

transforming the description of a problem into a solution by using our knowledge of the problem domain and by relying on our ability to select and use appropriate problem-solving strategies, techniques and tools.

Computers can be used to help us solving problems

Page 4: CS101: INTRODUCTION TO COMPUTER PROGRAMMING LECTURE-1: INTRODUCTION TO PROBLEM SOLVING - CS002: REVIEW -

Software Development Method (SDM)

Is a framework that is used to structure, plan, and control the process of developing an information system, which include the following steps:1.Specification of needs2.Problem analysis3.Design algorithm4.Implementation5.Testing and verification6.Documentation

Page 5: CS101: INTRODUCTION TO COMPUTER PROGRAMMING LECTURE-1: INTRODUCTION TO PROBLEM SOLVING - CS002: REVIEW -

Example (Problem)

Lets have the following problem:Design a program to

compute the car parking fee base on these rates: first 2 hours = SR 1 per hour, the following hours is SR 2 per

hour.

Design a program to compute the car parking fee base on these rates: first 2 hours = SR 1 per hour, the following hours is SR 2 per

hour.

Page 6: CS101: INTRODUCTION TO COMPUTER PROGRAMMING LECTURE-1: INTRODUCTION TO PROBLEM SOLVING - CS002: REVIEW -

Software Development Method (SDM)

1. Specification of needs2. Problem analysis3. Design algorithm

4. Implementation5. Testing and verification6. Documentation

Page 7: CS101: INTRODUCTION TO COMPUTER PROGRAMMING LECTURE-1: INTRODUCTION TO PROBLEM SOLVING - CS002: REVIEW -

Step-1: Specification of Needs To understand exactly:

what the problem is what is needed to solve it what the solution should provide if there are constraints and special

conditions.

Page 8: CS101: INTRODUCTION TO COMPUTER PROGRAMMING LECTURE-1: INTRODUCTION TO PROBLEM SOLVING - CS002: REVIEW -

Specification of needs (Example) What the problem is

Need to calculate the parking fee What is needed to solve it

Total time What the solution should provide

The parking fee If there are constraints and special

conditions.None

Page 9: CS101: INTRODUCTION TO COMPUTER PROGRAMMING LECTURE-1: INTRODUCTION TO PROBLEM SOLVING - CS002: REVIEW -

Software Development Method (SDM)

1. Specification of needs2. Problem analysis3. Design algorithm

4. Implementation5. Testing and verification6. Documentation

Page 10: CS101: INTRODUCTION TO COMPUTER PROGRAMMING LECTURE-1: INTRODUCTION TO PROBLEM SOLVING - CS002: REVIEW -

Step-2: Problem Analysis

In the analysis phase, we should identify the following: Inputs to the problem, their form and the

input media to be used Outputs expected from the problem, their

form and the output media to be used Special constraints or conditions (if any) Formulas or equations to be used

Page 11: CS101: INTRODUCTION TO COMPUTER PROGRAMMING LECTURE-1: INTRODUCTION TO PROBLEM SOLVING - CS002: REVIEW -

Problem analysis (Example)

Input:Total time (time)

Output:Parking fees (fees)

Formula:If time <= 2, fees = timeIf time > 2, fees = [(time – 2)X2] +2

ConstraintNone

Page 12: CS101: INTRODUCTION TO COMPUTER PROGRAMMING LECTURE-1: INTRODUCTION TO PROBLEM SOLVING - CS002: REVIEW -

Software Development Method (SDM)

1. Specification of needs2. Problem analysis3. Design algorithm

4. Implementation5. Testing and verification6. Documentation

Page 13: CS101: INTRODUCTION TO COMPUTER PROGRAMMING LECTURE-1: INTRODUCTION TO PROBLEM SOLVING - CS002: REVIEW -

Step-3: Design algorithm

An algorithm is a sequence of a finite number of steps arranged in a specific logical order which, when executed, produces the solution for a problem.

An algorithm must satisfy these requirements: It may have an input(s) It must have an output It should not be ambiguous (there should

not be different interpretations to it)

Page 14: CS101: INTRODUCTION TO COMPUTER PROGRAMMING LECTURE-1: INTRODUCTION TO PROBLEM SOLVING - CS002: REVIEW -

14

Control Structures

In 1966, two researchers, C. Bohn and G. Jacopini, demonstrated that any algorithm can be described using only 3 control structures: Sequence Selection Repetition

Called control structures or logic structures

Page 15: CS101: INTRODUCTION TO COMPUTER PROGRAMMING LECTURE-1: INTRODUCTION TO PROBLEM SOLVING - CS002: REVIEW -

15

The Sequence Structure

The sequence structure directs the computer to process the program instructions, one after another, in the order listed in the program

Page 16: CS101: INTRODUCTION TO COMPUTER PROGRAMMING LECTURE-1: INTRODUCTION TO PROBLEM SOLVING - CS002: REVIEW -

The Sequence Structure (continued)

16

Page 17: CS101: INTRODUCTION TO COMPUTER PROGRAMMING LECTURE-1: INTRODUCTION TO PROBLEM SOLVING - CS002: REVIEW -

17

The Selection Structure

Selection structure: makes a decision and then takes an appropriate action based on that decision Also called the decision structure

Page 18: CS101: INTRODUCTION TO COMPUTER PROGRAMMING LECTURE-1: INTRODUCTION TO PROBLEM SOLVING - CS002: REVIEW -

18

The Selection Structure (continued)

Page 19: CS101: INTRODUCTION TO COMPUTER PROGRAMMING LECTURE-1: INTRODUCTION TO PROBLEM SOLVING - CS002: REVIEW -

19

The Repetition Structure

Repetition structure: directs computer to repeat one or more instructions until some condition is met Also called a loop or iteration

Page 20: CS101: INTRODUCTION TO COMPUTER PROGRAMMING LECTURE-1: INTRODUCTION TO PROBLEM SOLVING - CS002: REVIEW -

The Repetition Structure (continued)

20

Page 21: CS101: INTRODUCTION TO COMPUTER PROGRAMMING LECTURE-1: INTRODUCTION TO PROBLEM SOLVING - CS002: REVIEW -

21

The Repetition Structure (continued) What could you do if you don’t know

precisely how many steps separate Rob from the chair?

Page 22: CS101: INTRODUCTION TO COMPUTER PROGRAMMING LECTURE-1: INTRODUCTION TO PROBLEM SOLVING - CS002: REVIEW -

The Repetition Structure (continued)

22

Page 23: CS101: INTRODUCTION TO COMPUTER PROGRAMMING LECTURE-1: INTRODUCTION TO PROBLEM SOLVING - CS002: REVIEW -

Design Algorithm

An algorithm can be represented using pseudocodes or flowcharts:

1.1. Pseudocode: Pseudocode: is a semiformal, English-like language with limited vocabulary that can be used to design and describe algorithms.

2.2. Flowcharts:Flowcharts: is a graph used to depict or show a step by step solution using symbols which represent a task

prepared by NI, edited by MAF

Page 24: CS101: INTRODUCTION TO COMPUTER PROGRAMMING LECTURE-1: INTRODUCTION TO PROBLEM SOLVING - CS002: REVIEW -

Pseudocodes: The Sequence control structure

A series of steps or statements that are executed in the order they are written in an algorithm.

The beginning and end of a block of statements can be optionally marked with the keywords begin and end.

Example-1:

Begin

Read the birth date from the user.

Calculate the difference between the birth date and today’s date.

Print the user age.

End

Page 25: CS101: INTRODUCTION TO COMPUTER PROGRAMMING LECTURE-1: INTRODUCTION TO PROBLEM SOLVING - CS002: REVIEW -

Flowchart – example-1

Begin

Read birth date

CalculateAge = current year – birth date

Displayage

End

Flowchart Symbols

Terminal symbol - indicates the beginning and end points of an algorithm.

Process symbol - shows an instruction other than input, output or selection.

Input-output symbol - shows an input or an output operation.

Selection symbol - shows a selection processfor two-way selection.

Flow lines - indicate the logical sequence ofexecution steps in the algorithm.

Page 26: CS101: INTRODUCTION TO COMPUTER PROGRAMMING LECTURE-1: INTRODUCTION TO PROBLEM SOLVING - CS002: REVIEW -

Pseudocodes: The Selection control structure

Defines two courses of action depending on the outcome of a condition. A condition is an expression that is, when computed, evaluated to either true or false.

The keyword used are if and else.

Format:if condition

then-part

else

else-part

end_if

Example-2:

if age is greater than 55print “Retire”

elseprint “Work Work Work”

end_if

Page 27: CS101: INTRODUCTION TO COMPUTER PROGRAMMING LECTURE-1: INTRODUCTION TO PROBLEM SOLVING - CS002: REVIEW -

Flowchart – example-2

Begin

Read age

End

Age > 55? NOYES

print “retired” print “keep working”

Page 28: CS101: INTRODUCTION TO COMPUTER PROGRAMMING LECTURE-1: INTRODUCTION TO PROBLEM SOLVING - CS002: REVIEW -

Pseudocodes: The Repetition control structure

Specifies a block of one or more statements that are repeatedly executed until a condition is satisfied.

Example-3: Summing up 1 to 10set cumulative sum to 0

set current number to 1

while current number is less or equal to 10

add the cumulative sum to current number

add 1 to current number

end_while

print the value of cumulative sum

Page 29: CS101: INTRODUCTION TO COMPUTER PROGRAMMING LECTURE-1: INTRODUCTION TO PROBLEM SOLVING - CS002: REVIEW -

Flowchart – example 3

Begin

End

current_number <= 10?NO

YES

sum = 0current_number = 1

sum = sum + current_numbercurrent_number = current_number + 1

print sum

Page 30: CS101: INTRODUCTION TO COMPUTER PROGRAMMING LECTURE-1: INTRODUCTION TO PROBLEM SOLVING - CS002: REVIEW -

Our Problem Algorithm (Pseudocode )

Page 31: CS101: INTRODUCTION TO COMPUTER PROGRAMMING LECTURE-1: INTRODUCTION TO PROBLEM SOLVING - CS002: REVIEW -

Algorithm (Flowchart)

Page 32: CS101: INTRODUCTION TO COMPUTER PROGRAMMING LECTURE-1: INTRODUCTION TO PROBLEM SOLVING - CS002: REVIEW -

Software Development Method (SDM)

1. Specification of needs2. Problem analysis3. Design algorithm4. Implementation5. Testing and verification6. Documentation

Page 33: CS101: INTRODUCTION TO COMPUTER PROGRAMMING LECTURE-1: INTRODUCTION TO PROBLEM SOLVING - CS002: REVIEW -

Step-4: Implementation

The process of implementing an algorithm by writing a computer program using a programming language (for example, using C language)

The output of the program must be the solution of the intended problem

The program must not do anything that it is not supposed to do (Think of those many viruses, buffer overflows,

trojan horses, etc. that we experience almost daily. All these result from programs doing more than they were intended to do)

Page 34: CS101: INTRODUCTION TO COMPUTER PROGRAMMING LECTURE-1: INTRODUCTION TO PROBLEM SOLVING - CS002: REVIEW -

Implementation (from pseudocodes)

Page 35: CS101: INTRODUCTION TO COMPUTER PROGRAMMING LECTURE-1: INTRODUCTION TO PROBLEM SOLVING - CS002: REVIEW -

Implementation (from flowchart)

Page 36: CS101: INTRODUCTION TO COMPUTER PROGRAMMING LECTURE-1: INTRODUCTION TO PROBLEM SOLVING - CS002: REVIEW -

Software Development Method (SDM)

1. Specification of needs2. Problem analysis3. Design algorithm4. Implementation5. Testing and verification6. Documentation

Page 37: CS101: INTRODUCTION TO COMPUTER PROGRAMMING LECTURE-1: INTRODUCTION TO PROBLEM SOLVING - CS002: REVIEW -

Step-5: Testing and Verification Program testing is the process of executing

a program to demonstrate its correctness Program verification is the process of

ensuring that a program meets user-requirement

After the program is compiled, we must run the program and test/verify it with different inputs before the program can be released to the public or other users (or to the instructor of this class)

Page 38: CS101: INTRODUCTION TO COMPUTER PROGRAMMING LECTURE-1: INTRODUCTION TO PROBLEM SOLVING - CS002: REVIEW -

Testing and Verification (Syntax)

Page 39: CS101: INTRODUCTION TO COMPUTER PROGRAMMING LECTURE-1: INTRODUCTION TO PROBLEM SOLVING - CS002: REVIEW -

Testing and Verification (Syntax)

Page 40: CS101: INTRODUCTION TO COMPUTER PROGRAMMING LECTURE-1: INTRODUCTION TO PROBLEM SOLVING - CS002: REVIEW -

Testing and Verification(Values)

Page 41: CS101: INTRODUCTION TO COMPUTER PROGRAMMING LECTURE-1: INTRODUCTION TO PROBLEM SOLVING - CS002: REVIEW -

Software Development Method (SDM)

1. Specification of needs2. Problem analysis3. Design algorithm4. Implementation5. Testing and verification6. Documentation

Page 42: CS101: INTRODUCTION TO COMPUTER PROGRAMMING LECTURE-1: INTRODUCTION TO PROBLEM SOLVING - CS002: REVIEW -

Step-6: Documentation

Contains details produced at all stages of the program development cycle.

Can be done in 2 ways: Writing comments between your line of codes Creating a separate text file to explain the

program Important not only for other people to use or

modify your program, but also for you to understand your own program after a long time (believe me, you will forget the details of your own program after some time ...)

Page 43: CS101: INTRODUCTION TO COMPUTER PROGRAMMING LECTURE-1: INTRODUCTION TO PROBLEM SOLVING - CS002: REVIEW -

Documentation

Page 44: CS101: INTRODUCTION TO COMPUTER PROGRAMMING LECTURE-1: INTRODUCTION TO PROBLEM SOLVING - CS002: REVIEW -

Documentation

Page 45: CS101: INTRODUCTION TO COMPUTER PROGRAMMING LECTURE-1: INTRODUCTION TO PROBLEM SOLVING - CS002: REVIEW -

Summary

This chapter introduced the concept of problem solving-a process of transforming the description of a problem into a solution.

A commonly used method – SDM which consists of 6 steps

3 basic control structures : sequence, selection and repetition structures

Pseudocode vs. Flow chart