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

advertisement

Chapter 3:

Completing the Problem-

Solving Process and

Getting Started with C++

Introduction to Programming with C++

Fourth Edition

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

Introduction to Programming with C++, Fourth Edition 2

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

Introduction to Programming with C++, Fourth Edition 3

More on the Problem-Solving

Process

Introduction to Programming with C++, Fourth Edition 4

Coding the Algorithm into a Program

Introduction to Programming with C++, Fourth Edition 5

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

Introduction to Programming with C++, Fourth Edition 6

Coding the Algorithm into a Program

(continued)

Introduction to Programming with C++, Fourth Edition 7

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

Introduction to Programming with C++, Fourth Edition 8

Assigning Names, Data Types, and Initial

Values to the IPO Items (continued)

Introduction to Programming with C++, Fourth Edition 9

Translating the Algorithm Steps into C++ Code

Introduction to Programming with C++, Fourth Edition 10

Desk-Checking the Program

Introduction to Programming with C++, Fourth Edition 11

Desk-Checking the Program

(continued)

Introduction to Programming with C++, Fourth Edition 12

Desk-Checking the Program

(continued)

Introduction to Programming with C++, Fourth Edition 13

Desk-Checking the Program

(continued)

Introduction to Programming with C++, Fourth Edition 14

Desk-Checking the Program

(continued)

Introduction to Programming with C++, Fourth Edition 15

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

Introduction to Programming with C++, Fourth Edition 16

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

Introduction to Programming with C++, Fourth Edition 17

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

Introduction to Programming with C++, Fourth Edition 18

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

Introduction to Programming with C++, Fourth Edition 19

Process by which Source Code is

Translated into Executable Code

Introduction to Programming with C++, Fourth Edition 20

Introduction to Programming with C++, Fourth Edition 21

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

Introduction to Programming with C++, Fourth Edition 22

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

Introduction to Programming with C++, Fourth Edition 23

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

Introduction to Programming with C++, Fourth Edition 24

Summary (continued)

• Programs have various components:

– Comments

– Directives

– using statements

– Functions

Introduction to Programming with C++, Fourth Edition 25

Download