Title: Programming
1CHAPTER 1
- Programming
- Problems Solving
2Objectives
- Define the meaning of programming and program
correctly - List step by step process in program development
3What is computer programming?
- Process to plan the sequence of instructions that
will be execute by the computer - Task / events
- Planning
- Scheduling
- Execution
- To handle specific task / events
4What is computer program?
- Sequence of instructions that highlight the steps
to execute by the computer - Use programming language
- Computer should understand the instruction given
by the programmer
5What is Problem Solving?
- Process of transforming the description of a
problem into a solution of that problem 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. - - page 21, Problem Solving Using C, Yuksel Uckan
6Problem specifics analysis
- Requirement specification eliminated
ambiguities in the problem statement. - Analysis identified problem inputs, outputs
collected information about the inputs. - Design developed a series of steps with a
logical order which, when applied, would produce
the output of the problem - Implementation Using tools.
- Testing verification check the output to
verify the correctness of the method.
7Problem solving approach
- Engineering Science Engineering Science
method. - Bussiness-oriented systems approach
- Computer programs software development
method/software life-cycle
8Software development methods
- Requirement specification
- Analysis
- Design
- ------------------------------
- Implementation
- Testing Verification
- Documentation
Problem solving phase
Implementation phase
9Requirement Specification
- Understanding what the problem is?
- What is needed to solve it?
- What is solution should provide?
- Constraints special conditions.
- Defining problem depends on degree of
familiarity with the problem domain. What would
you do if you dont?
10Analysis
- Inputs to the problem, their form input media
to be used - Outputs expected from the solution, their form
output media to be used - Any special constraints or conditions
- Formulas or equations to be used
11Design
- Method of solution what is define by design?
- Also called as algorithm a sequence of a finite
number of steps arranged in a specific logical
order, which, when executed, produce the solution
for a problem.
12A quick history of algorithm
- Introduce by a Persian mathematician named Abu
Jafar Mohammed Ibnu Musa Al-Khawarizmi - Al-khawarizmi
- A set of procedure that explain the way to solve
a problem
13Why study algorithms?
- Describes the steps needed to perform a
computation - Important for writing efficient code
- Code that execute faster which uses less memory
- A finite number of steps for solving a particular
problem within a certain time frame. - A finite step by step problem solving technique
14Characteristics of a good algorithm
- Simple, clear concise readable
- Solvable, terminates after a finite number of
instructions - Correct, efficient robust
- Accomplishes at least one task or produces at
least one output
15Algorithm representation
- Pseudocode
- A semiformal, Englishlike language with a
limited vocabulary can be used to design
describe algorithm. - Flowchart
- A graph consisting of geometrical shapes that are
connected by flow lines.
16Guidelines to develop the pseudocode
- Each step has not more than 2 action
- Each steps is execute in-order
- The word End is used to show the end of process
- Selection / conditional structure (optional)
- Repetition / loop structure (optional)
- Use arithmetic symbol
17- Variable name can be declare to identify the
appropriate its data type represent / hold
value - Examples of data types Integer, floating-point,
character, etc. - Initial value for variable (optional)
- Logical operator (if required)
- lt, lt, gt, gt, !,
18- Word must be clear meaningful
- Use the word Read / Input / Get for data input to
algorithm - Use the word Print / Write / Output to mention
the output from algorithm
19Flowchart
- Flowcharts were formerly used to describe each
processing path in a program (the main program
and various subroutines that could be branched
to) - Programmers were admonish to always flowchart
their logic rather than carry it in their heads - Use of simple geometric symbols to represent the
beginning or end of a program (an oval), a
process (a rectangle), a decision (a diamond), or
an I/O process (a parallelogram). These symbols
are defined in ANSI x 3.5 and ISO 1028
20Symbols use in flowchart
Begin / End
Activity Flow
21Related websites
- Further information
- http//users.erofs.com/zenithco/khawaris.html
- Download tools (free)
- http//www.smartdraw.com
22Guidelines to develop the flowchart
- Identify the correct symbol
- according to their function
- Get a correct formula
- Use the appropriate data
- Test your flowchart
- Trace each symbol compare with the expected
output
23Example
- Problem Add 2 numbers
- Steps
- Begin
- Read number 1
- Read number 2
- Add number 1 number 2
- End
Read number 1
Read number 2
24Selection Structure
- Single selection
- Bi-selection
- Multi-selection
25Single selection
- Test for one condition only
- Choose either true or false
- There are an alternate flow if the condition is
true - Syntax
- Step a
- If ltcondition truegt
- Begin
- Step 1
- Step 2
- . . . . . .
- Step n
- End
- Step n 1
26Example
Problem Compare 2 values. If number 1 is
greater than number 2, print message Number 1
Flowchart
- Pseudocode
- Begin
- Read no1
- Read no2
- If (no1 gt no2 )
- 4.1 Print Number 1
- End
Read no1
Read no2
True
If no1 gt no2
Print Number1
False
27Bi-selection
- Choose either true or false
- 2 different alternate flow for both condition
- Syntax
If ltcondition truegt Begin Step 1 . . . . Step
n End If ltcondition falsegt Begin Step 1 . . .
. Step n End
28Example
Problem Compare 2 values. If number 1 is
greater than number 2, print message Number 1
else print message Number 2
- Pseudocode
- Begin
- Read no1
- Read no2
- If (no1 gt no2 )
- 4.1 Print Number 1
- Else
- 5.1 Print Number 2
- End
29Example Flowchart
Read no1
Read no2
If no1 gt no2
True
False
Print Number 1
Print Number 2
30Multi-selection
- There are several alternative flow regarding to
the satisfied condition
Problem Compare 2 values. If number 1 is greater
then number 2, print message Number 1. If
number 2 is greater than number 1, print message
Number 2, otherwise print message Equal.
31Example Pseudocode
- Begin
- Read no1
- Read no2
- If (no1 gt no2 )
- 4.1 Print Number 1
- Else If (no2 gt no1 )
- 5.1 Print Number 2
- Else
- 6.1 Print Equal
- 7. End
32Example Flowchart
Read no1
Read no2
True
Print Number 1
If no1 gt no2
False
True
Print Number 2
If no2 gt no2
False
33Repetition Structure
34FOR Loop
- 3 components
- Initial value for counter
- Condition to repeat the loop
- Update the counter
- The counter can be increase / decrease
- Condition might be TRUE or FALSE
- Must test the condition first
- Condition result might be TRUE or FALSE
- If the condition is TRUE, alternate flow will be
execute
35General Syntax
- FOR (counter initial value test counter
update counter) - BEGIN
- Statement 1
- Statement 2
- . . .
- Statement n
- END
Test C
FALSE
TRUE
36Example problem
- Print a multiplication table for 1 to 10 by
number 2
37Solution Pseudocode
- Begin
- Let TOTAL 0
- For (C1 Clt11 increase value C by 1)
- 3.1 Begin
- 3.2 TOTAL TOTAL 2
- 3.3 Print TOTAL
- 3.4 End
- End
Alternate flow
38Solution Flowchart
True
False
39WHILE loop
- Must test the condition first
- Condition result might be TRUE or FALSE
- If the condition is TRUE, alternate flow will be
execute - If the condition is FALSE, skip the alternate
flow - If the condition is FALSE at the first test,
alternate flow will not be execute anymore
40General syntax
- WHILE ltcondition TRUEgt
- Begin
- Statement 1
- Statement 2
- . . .
- Statement n
- End
TRUE
Condition
FALSE
41Example problem
- Do the summation for the input number until user
key-in 999. Print total
42Pseudocode
- Begin
- TOTAL 0
- INPUT 0
- WHILE (INPUT ! 999)
- 2.1 Begin
- 2.2 Read INPUT
- 2.3 TOTAL TOTAL INPUT
- 2.4 End
- Print TOTAL
- End
Alternate flow
43Flowchart
44DO WHILE Loop
- Condition test at the end of loop
- The loop will be repeated if the condition is
TRUE - The alternate flow will be executed at least once
45General syntax
- Begin
- Statement 1
- Statement 2
- . . .
- Statement n
- End
- WHILE ltcondition TRUEgt
Condition
TRUE
FALSE
46Pseudocode
- Begin
- TOTAL 0
- 2.1 Begin
- 2.2 Read INPUT
- 2.3 TOTAL TOTAL INPUT
- 2.4 End
- 2.5 WHILE (INPUT ! 999)
- Print TOTAL
- End
Alternate flow
47Flowchart