C++ Programming: From Problem
Analysis to Program Design, Fifth Edition
Chapter 1: An Overview of
Computers and Programming
Languages
Elements of a Computer System
The Evolution of Programming Languages
◼ High-level languages include Basic, C, C++,
C#, Java, Python, FORTRAN, COBOL, and
Pascal.
◼ Compiler: translates a program written in a
high-level language to machine language.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition
Processing a C++ Program
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World!" << endl;
return 0;
}
Sample Run:
Hello World!
C++ Programming: From Problem Analysis to Program Design, Fifth Edition
Processing a C++ Program (cont'd.)
◼ To execute a C++ program:
❑ Use an editor to create a source program in C++
(.cpp file)
❑ Preprocessor directives begin with # and are
processed by the preprocessor
❑ Use the compiler to:
◼ Check that the program obeys the rules
◼ Translate into machine language (object program, i.e.,
.obj file)
C++ Programming: From Problem Analysis to Program Design, Fifth Edition
Processing a C++ Program (cont'd.)
◼ To execute a C++ program (cont'd.):
❑ Linker:
◼ Combines object program with other programs provided
by a library to create executable code (.exe file)
❑ Loader:
◼ Loads executable program into main memory
❑ The last step is to execute the program
C++ Programming: From Problem Analysis to Program Design, Fifth Edition
Processing a C++ Program (cont'd.)
C++ Programming: From Problem Analysis to Program Design, Fifth Edition
Programming with the Problem Analysis–
Coding–Execution Cycle
◼ Programming is a process of problem solving
◼ One problem-solving technique:
❑ Analyze the problem
❑ Outline the problem requirements
❑ Design steps (algorithm) to solve the problem
◼ Algorithm:
❑ Step-by-step problem-solving process
❑ Solution achieved in finite amount of time
C++ Programming: From Problem Analysis to Program Design, Fifth Edition
The Problem Analysis–Coding–Execution
Cycle (cont’d.)
C++ Programming: From Problem Analysis to Program Design, Fifth Edition
The Problem Analysis–Coding–Execution
Cycle (cont'd.)
◼ Run code through compiler
◼ If compiler generates errors
❑ Look at code and remove errors
❑ Run code again through compiler
◼ If there are no syntax errors
❑ Compiler generates equivalent machine code
◼ Linker links machine code with library files
C++ Programming: From Problem Analysis to Program Design, Fifth Edition
The Problem Analysis–Coding–Execution
Cycle (cont'd.)
◼ Once compiled and linked, loader can place
program into main memory for execution
◼ The final step is to execute the program
◼ Compiler guarantees that the program follows
the rules of the language
❑ Does not guarantee that the program will run
correctly
C++ Programming: From Problem Analysis to Program Design, Fifth Edition
Example 1-1
◼ Design an algorithm to find the perimeter and
area of a rectangle
◼ The perimeter and area of the rectangle are
given by the following formulas:
perimeter = 2 * (length + width)
area = length * width
C++ Programming: From Problem Analysis to Program Design, Fifth Edition
Example 1-1 (cont'd.)
◼ Algorithm:
❑ Get length of the rectangle
❑ Get width of the rectangle
❑ Find the perimeter using the following equation:
perimeter = 2 * (length + width)
❑ Find the area using the following equation:
area = length * width
C++ Programming: From Problem Analysis to Program Design, Fifth Edition