16

Click here to load reader

Chapter 1

  • Upload
    ghada

  • View
    24

  • Download
    3

Embed Size (px)

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

Page 1: Chapter 1

Chapter 1Overview

Page 2: Chapter 1

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

. . .

Page 3: Chapter 1

3

Programming Life Cycle Phases

• Problem-Solving• Implementation• Maintenance

Page 4: Chapter 1

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

Page 5: Chapter 1

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?

Page 6: Chapter 1

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)

Page 7: Chapter 1

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

Page 8: Chapter 1

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?

Page 9: Chapter 1

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

Page 10: Chapter 1

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?

Page 11: Chapter 1

High-level programming languagemore like a nature language

(You almost understand it!)C++JavaCC#PHP

similar to English

But computers don’t understand it!

Page 12: Chapter 1

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

Page 13: Chapter 1

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

Page 14: Chapter 1

Always solve the problem before writing the program!

Page 15: Chapter 1

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?

Page 16: Chapter 1

Problem Solving TechniquesFor a large problem:

divide and conquerbuilding-block approachmerging solutions

How to build a house?