160 likes | 288 Views
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.
E N D
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 • Analyze 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
Algorithm How to put an elephant into a fridge? • An algorithm is a finitelist 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
Example Problem: Coin Counting • I 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 Counting • Count and add the coins one by one. • Algorithm: • Read coin • Add the coin value to the summed value • Repeat Step 1) and 2) if # of coins counted is not 100 yet
Example: Triangle • The 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 strict • Grammar rules • Symbols • Special 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; } Do you understand? Note: This is NOT a complete program! Let’s try if it works.
High-level programming language • more like a nature language (You almost understand it!) • C++ • Java • C • C# • PHP • similar to English But computers don’t understand it!
Low-level programming language • machine language (computer’s native language) • machine specific, not portable • composed of 0 and 1’s • assembly language: • symbolic representation of the machine codes • 10010110 00000000 10100101 Add AX, Score
Lab0.cpp Lab0.obj Lab0.exe SOURCE OBJECT EXECUTABLE written in C++ written in machine language written in machine language via linker via compiler other code from libraries, etc. Running a program • We need to translate the C++ program into machine code!
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 Techniques • For a large problem: • divide and conquer • building-block approach • merging solutions How to build a house?