View
24
Download
3
Category
Preview:
DESCRIPTION
Chapter 1. Overview. STEP 1. STEP 2. STEP 3. What is Computer Programming?. It is the process of planning a sequence of steps ( called instructions) for a computer to follow. Programming Life Cycle Phases. Problem-Solving Implementation Maintenance. Problem-Solving Phase. - PowerPoint PPT Presentation
Citation preview
Chapter 1Overview
2
What is Computer Programming? It is the process of planning a sequence
of steps (called instructions) for a computer to follow.
STEP 1
STEP 2
STEP 3
. . .
3
Programming Life Cycle Phases
• Problem-Solving• Implementation• Maintenance
4
Problem-Solving PhaseAnalyze the problem and specify what
the solution must do
Develop a general solution(algorithm) to solve the problem
Verify that your solution really solves the problem
AlgorithmAn algorithm is a finite list
of step-by-step instructions for solving a problem.
It can be presented by Pseudo Code
If score is positive
Add score to Total
Else
Display an error message
How to put an elephant into a fridge?
Example Problem: Coin CountingI have 100 coins (quarter, dime, nickel
and penny only), and I want to figure out how much their total value is.
Count and add the coins one by one.
Divide the coins into four piles (quarter, dime, nickel and penny), then count them separately. Sum the values of the four piles.
What will you do?
OR
Which one is better? (for human and for computer)
Example Problem: Coin CountingCount and add the coins one by
one.Algorithm:
1) Read coin2) Add the coin value to the summed value3) Repeat Step 1) and 2) if # of coins counted
is not 100 yet
Example: TriangleThe triangle program accepts 3 integers: a, b and c, which
are taken to be sides of a triangle.The output of the program is the type of triangle
determined by the three sides: Equilateral, Isosceles, Scalene, or NotATriangle.
Can you write an algorithm for this problem?
What is a programming language?It is a language (just like English,
German,…) with strictGrammar rulesSymbolsSpecial words
that can be interpreted by a computer (compiler to be exact).
Example Program int numCoin = 0; float sumValue = 0; string coinType;
while ( numCoin < 100 ) { cin >> coinType;
if ( coinType == "quarter" ) sumValue = sumValue + 0.25; if ( coinType == "dime" ) sumValue = sumValue + 0.1; if ( coinType == "nickel" ) sumValue = sumValue + 0.05; if ( coinType == "penny" ) sumValue = sumValue + 0.01;
numCoin = numCoin + 1; }
Note: This is NOT a complete program!
Let’s try if it works.
Do you understand?
High-level programming languagemore like a nature language
(You almost understand it!)C++JavaCC#PHP
similar to English
But computers don’t understand it!
Low-level programming languagemachine language (computer’s native language)
machine specific, not portablecomposed of 0 and 1’s
assembly language:symbolic representation of the machine codes
10010110 00000000 10100101
Add AX, Score
Running a programWe need to translate the C++ program
into machine code!
other code from libraries,
etc.
other code from libraries,
etc.
written in machine language
written in machine language
written in machine language
written in machine language
written in C++
written in C++
via compiler via linker
SOURCE OBJECT EXECUTABLE
Lab0.cpp Lab0.obj Lab0.exe
Always solve the problem before writing the program!
15
Problem Solving Techniques Ask questions
what is the input data? what should be the output? what if …?
Look for familiar things find the cheapest product in the inventory find the coldest day of the year
Solve by analogy how to play tennis? I know how to play badminton. It’s similar!
Use means-ends analysis determine the I/O and then work out the details AB, how?
Problem Solving TechniquesFor a large problem:
divide and conquerbuilding-block approachmerging solutions
How to build a house?
Recommended