Chapter 3: Completing the Problem- Solving Process and Getting Started with C++ Introduction to...

25
Chapter 3: Completing the Problem-Solving Process and Getting Started with C++ Introduction to Programming with C++ Fourth Edition

Transcript of Chapter 3: Completing the Problem- Solving Process and Getting Started with C++ Introduction to...

Page 1: Chapter 3: Completing the Problem- Solving Process and Getting Started with C++ Introduction to Programming with C++ Fourth Edition.

Chapter 3:Completing the Problem-

Solving Process and Getting Started with C++

Introduction to Programming with C++

Fourth Edition

Page 2: Chapter 3: Completing the Problem- Solving Process and Getting Started with C++ Introduction to Programming with C++ Fourth Edition.

Introduction to Programming with C++, Fourth Edition 2

Objectives

• Code an algorithm into a program• Desk-check a program• Evaluate and modify a program• Differentiate among source code, object code,

and executable code• Understand the components of a C++ program

Page 3: Chapter 3: Completing the Problem- Solving Process and Getting Started with C++ Introduction to Programming with C++ Fourth Edition.

Introduction to Programming with C++, Fourth Edition 3

Objectives (continued)

• Create a Visual C++ .NET solution, project, and source file

• Open a Visual C++ .NET solution• Save, build, and execute a C++ program• Locate an error in a C++ program• Make a backup copy of a solution

Page 4: Chapter 3: Completing the Problem- Solving Process and Getting Started with C++ Introduction to Programming with C++ Fourth Edition.

Introduction to Programming with C++, Fourth Edition 4

More on the Problem-Solving Process

Page 5: Chapter 3: Completing the Problem- Solving Process and Getting Started with C++ Introduction to Programming with C++ Fourth Edition.

Introduction to Programming with C++, Fourth Edition 5

Coding the Algorithm into a Program

Page 6: Chapter 3: Completing the Problem- Solving Process and Getting Started with C++ Introduction to Programming with C++ Fourth Edition.

Introduction to Programming with C++, Fourth Edition 6

Coding the Algorithm into a Program (continued)

• IPO chart shows:– Input, processing, and output items– Algorithm needed to solve the problem

• The algorithm shows the steps to calculate and display Sarah’s new weekly pay

• The calculation is based on the current weekly pay and raise rate values entered by the user

• Algorithm also calculates an intermediate value, weekly raise

Page 7: Chapter 3: Completing the Problem- Solving Process and Getting Started with C++ Introduction to Programming with C++ Fourth Edition.

Introduction to Programming with C++, Fourth Edition 7

Coding the Algorithm into a Program (continued)

Page 8: Chapter 3: Completing the Problem- Solving Process and Getting Started with C++ Introduction to Programming with C++ Fourth Edition.

Introduction to Programming with C++, Fourth Edition 8

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

• Assign a descriptive name to each unique input, processing, and output item listed in the IPO

• Be aware of naming rules• Assign a data type to each input, processing,

and output item• Assign an initial value

Page 9: Chapter 3: Completing the Problem- Solving Process and Getting Started with C++ Introduction to Programming with C++ Fourth Edition.

Introduction to Programming with C++, Fourth Edition 9

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

Page 10: Chapter 3: Completing the Problem- Solving Process and Getting Started with C++ Introduction to Programming with C++ Fourth Edition.

Introduction to Programming with C++, Fourth Edition 10

Translating the Algorithm Steps into C++ Code

Page 11: Chapter 3: Completing the Problem- Solving Process and Getting Started with C++ Introduction to Programming with C++ Fourth Edition.

Introduction to Programming with C++, Fourth Edition 11

Desk-Checking the Program

Page 12: Chapter 3: Completing the Problem- Solving Process and Getting Started with C++ Introduction to Programming with C++ Fourth Edition.

Introduction to Programming with C++, Fourth Edition 12

Desk-Checking the Program (continued)

Page 13: Chapter 3: Completing the Problem- Solving Process and Getting Started with C++ Introduction to Programming with C++ Fourth Edition.

Introduction to Programming with C++, Fourth Edition 13

Desk-Checking the Program (continued)

Page 14: Chapter 3: Completing the Problem- Solving Process and Getting Started with C++ Introduction to Programming with C++ Fourth Edition.

