Chapter 1 Overview What is Computer Programming It

  • Slides: 16
Download presentation
Chapter 1 Overview

Chapter 1 Overview

What is Computer Programming? � It is the process of planning a sequence of

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. . . 2

Programming Life Cycle Phases • Problem-Solving • Implementation • Maintenance 3

Programming Life Cycle Phases • Problem-Solving • Implementation • Maintenance 3

Problem-Solving Phase �Analyze the problem and specify what the solution must do �Develop a

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 4

Algorithm How to put an elephant into a fridge? �An algorithm is a finite

Algorithm How to put an elephant into a fridge? �An 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

Example Problem: Coin Counting �I have 100 coins (quarter, dime, nickel and penny only),

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. What will you do? �Count and add the coins one by one. OR �Divide the coins into four piles (quarter, dime, nickel and penny), then count them separately. Sum the values of the four piles. Which one is better? (for human and for computer)

Example Problem: Coin Counting �Count and add the coins one by one. �Algorithm: 1)

Example Problem: Coin Counting �Count and add the coins one by one. �Algorithm: 1) Read coin 2) Add the coin value to the summed value 3) 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

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 Not. ATriangle. Can you write an algorithm for this problem?

What is a programming language? �It is a language (just like English, German, …)

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 float string num. Coin = 0; sum. Value = 0; coin.

Example Program int float string num. Coin = 0; sum. Value = 0; coin. Type; Do you understand? while ( num. Coin < 100 ) { cin >> coin. Type; if ( coin. Type sum. Value = == "quarter" ) sum. Value + 0. 25; == "dime" ) sum. Value + 0. 1; == "nickel" ) sum. Value + 0. 05; == "penny" ) sum. Value + 0. 01; num. Coin = num. Coin + 1; } 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

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

Low-level programming language �machine language (computer’s native language) �machine specific, not portable �composed of 0 and 1’s 10010110 0000 10100101 �assembly language: �symbolic representation of the machine codes Add AX, Score

Running a program �We need to translate the C++ program into machine code! Lab

Running a program �We need to translate the C++ program into machine code! Lab 0. cpp SOURCE Lab 0. obj OBJECT written in C++ written in machine language via compiler Lab 0. exe EXECUTABLE written in machine language via linker other code from libraries, etc.

Always solve the problem before writing the program!

Always solve the problem before writing the program!

Problem Solving Techniques � Ask questions what is the input data? � what should

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 � A B, how? � 15

Problem Solving Techniques �For a large problem: �divide and conquer �building-block approach �merging solutions

Problem Solving Techniques �For a large problem: �divide and conquer �building-block approach �merging solutions How to build a house?