CS101: INTRODUCTION TO COMPUTER PROGRAMMING LECTURE-1: INTRODUCTION TO PROBLEM SOLVING - CS002:...
-
Upload
cadence-wragge -
Category
Documents
-
view
225 -
download
2
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 -](https://reader035.fdocuments.net/reader035/viewer/2022062511/551b0c275503462e578b5855/html5/thumbnails/1.jpg)
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 -](https://reader035.fdocuments.net/reader035/viewer/2022062511/551b0c275503462e578b5855/html5/thumbnails/2.jpg)
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 -](https://reader035.fdocuments.net/reader035/viewer/2022062511/551b0c275503462e578b5855/html5/thumbnails/3.jpg)
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 -](https://reader035.fdocuments.net/reader035/viewer/2022062511/551b0c275503462e578b5855/html5/thumbnails/4.jpg)
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 -](https://reader035.fdocuments.net/reader035/viewer/2022062511/551b0c275503462e578b5855/html5/thumbnails/5.jpg)
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 -](https://reader035.fdocuments.net/reader035/viewer/2022062511/551b0c275503462e578b5855/html5/thumbnails/6.jpg)
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 -](https://reader035.fdocuments.net/reader035/viewer/2022062511/551b0c275503462e578b5855/html5/thumbnails/7.jpg)
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 -](https://reader035.fdocuments.net/reader035/viewer/2022062511/551b0c275503462e578b5855/html5/thumbnails/8.jpg)
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 -](https://reader035.fdocuments.net/reader035/viewer/2022062511/551b0c275503462e578b5855/html5/thumbnails/9.jpg)
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 -](https://reader035.fdocuments.net/reader035/viewer/2022062511/551b0c275503462e578b5855/html5/thumbnails/10.jpg)
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 -](https://reader035.fdocuments.net/reader035/viewer/2022062511/551b0c275503462e578b5855/html5/thumbnails/11.jpg)
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 -](https://reader035.fdocuments.net/reader035/viewer/2022062511/551b0c275503462e578b5855/html5/thumbnails/12.jpg)
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 -](https://reader035.fdocuments.net/reader035/viewer/2022062511/551b0c275503462e578b5855/html5/thumbnails/13.jpg)
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 -](https://reader035.fdocuments.net/reader035/viewer/2022062511/551b0c275503462e578b5855/html5/thumbnails/14.jpg)
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 -](https://reader035.fdocuments.net/reader035/viewer/2022062511/551b0c275503462e578b5855/html5/thumbnails/15.jpg)
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 -](https://reader035.fdocuments.net/reader035/viewer/2022062511/551b0c275503462e578b5855/html5/thumbnails/16.jpg)
The Sequence Structure (continued)
16
![Page 17: CS101: INTRODUCTION TO COMPUTER PROGRAMMING LECTURE-1: INTRODUCTION TO PROBLEM SOLVING - CS002: REVIEW -](https://reader035.fdocuments.net/reader035/viewer/2022062511/551b0c275503462e578b5855/html5/thumbnails/17.jpg)
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 -](https://reader035.fdocuments.net/reader035/viewer/2022062511/551b0c275503462e578b5855/html5/thumbnails/18.jpg)
18
The Selection Structure (continued)
![Page 19: CS101: INTRODUCTION TO COMPUTER PROGRAMMING LECTURE-1: INTRODUCTION TO PROBLEM SOLVING - CS002: REVIEW -](https://reader035.fdocuments.net/reader035/viewer/2022062511/551b0c275503462e578b5855/html5/thumbnails/19.jpg)
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 -](https://reader035.fdocuments.net/reader035/viewer/2022062511/551b0c275503462e578b5855/html5/thumbnails/20.jpg)
The Repetition Structure (continued)
20
![Page 21: CS101: INTRODUCTION TO COMPUTER PROGRAMMING LECTURE-1: INTRODUCTION TO PROBLEM SOLVING - CS002: REVIEW -](https://reader035.fdocuments.net/reader035/viewer/2022062511/551b0c275503462e578b5855/html5/thumbnails/21.jpg)
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 -](https://reader035.fdocuments.net/reader035/viewer/2022062511/551b0c275503462e578b5855/html5/thumbnails/22.jpg)
The Repetition Structure (continued)
22
![Page 23: CS101: INTRODUCTION TO COMPUTER PROGRAMMING LECTURE-1: INTRODUCTION TO PROBLEM SOLVING - CS002: REVIEW -](https://reader035.fdocuments.net/reader035/viewer/2022062511/551b0c275503462e578b5855/html5/thumbnails/23.jpg)
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 -](https://reader035.fdocuments.net/reader035/viewer/2022062511/551b0c275503462e578b5855/html5/thumbnails/24.jpg)
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 -](https://reader035.fdocuments.net/reader035/viewer/2022062511/551b0c275503462e578b5855/html5/thumbnails/25.jpg)
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 -](https://reader035.fdocuments.net/reader035/viewer/2022062511/551b0c275503462e578b5855/html5/thumbnails/26.jpg)
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 -](https://reader035.fdocuments.net/reader035/viewer/2022062511/551b0c275503462e578b5855/html5/thumbnails/27.jpg)
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 -](https://reader035.fdocuments.net/reader035/viewer/2022062511/551b0c275503462e578b5855/html5/thumbnails/28.jpg)
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 -](https://reader035.fdocuments.net/reader035/viewer/2022062511/551b0c275503462e578b5855/html5/thumbnails/29.jpg)
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 -](https://reader035.fdocuments.net/reader035/viewer/2022062511/551b0c275503462e578b5855/html5/thumbnails/30.jpg)
Our Problem Algorithm (Pseudocode )
![Page 31: CS101: INTRODUCTION TO COMPUTER PROGRAMMING LECTURE-1: INTRODUCTION TO PROBLEM SOLVING - CS002: REVIEW -](https://reader035.fdocuments.net/reader035/viewer/2022062511/551b0c275503462e578b5855/html5/thumbnails/31.jpg)
Algorithm (Flowchart)
![Page 32: CS101: INTRODUCTION TO COMPUTER PROGRAMMING LECTURE-1: INTRODUCTION TO PROBLEM SOLVING - CS002: REVIEW -](https://reader035.fdocuments.net/reader035/viewer/2022062511/551b0c275503462e578b5855/html5/thumbnails/32.jpg)
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 -](https://reader035.fdocuments.net/reader035/viewer/2022062511/551b0c275503462e578b5855/html5/thumbnails/33.jpg)
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 -](https://reader035.fdocuments.net/reader035/viewer/2022062511/551b0c275503462e578b5855/html5/thumbnails/34.jpg)
Implementation (from pseudocodes)
![Page 35: CS101: INTRODUCTION TO COMPUTER PROGRAMMING LECTURE-1: INTRODUCTION TO PROBLEM SOLVING - CS002: REVIEW -](https://reader035.fdocuments.net/reader035/viewer/2022062511/551b0c275503462e578b5855/html5/thumbnails/35.jpg)
Implementation (from flowchart)
![Page 36: CS101: INTRODUCTION TO COMPUTER PROGRAMMING LECTURE-1: INTRODUCTION TO PROBLEM SOLVING - CS002: REVIEW -](https://reader035.fdocuments.net/reader035/viewer/2022062511/551b0c275503462e578b5855/html5/thumbnails/36.jpg)
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 -](https://reader035.fdocuments.net/reader035/viewer/2022062511/551b0c275503462e578b5855/html5/thumbnails/37.jpg)
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 -](https://reader035.fdocuments.net/reader035/viewer/2022062511/551b0c275503462e578b5855/html5/thumbnails/38.jpg)
Testing and Verification (Syntax)
![Page 39: CS101: INTRODUCTION TO COMPUTER PROGRAMMING LECTURE-1: INTRODUCTION TO PROBLEM SOLVING - CS002: REVIEW -](https://reader035.fdocuments.net/reader035/viewer/2022062511/551b0c275503462e578b5855/html5/thumbnails/39.jpg)
Testing and Verification (Syntax)
![Page 40: CS101: INTRODUCTION TO COMPUTER PROGRAMMING LECTURE-1: INTRODUCTION TO PROBLEM SOLVING - CS002: REVIEW -](https://reader035.fdocuments.net/reader035/viewer/2022062511/551b0c275503462e578b5855/html5/thumbnails/40.jpg)
Testing and Verification(Values)
![Page 41: CS101: INTRODUCTION TO COMPUTER PROGRAMMING LECTURE-1: INTRODUCTION TO PROBLEM SOLVING - CS002: REVIEW -](https://reader035.fdocuments.net/reader035/viewer/2022062511/551b0c275503462e578b5855/html5/thumbnails/41.jpg)
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 -](https://reader035.fdocuments.net/reader035/viewer/2022062511/551b0c275503462e578b5855/html5/thumbnails/42.jpg)
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 -](https://reader035.fdocuments.net/reader035/viewer/2022062511/551b0c275503462e578b5855/html5/thumbnails/43.jpg)
Documentation
![Page 44: CS101: INTRODUCTION TO COMPUTER PROGRAMMING LECTURE-1: INTRODUCTION TO PROBLEM SOLVING - CS002: REVIEW -](https://reader035.fdocuments.net/reader035/viewer/2022062511/551b0c275503462e578b5855/html5/thumbnails/44.jpg)
Documentation
![Page 45: CS101: INTRODUCTION TO COMPUTER PROGRAMMING LECTURE-1: INTRODUCTION TO PROBLEM SOLVING - CS002: REVIEW -](https://reader035.fdocuments.net/reader035/viewer/2022062511/551b0c275503462e578b5855/html5/thumbnails/45.jpg)
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