Computer Programming TCP1224 Chapter 3 Completing the Problem-Solving Process and Getting Started with C++

Post on 13-Jan-2016

213 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

Transcript

Computer ProgrammingTCP1224

Chapter 3Completing the Problem-Solving Process and Getting Started with C++

Week 2 – What we talked about•Analysis of a problem

•Input-Process-Output (IPO) Chart

•Algorithm

•Pseudocode and Flowchart

2

Comments about tutorial• I am glad that many of you tried it, but many seem to be

unsure of what you were doing. From my limited observation;

▫ Many are probably thinking “what does this have to do with programming?”

▫ Some have already sounded out that programming may be difficult! (utter nonsense, it is very easy).

• What is programming?

▫ Syntax that is hard to remember?

▫ The need to learn another communication language?

▫ It’s that sequence, repetition and selection thing-a-mie-bob?

3

UN-official view of programming• It is a set of simple to follow instructions for a dumb piece

of machinery that has to be spelt out step by step in simple to understand language.

▫ Most of us explain an issue or answer questions for people who have some intelligence and will understand our answers. E.g.,

Do you drink? Meaning “do you drink any alcoholic beverage”?

Where were you? Usually meaning, “I was looking for you all over and could not

find you, where were you at noon when I came to you room?”

4

UN-official view of programming• Now, imagine that you need to explain to someone who

has no sign of intelligence.

▫ You will need to speak to that person sloooowwwwlyyyyyy … or actually, you need to speak to that person, word by word.

▫ So, same as how you would tell the computer, as in programming. You need to tell step by step … it is really dumb!

▫ So, back to the lecture … a computer can only execute instructions in sequence, is able to repeat (repetition) and you can tell it how to decide (selection).

5

So, what’s this IPO chart for?• For you to tell the “intelligent-less” machine, what it

can work with (the input) to produce the results (output).

• The “processing” component is the step by step instructions for the computer.

▫ A computer has no intelligence, so even a simple thing like obtaining the input has to be told. E.g. “Enter the loan period”. It is better to maybe even say, “get input from keyboard for the loan period in months”.

▫ You will need to specify these steps.

▫ In your mathematics classes, you have often been told to answer showing the steps you used to derive the results.

6

Math exampleA very popular math solving step …

Let x = y

x ( – y + y ) = y

x – y + y / ( x – y ) = y / ( x – y )

1 + y / ( x – y ) = y / ( x – y )

1 = ( y / ( x – y ) ) - ( y / ( x – y ) )

1 = 0

7

Algorithm• In general, most of you need to be more detailed in your

algorithm.

• Clearly state where does the values used originate from and what each notation mean.

• Proof read it by making yourself “intelligent-less” and determine if what you wrote can produce the output required.

8

9

This week’s objectives?• Code an algorithm into a program (How to)

• Desk-check a program (as oppose to Desk-check the algorithm)

• Evaluate and modify a program

• Understand the components of a C++ program

• Create a C++ program

10

Concept Lesson• More on the Problem-Solving Process

• Coding the Algorithm into a Program

• Desk-Checking the Program

• Evaluating and Modifying the Program

• Creating a C++ Program

11

More on the Problem-Solving Process

12

Coding the Algorithm into a Program

13

Assigning Names, Data Types, and Initial Values to the IPO Items• To code algorithm (i.e. to be able to transform from the

algorithm to a program), first assign a name to each input, processing, and output item in IPO chart. By convention;▫ Names can contain only letters, numbers, and _ ▫ Cannot contain punctuation characters or spaces▫ Examples:

raise newPay

▫ Each input/processing/output item must have a data type

▫ You may initialize each item

14

usually in lowercase letters

use camel case if name contains multiple words

Assigning Names, Data Types, and Initial Values to the IPO Items• Camel case

▫ The words are written without any spaces in between them, and the first letter of the 2nd and following words are capitalized.

▫ This creates humps on the words and hence are known as camel case.

▫ E.g. thisLectureIsBoring• Data type

▫ A computer stores information in bits and bytes and have limited variations. In each and every name given, the computer has to be told how to store them and how to use them.

▫ E.g. if it is a real number, the C++ language calls it a float or double. Why the difference?

• Initialize▫ Each time you create a new “variable”, it is just a name. More

often than not, you need to put something in there.

15

Assigning Names, Data Types, and Initial Values to the IPO Items

16

double is a keyword

this is a statement

all C++ statements must end with a semicolon

Translating the Algorithm into Code

17

cout: standard output streamcin: standard input stream<<: insertion operator>>: extraction operator

stream: sequence of characters, to perform standard I/O operations

a stream manipulator

Review: The core C++ code

18

Desk-Checking the Program

19

Desk-Checking the Program (continued)

20

Desk-Checking the Program (continued)

21

Evaluating and Modifying the Program• Testing is running the program, with sample

data

▫Results should agree with desk-check ones!

• Debugging is locating/removing errors in program

▫Program errors are called bugs

22

Evaluating and Modifying the Program• A syntax error occurs if an instruction violates

the programming language’s syntax (set of rules)

▫E.g., cout < "Hello";

• A logic error occurs if an instruction does not give the expected results

▫E.g., average = number1 + number2 / 2;

23

Creating a C++ Program

24

source file: Ch3Lab2.cpp

object file: Ch3Lab2.obj

executable file: Ch3Lab2.exe

created using an IDE or a general-purpose editor

An Introduction to Programming with C++,

Fifth Edition

25

Summary• Fourth step in the problem-solving process is

to code the algorithm into a program• In C++, you perform standard I/O operations

using streams (sequences of characters)▫cout and cin▫Insertion operator (<<) sends data to output

stream▫Extraction operator (>>) gets data from input

stream• After coding the algorithm, you desk-check

program• Final step in the problem-solving process is

to evaluate and modify (if necessary) the program

26

Summary (continued)• Some programs have errors, called bugs

▫Syntax error occurs when an instruction violates one of the rules of the programming language’s syntax

▫Logic error occurs when you enter an instruction that does not give you the expected results

▫You debug to locate and remove errors• To create and execute a C++ program, you

need to have a text editor and a C++ compiler▫Compiler translates source code into object

code▫Linker produces executable file

27

Summary (continued)• Comments are internal documentation of

programs• C++ programs typically include at least one

directive• using statements tell compiler where it can

find the definition of certain keywords• A function is a block of code that performs a

task▫main() is where the execution of program

begins• The first line in a function is called the

header▫After the header comes the body, enclosed in

braces

28

29

top related