Title: Lecture1: Introduction to Programming and Problem Solving
1Lecture1 Introduction to Programming and
Problem Solving
2Topics
- 1. Computer Operations
- 2. What is programming?
- 3. What are the steps of program development?
- 4. What is structured programming
- 5. Data and data structures
3Six Basic Computer Operations
- 1. A computer can receive (input) data
- 2. A computer can store data in memory
- 3. A computer can perform arithmetic and
manipulate text strings - 4. A computer can compare the contents of two
memory locations and select one of two
alternatives - 5. A computer can repeat a group of operations
- 6. A computer can output information (processed
data)
4Computer Operations
5What is Programming
- Program a very specific set of instructions (or
command lines) that making a computer do what you
want it to do - Programming the process of creating a program
- the development of a solution to an identified
program, and setting up of a related series of
instructions which, when directed through
computer hardware, will produce the desired
results
6Steps in program development
- 1. Define the problem
- 2. Outline the solution
- 3. Develop the outline into an algorithm
- 4. Test the algorithm for correctness
- 5. Code the algorithm into a specific
programming language - 6. Run the program on the computer
- 7. Document and maintain the program
7Define the Problem
- Divide the problem into three components
- (called IPO)
- Inputs what do you have?
- Outputs what do you want to have?
- Processing
- how do you go from inputs to outputs?
- A defining diagram is recommended
8Outline the Solution
- The major processing steps involved
- The major subtasks (if any)
- The major control structures (e.g. repetition
loops) - The major variables and record structures
- The mainline logic
9Develop the Outline into an Algorithm
- Algorithm is a set of precise steps that describe
exactly the tasks to be performed, and the order
in which they are to be carried out - Pseudocode (a form of structured English) is used
to represent the solution algorithm
10Test the Algorithm for Correctness
- The main purpose of desk checking the algorithm
is to identify major logic errors early, so that
they may be easily corrected - Test data needs to be walked through each step in
the algorithm, to check that the instructions
described in the algorithm will actually do what
they are supposed to
11Code the Algorithm into a Specific Programming
Language
- Only after all design considerations have been
met should you actually start to code the program
into your chosen programming language (e.g.
Visual Basic, Java, C)
12Run the Program on the Computer
- This step uses a program compiler and
programmer-designed test data to machine test the
code for syntax errors - Program complier translate high-level languages
(e.g. VB) to low-level machine language before
execution
13Document and Maintain the Program
- Not the last step in the program development
process - An ongoing task from the initial definition of
the problem to the final test result - Involves both external documentation (such as
hierarchy charts) and internal documentation that
may have been coded in the program
14Example 1 Circle
- Calculate the circumference and area
- IPO
- I radius
- P calculate circumference and area as
- circumference 2 P I radius
- areaPI radius 2
- O radius, circumference, area
15Circle (Pseudocode)
- Begin program
- get radius, PI
- circumference 2 P I radius
- areaPI radius radius
- display circumference, area
- End program
16Example 2 Payroll Problem
- I time cards, pay rate, deductions
- P calculate gross pay, calculate net pay
- hours worked end time start time
- gross pay hours worked pay rate
- net pay gross pay - deductions
- O payroll records (gross pay, deductions net pay)
17Payroll problem (cont.)
- Subtasks
- Deductions include required (taxes) and optional
(staff benefits) - Gross pay may include vacation time, sick time,
personal time. - Major control structures
- Repeat payroll processing for all employees
- Major variables
- Employee pretty much static data
- Name, address, department,
- Employee non-static historical data
- Year to date earnings,
- Employee current data
- Hours worked, vacation hours, sick hours,
- Global data
- Tax tables federal state , tax cutoffs
18Payroll Problem Pseudocode
- (assuming deductions are given)
- Begin program
- Read Start_time, End_time, Pay_rate, Deductions
- Hours_worked End_time Start_time
- Gross_pay Hours_worked Pay_rate
- Net_Pay Gross_pay Deductions
- Display Gross_pay, Deductions, Net_pay
- End program
19Structured Programming
- Top-Down
- outline a general solution to the problem first
- start coding at the beginning of the problem and
work systematically through each step until
reaching the end - Modular
- keep things together that belong together
- e.g. calculating sales tax or printing report
headings - Three control structures
- Sequence (one after another)
- Selection (making decisions/choices)
- Repetition (doing things over and over)
20Sequence
- The straightforward execution of one processing
step after another - pseudocode statements
- statement a
- statement b
- statement c
21Example of Sequence in VB
- Private Sub CmdTotlasales_Click()
- Numbercontracts CInt(TxtTotalContracts.Text)
- Pricecontracts CCur(TxtContractPrice.Text)
- Totalrevenue Numbercontracts Pricecontracts
- TxtTotalSales.Text
- FormatCurrency(Totalrevenue, 2)
-
- End Sub
22Selection
- Pseudocode statements
- IF condition p is true THEN
- statement(s) in true case
- ELSE
- statement(s) in false case
- ENDIF
- If condition p is true then the statement or
statements in the true case will be executed, and
the statements in the false case will be skipped
23Example of Selection in VB
- If strGrade A then
- sngGradePoint 4.0
- Elseif strGrade B then
- sngGradePoint 3.0
- Elseif strGrade C then
- sngGradePoint 2.0
- Elseif strGrade D then
- sngGradePoint 1.0
- Else
- sngGradePoint 0.0
- End If
24Repetition
- A set of instructions to be performed repeatedly,
as long as a condition is true - Pseudocode statements
- DOWHILE condition p is true
- statement block
- ENDDO
25Example of Repetition
- Add up all odds between 1 to 20
- 1 3 5 7 9 19 ?
- Humans way
- 1 3 4
- 4 5 9
- 9 7 16
-
-
81 19 100
26Example of Repetition (Cont)
- Computers way
- Private Sub CmdCalcSum_Click()
- intSum 0 intCounter 1 Do while
intCounter lt 20 intSum intSum
intCounter intCounter intCounter 2 Loop - End sub
27Data
- Variable
- the name given to a collection of memory cells,
designed to store a particular data item - the value stored in that variable may change or
vary as the program executes - Constant
- a data item with a name and a value that remain
the same during the execution of the program
(e.g. 8, 10) - Literal
- a constant whose name is the written
representation of its value (e.g. 8, 10)
28Data type
- Integers
- whole numbers
- Real
- Numbers with a decimal point
- Two choices in VB single and double
- Character
- Boolean
- True or False
29Data Structures(also called data aggregates)
- Record
- One set of related data
- A time card an invoice
- File
- A set of related records
- Array
- A collection of data that have same data type
- String
- An array of characters
- Hello Daniel Chen