Introduction to Programming with C++, Fourth Edition 14

Desk-Checking the Program (continued)

Page 15: Chapter 3: Completing the Problem- Solving Process and Getting Started with C++ Introduction to Programming with C++ Fourth Edition.

Introduction to Programming with C++, Fourth Edition 15

Desk-Checking the Program (continued)

Page 16: Chapter 3: Completing the Problem- Solving Process and Getting Started with C++ Introduction to Programming with C++ Fourth Edition.

Introduction to Programming with C++, Fourth Edition 16

Evaluating and Modifying the Program

• Debugging - the process of locating and removing any errors, called bugs, in a program

• Program errors can be either syntax errors or logic errors

• You create a syntax error when you enter an instruction that violates the programming language’s syntax

Page 17: Chapter 3: Completing the Problem- Solving Process and Getting Started with C++ Introduction to Programming with C++ Fourth Edition.

Introduction to Programming with C++, Fourth Edition 17

Evaluating and Modifying the Program (continued)

• Logic errors - much more difficult to find because they can occur for a variety of reasons and do not trigger an error message from the compiler

Page 18: Chapter 3: Completing the Problem- Solving Process and Getting Started with C++ Introduction to Programming with C++ Fourth Edition.

Introduction to Programming with C++, Fourth Edition 18

Creating a C++ Program

• C++ evolved from the procedure-oriented C programming language, which was developed in 1972 at Bell Laboratories by Dennis Ritchie

• In 1985, Bjarne Stroustrup (Bell Laboratories) added object-oriented features to C

• C++ is a superset of C• Source code - C++ instructions• Source file – contains the source code

Page 19: Chapter 3: Completing the Problem- Solving Process and Getting Started with C++ Introduction to Programming with C++ Fourth Edition.

Introduction to Programming with C++, Fourth Edition 19

Creating a C++ Program (continued)

• Object code - the 0s and 1s that the computer can understand

• Object file - the file containing the object code• Linker - combines the object file with other

machine code necessary for your C++ program to run correctly

• Executable file - contains all of the machine code necessary to run your C++ program

Page 20: Chapter 3: Completing the Problem- Solving Process and Getting Started with C++ Introduction to Programming with C++ Fourth Edition.

Introduction to Programming with C++, Fourth Edition 20

Process by which Source Code is Translated into Executable Code

Page 21: Chapter 3: Completing the Problem- Solving Process and Getting Started with C++ Introduction to Programming with C++ Fourth Edition.

Introduction to Programming with C++, Fourth Edition 21

Page 22: Chapter 3: Completing the Problem- Solving Process and Getting Started with C++ Introduction to Programming with C++ Fourth Edition.

Introduction to Programming with C++, Fourth Edition 22

Creating a C++ Program

• Comment (internal documentation) - a message to the person reading the program

• Function - a block of code that performs a task• Void functions – do not return values after

completing their assigned tasks• Function header - marks the beginning of the

function• Function body - everything between the

opening and closing braces

Page 23: Chapter 3: Completing the Problem- Solving Process and Getting Started with C++ Introduction to Programming with C++ Fourth Edition.

Introduction to Programming with C++, Fourth Edition 23

Summary

• After analyzing a problem, code the algorithm into a program

• Desk-check the program to verify that the algorithm was correctly translated

• Evaluate and modify if necessary• Program errors can be either:

– Syntax: violate a rule of the language

– Logic: error in the algorithm

Page 24: Chapter 3: Completing the Problem- Solving Process and Getting Started with C++ Introduction to Programming with C++ Fourth Edition.

Introduction to Programming with C++, Fourth Edition 24

Summary (continued)

• To create and execute a C++ program, you need to have a text editor and a C++ compiler

• Source code is C++ instructions you enter• The compiler translates source code into

machine code, or object code• Linker produces an executable file containing all

of the machine code to run your C++ program

Page 25: Chapter 3: Completing the Problem- Solving Process and Getting Started with C++ Introduction to Programming with C++ Fourth Edition.

Introduction to Programming with C++, Fourth Edition 25

Summary (continued)

• Programs have various components:– Comments

– Directives

– using statements

– Functions