problem solving and program design. comp104 problem solving / slide 2 our first program // a simple...
Post on 19-Dec-2015
220 views
TRANSCRIPT
![Page 1: Problem Solving and Program Design. COMP104 Problem Solving / Slide 2 Our First Program // a simple program #include using namespace std; int main() {](https://reader031.vdocuments.net/reader031/viewer/2022032105/56649d3f5503460f94a186ce/html5/thumbnails/1.jpg)
Problem Solving
and
Program Design
![Page 2: Problem Solving and Program Design. COMP104 Problem Solving / Slide 2 Our First Program // a simple program #include using namespace std; int main() {](https://reader031.vdocuments.net/reader031/viewer/2022032105/56649d3f5503460f94a186ce/html5/thumbnails/2.jpg)
COMP104 Problem Solving / Slide 2
Our First Program
// a simple program#include <iostream>using namespace std;int main() { cout << "Hello world!" << endl; return 0;}
Preprocessor statements
Printstatement
Ends executionof main() which ends
program
Comments
Function
Function named main()
indicates start of
program
![Page 3: Problem Solving and Program Design. COMP104 Problem Solving / Slide 2 Our First Program // a simple program #include using namespace std; int main() {](https://reader031.vdocuments.net/reader031/viewer/2022032105/56649d3f5503460f94a186ce/html5/thumbnails/3.jpg)
COMP104 Problem Solving / Slide 3
C++ Software Development Major activities
Editing (to write the program) Compiling (creates .obj file) Linking with compiled files (creates .exe
file) Object files Library modules
Loading and executing Testing the program
Compile
Link
Library routines
Other object files
Think
Edit
Load
Execute
Source Program
![Page 4: Problem Solving and Program Design. COMP104 Problem Solving / Slide 2 Our First Program // a simple program #include using namespace std; int main() {](https://reader031.vdocuments.net/reader031/viewer/2022032105/56649d3f5503460f94a186ce/html5/thumbnails/4.jpg)
COMP104 Problem Solving / Slide 4
Problem Solving
Define the problem. Develop a solution.
Outline solution first. Then write down the solution steps in detail.
Test the solution and revise if necessary.
Document and maintain the solution.
![Page 5: Problem Solving and Program Design. COMP104 Problem Solving / Slide 2 Our First Program // a simple program #include using namespace std; int main() {](https://reader031.vdocuments.net/reader031/viewer/2022032105/56649d3f5503460f94a186ce/html5/thumbnails/5.jpg)
COMP104 Problem Solving / Slide 5
Example 1
Define Problem:
Find the best way to travel from HKUST to Central quickly and cheaply.
![Page 6: Problem Solving and Program Design. COMP104 Problem Solving / Slide 2 Our First Program // a simple program #include using namespace std; int main() {](https://reader031.vdocuments.net/reader031/viewer/2022032105/56649d3f5503460f94a186ce/html5/thumbnails/6.jpg)
COMP104 Problem Solving / Slide 6
Example 1
Develop Solution: Evaluate all possible routes and modes of transportation Select the route that meets our goal (fastest and cheapest)
![Page 7: Problem Solving and Program Design. COMP104 Problem Solving / Slide 2 Our First Program // a simple program #include using namespace std; int main() {](https://reader031.vdocuments.net/reader031/viewer/2022032105/56649d3f5503460f94a186ce/html5/thumbnails/7.jpg)
COMP104 Problem Solving / Slide 7
Example 1
Testing: Test the route and make sure conditions
have not changed (e.g., increased bus fares, road construction).
Documentation: Give description of solution and explain it.
Maintenance: Test and revise the route as needed.
![Page 8: Problem Solving and Program Design. COMP104 Problem Solving / Slide 2 Our First Program // a simple program #include using namespace std; int main() {](https://reader031.vdocuments.net/reader031/viewer/2022032105/56649d3f5503460f94a186ce/html5/thumbnails/8.jpg)
COMP104 Problem Solving / Slide 8
Programming as Problem Solving
Define the problem. What is the input & output? What constraints must be satisfied? What information is essential?
Develop a solution Construct an algorithm (steps that must be done)
Implement a program. Compile, test, and debug the program. Document and maintain the program.
![Page 9: Problem Solving and Program Design. COMP104 Problem Solving / Slide 2 Our First Program // a simple program #include using namespace std; int main() {](https://reader031.vdocuments.net/reader031/viewer/2022032105/56649d3f5503460f94a186ce/html5/thumbnails/9.jpg)
COMP104 Problem Solving / Slide 9
Example 2
Problem Statement: Convert US dollars into Hong Kong dollars.
Problem Analysis: Input: Amount in US$ Output: Amount in HK$ Apply official currency exchange rates.
![Page 10: Problem Solving and Program Design. COMP104 Problem Solving / Slide 2 Our First Program // a simple program #include using namespace std; int main() {](https://reader031.vdocuments.net/reader031/viewer/2022032105/56649d3f5503460f94a186ce/html5/thumbnails/10.jpg)
COMP104 Problem Solving / Slide 10
Example 2
Algorithm Read in amount in US$ Calculate the HK$ amount Display the results
![Page 11: Problem Solving and Program Design. COMP104 Problem Solving / Slide 2 Our First Program // a simple program #include using namespace std; int main() {](https://reader031.vdocuments.net/reader031/viewer/2022032105/56649d3f5503460f94a186ce/html5/thumbnails/11.jpg)
COMP104 Problem Solving / Slide 11
Example 2 // converts US$ to HK$ #include <iostream> using namespace std; int main(){
double usdollars; double hkdollars;
// read in amount in US$ cout <<"Enter US$ amount and press return: "; cin >> usdollars;
// calculate the HK$ amount hkdollars = 7.8 * usdollars;
// display the results cout << "US$" << usdollars << " = HK$" << hkdollars << endl; return 0;
}
![Page 12: Problem Solving and Program Design. COMP104 Problem Solving / Slide 2 Our First Program // a simple program #include using namespace std; int main() {](https://reader031.vdocuments.net/reader031/viewer/2022032105/56649d3f5503460f94a186ce/html5/thumbnails/12.jpg)
COMP104 Problem Solving / Slide 12
Example 2
Program Implementation: Program comments: // Library reference: #include Function type: int Function name and (lack of) parameters:
main( ) Statement braces: { } Variable declaration: double Input/output functions: cin, cout
![Page 13: Problem Solving and Program Design. COMP104 Problem Solving / Slide 2 Our First Program // a simple program #include using namespace std; int main() {](https://reader031.vdocuments.net/reader031/viewer/2022032105/56649d3f5503460f94a186ce/html5/thumbnails/13.jpg)
COMP104 Problem Solving / Slide 13
What Makes a Good Program?
Correctness Meets the problem requirements Produces correct results
Easy to read and understand Easy to modify Easy to debug Efficient
Fast Requires less memory