C++ Programming ch01
C++ Programming ch01
C++ Programming ch01
2
Introduction
• Without software, the computer is useless
• Software is developed with programming languages
– C++ is a programming language
• C++ suited for a wide variety of programming tasks
5
The Language of a Computer
• Analog signals: continuous wave forms
• Digital signals: sequences of 0s and 1s
• Machine language: language of a computer; a
sequence of 0s and 1s
• Binary digit (bit): the digit 0 or 1
• Binary code (binary number): a sequence of 0s
and 1s
6
The Language of a Computer (cont’d.)
• Byte:
– A sequence of eight bits
• Kilobyte (KB): 210 bytes = 1024 bytes
• ASCII (American Standard Code for Information
Interchange)
– 128 characters
– A is encoded as 1000001 (65th character)
– 3 is encoded as 0110011
7
Processing a C++ Program
#include <iostream>
using namespace std;
int main()
{
cout << "My first C++ program." << endl;
return 0;
}
Sample Run:
My first C++ program.
8
Processing a C++ Program (cont’d.)
• To execute a C++ program:
– Use an editor to create a source program in C++
– Preprocessor directives begin with # and are processed by
the preprocessor
– Use the compiler to:
• Check that the program obeys the language rules
• Translate into machine language (object program)
9
Processing a C++ Program (cont’d.)
• To execute a C++ program (cont'd.):
– Linker:
• Combines object program with other programs provided by the
SDK to create executable code
• Library: contains prewritten code you can use
– Loader:
• Loads executable program into main memory
– The last step is to execute the program
• Some IDEs do all this with a Build or Rebuild
command
10
Processing a C++ Program (cont’d.)
11
Programming with the Problem
Analysis–Coding–Execution Cycle
• Algorithm:
– Step-by-step problem-solving
process
– Solution achieved in finite
amount of time
• Programming is a process of
problem solving
12
The Problem Analysis–Coding–
Execution Cycle (cont’d.)
• Step 1: Analyze the problem
– Outline the problem and its requirements
– Design steps (algorithm) to solve the problem
• Step 2: Implement the algorithm
– Implement the algorithm in code
– Verify that the algorithm works
• Step 3: Maintenance
– Use and modify the program if the problem domain
changes
13
The Problem Analysis–Coding–
Execution Cycle (cont’d.)
• Thoroughly understand the problem and all requirements
– Does program require user interaction?
– Does program manipulate data?
– What is the output?
14
The Problem Analysis–Coding–
Execution Cycle (cont’d.)
• Once the algorithm is designed and correctness
verified
– Write the equivalent code in high-level language
• Enter the program using text editor
15
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 system resources
16
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
17
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:
18
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
19
Example 1-3
• Design an algorithm to calculates the monthly
paycheck of a salesperson at a local department
store.
payCheck = baseSalary + bonus + additionalBonus
• Data:
– base salary
– The number of years that the salesperson has been with
the company
– The total sales made by the salesperson for that month
20
Example 1-3 (cont’d.)
• Suppose noOfServiceYears denotes the number of years that
the salesperson has been with the store
• Suppose bonus denotes the bonus.
21
Example 1-3 (cont’d.)
• Suppose totalSales denotes the total sales made by the
salesperson for the month
• Suppose additionalBonus denotes the additional bonus.
23
Example 1-5
• Calculate each student’s grade
– 10 students in a class; each student has taken five tests;
each test is worth 100 points
• Design algorithms to:
– Calculate the grade for each student and class average
– Find the average test score
– Determine the grade
• Data: students’ names; test scores
24
Example 1-5 (cont’d.)
• Algorithm to determine the average test score:
– Get the five test scores
– Add the five test scores
• Suppose sum stands for the sum of the test scores
– Suppose average stands for the average test score:
• average = sum / 5;
25
Example 1-5 (cont’d.)
• Algorithm to determine the grade:
if average is greater than or equal to 90
grade = A
otherwise
if average is greater than or equal to 80 and less than 90
grade = B
otherwise
if average is greater than or equal to 70 and less than 80
grade = C
otherwise
if average is greater than or equal to 60 and less than 70
grade = D
otherwise
grade = F
26
Example 1-5 (cont’d.)
• Main algorithm is as follows:
– totalAverage = 0;
– Repeat the following for each student:
• Get student’s name
• Use the algorithm to find the average test score
• Use the algorithm to find the grade
• Update totalAverage by adding current student’s average test
score
– Determine the class average as follows:
• classAverage = totalAverage / 10
27
Refer to text book and read
examples 1-2, and 1-4
28
Compiling C++ Code
• Offline IDEs
– Microsoft visual studio (will be explained in this slides)
– Eclipse
– Code::Blocks
– CodeLite
Microsoft visual studio 2015 home
screen
Before you create your first C++ project in Visual Studio, you
need to install Visual C++ 2015 Tools for Windows Desktop:
To create your HelloWorld project => File ->new-
>project, you can choose the Win32 Console Application
template, Name your project and click “ok”
When this window appears click next
Choose Empty project and click
finish
From solution explorer window, right click on
source files and choose to add new item as follows
Select C++ File, give it a name and
click add
Write your code in the .cpp file and click
on the green triangle to run your program
This window will appear