Upload
sunnyopg
View
241
Download
0
Embed Size (px)
Citation preview
8/14/2019 Lecture 5 - Simple C++ Prog. Pt 1.pdf
1/20
1992-2012 by Pearson Education, Inc. & John Wiley & SonsSome portions are adopted from C++ f or Everyone by Horstmann
ENGR 1200U Introduction to Programming
Lecture 5
Simple C++ Programs (Chapter 2)
Dr. Eyhab Al-Masri
ENGR 1200UWinter 2013 - UOIT
/*--------------------------------------------------------
* Program chapter1_1
* This program computes the distance between two points.
*/
#include // Required for cout, endl.
#include // Required for sqrt()
using namespace std;
int main()
{
// Declare and initialize objects.
double x1(1), y1(5), x2(4), y2(7),
side1, side2, distance;
// Compute sides of a right triangle.
side1 = x2 - x1;
side2 = y2 - y1;
distance = sqrt(side1*side1 + side2*side2);
// Print distance.
cout
8/14/2019 Lecture 5 - Simple C++ Prog. Pt 1.pdf
2/20
ENGR 1200UWinter 2013 - UOIT
/*--------------------------------------------------------
* Program chapter1_1
* This program computes the distance between two points.
*/
#include // Required for cout, endl.
#include // Required for sqrt()
using namespace std;
int main()
{
// Declare and initialize objects.
double x1(1), y1(5), x2(4), y2(7),
side1, side2, distance;
// Compute sides of a right triangle.
side1 = x2 - x1;
side2 = y2 - y1;
distance = sqrt(side1*side1 + side2*side2);
// Print distance.
cout
8/14/2019 Lecture 5 - Simple C++ Prog. Pt 1.pdf
3/20
ENGR 1200UWinter 2013 - UOIT
/*--------------------------------------------------------
* Program chapter1_1
* This program computes the distance between two points.
*/
#include // Required for cout, endl.
#include // Required for sqrt()
using namespace std;
int main()
{
// Declare and initialize objects.
double x1(1), y1(5), x2(4), y2(7),
side1, side2, distance;
// Compute sides of a right triangle.
side1 = x2 - x1;
side2 = y2 - y1;
distance = sqrt(side1*side1 + side2*side2);
// Print distance.
cout
8/14/2019 Lecture 5 - Simple C++ Prog. Pt 1.pdf
4/20
ENGR 1200UWinter 2013 - UOIT
/*--------------------------------------------------------
* Program chapter1_1
* This program computes the distance between two points.
*/
#include // Required for cout, endl.
#include // Required for sqrt()
using namespace std;
int main()
{
// Declare and initialize objects.
double x1(1), y1(5), x2(4), y2(7),
side1, side2, distance;
// Compute sides of a right triangle.
side1 = x2 - x1;
side2 = y2 - y1;
distance = sqrt(side1*side1 + side2*side2);
// Print distance.
cout
8/14/2019 Lecture 5 - Simple C++ Prog. Pt 1.pdf
5/20
ENGR 1200UWinter 2013 - UOIT
/*--------------------------------------------------------
* Program chapter1_1
* This program computes the distance between two points.
*/
#include // Required for cout, endl.
#include // Required for sqrt()
using namespace std;
int main()
{
// Declare and initialize objects.
double x1(1), y1(5), x2(4), y2(7),
side1, side2, distance;
// Compute sides of a right triangle.
side1 = x2 - x1;
side2 = y2 - y1;
distance = sqrt(side1*side1 + side2*side2);
// Print distance.
cout
8/14/2019 Lecture 5 - Simple C++ Prog. Pt 1.pdf
6/20
ENGR 1200UWinter 2013 - UOIT
/*--------------------------------------------------------
* Program chapter1_1
* This program computes the distance between two points.
*/
#include // Required for cout, endl.
#include // Required for sqrt()
using namespace std;
int main()
{
// Declare and initialize objects.
double x1(1), y1(5), x2(4), y2(7),
side1, side2, distance;
// Compute sides of a right triangle.
side1 = x2 - x1 side2 = y2 - y1;
distance = sqrt(side1*side1 + side2*side2);
// Print distance.
cout
8/14/2019 Lecture 5 - Simple C++ Prog. Pt 1.pdf
7/20
ENGR 1200UWinter 2013 - UOIT
/*--------------------------------------------------------
* Program chapter1_1
* This program computes the distance between two points.
*/
#include // Required for cout, endl.
#include // Required for sqrt()
using namespace std;
int main()
{
// Declare and initialize objects.
double x1(1), y1(5), x2(4), y2(7),
side1, side2, distance;
// Compute sides of a right triangle.
side1 = x2 - x1;
side2 = y2 - y1;
distance = sqrt(side1*side1 + side2*side2);
// Print distance.
cout
8/14/2019 Lecture 5 - Simple C++ Prog. Pt 1.pdf
8/20
ENGR 1200UWinter 2013 - UOIT
/*--------------------------------------------------------
* Program chapter1_1
* This program computes the distance between two points.
*/
#include // Required for cout, endl.
#include // Required for sqrt()
using namespace std;
int main()
{
// Declare and initialize objects.
double x1(1), y1(5), x2(4), y2(7),
side1, side2, distance;
// Compute sides of a right triangle.
side1 = x2 - x1;
side2 = y2 / 0;
distance = sqrt(side1*side1 + side2*side2);
// Print distance.
cout
8/14/2019 Lecture 5 - Simple C++ Prog. Pt 1.pdf
9/20
ENGR 1200UWinter 2013 - UOIT
The characters \n are not printed on the screen
The backslash (\) is called an escape character. It indicates that a special character is to be output
The escape sequence \n means newline Causes the cursor to move to the beginning of the
next line on the screen
When the return statement is used at the end of main
the value 0 indicates that the program has terminatedsuccessfully
ENGR 1200UWinter 2013 - UOIT
8/14/2019 Lecture 5 - Simple C++ Prog. Pt 1.pdf
10/201
ENGR 1200UWinter 2013 - UOIT
For each problemthe programmer goesthrough these steps
Algorithms No!An algorithm is
aRECIPE
ENGR 1200UWinter 2013 - UOIT
Any solvable computing problem can be solvedby the execution of a series of actions in aspecific order
An algorithm is a procedure for solving aproblem in terms of
the actions to execute and
the order in which the actions execute
Specifying the order in which statements(actions) execute in a computer program iscalled program control
8/14/2019 Lecture 5 - Simple C++ Prog. Pt 1.pdf
11/201
ENGR 1200UWinter 2013 - UOIT
Pseudocode is an artificial and informal languagethat helps you develop algorithms. Similar to everyday English
Convenient and user friendly.
Helps you think out a program beforeattempting to write it.
Carefully prepared pseudocode can easily beconverted to a corresponding C++ program.
ENGR 1200UWinter 2013 - UOIT
C++ Standard Library C++ programs consist of pieces called classes and
functions
Most C++ programmers take advantage of the richcollections of classes and functions in the C++ StandardLibrary
Two parts to learn the C++ world The C++ language itself, How to use the classes and functions in the C++
Standard Library
8/14/2019 Lecture 5 - Simple C++ Prog. Pt 1.pdf
12/201
ENGR 1200UWinter 2013 - UOIT
C++ systems consist of three parts: Program development environment Language C++ Standard Library
Typically, there are six phases in which C++programs go through Edit Preprocess Compile Link
Load, and Execute
ENGR 1200UWinter 2013 - UOIT
Data type double is for specifying real numbers,and data type char for specifying character data.
Real numbers are numbers with decimal points,such as 3.4, 0.0 and 11.19
A char variable may hold only a singlelowercase letter, a single uppercase letter, asingle digit or a single special character (e.g., $
or *) Types such as int, double and char are called
fundamental data types
8/14/2019 Lecture 5 - Simple C++ Prog. Pt 1.pdf
13/201
ENGR 1200UWinter 2013 - UOIT
A number writtenby a programmer
is called anumber li teral.
There arerules for
writing literalvalues:
ENGR 1200UWinter 2013 - UOIT
Keyword Example of a constant bool t r ue
char ' 5'
i nt 25
doubl e 25. 0
st r i ng "hel l o" //#i ncl ude
Fundamental Data Types contd)
8/14/2019 Lecture 5 - Simple C++ Prog. Pt 1.pdf
14/201
1992-2012 by Pearson Education, Inc. & John Wiley & SonsSome portions are adopted from C++ f or Everyone by Horstmann
Constants and Variables
ENGR 1200UWinter 2013 - UOIT
Constants and variables represent memory locations that are usedto store information
Constants are objects that store specific data that can not bemodified. 10 is an integer constant
4. 5 is a floating point constant
"The di st ance between the t wo poi nt s i s" is a string constant
'a' is a character constant
Variables are named memory locations that store values that canbe modified. doubl e x1( 1. 0) , x2( 4. 5) , si de1;
si de1 = x2 - x1;
x1, x2 and si de1 are examples of variables that can be modified.
8/14/2019 Lecture 5 - Simple C++ Prog. Pt 1.pdf
15/201
ENGR 1200UWinter 2013 - UOIT
Must begin with an alphabetic character or the
underscore character _
You cannot use other symbols such as $ or %. Spaces
are not permitted inside names;
Alphabetic characters may be either upper or lower case
C++ is CASE SENSITIVE, so a != A, etc May contain digits, but not as the first character
May NOT be C++ keywords
ENGR 1200UWinter 2013 - UOIT
8/14/2019 Lecture 5 - Simple C++ Prog. Pt 1.pdf
16/201
ENGR 1200UWinter 2013 - UOIT
The following statement defines a variable.
intcans_per_pack= 6;
cans_per_packis the variables name.
int
indicates that the variable cans_per_pack
will be used to hold integers.
= 6
indicates that the variable cans_per_pack
will initially contain the value 6.
ENGR 1200UWinter 2013 - UOIT
8/14/2019 Lecture 5 - Simple C++ Prog. Pt 1.pdf
17/201
ENGR 1200UWinter 2013 - UOIT
You should pick a variable name that explains itspurpose A good name describes the contents of the variable or
what the variable will be used for
For example, it is better to use a descriptive name,such as can_volume, than a terse name, such as cv.
Must be declared (and therefore typed) beforethey may be used
C++ is a strongly typed programming language Every variable must be declared before usage
ENGR 1200UWinter 2013 - UOIT
When creating variables, the programmerspecifies the type of information to be stored(data type)
C++ does not provide initial values for variables Initialization is putting a value into a variable when it is
created
Initialization is not required
Using the value of a variable before it is initialized mayresult in garbage
8/14/2019 Lecture 5 - Simple C++ Prog. Pt 1.pdf
18/201
ENGR 1200UWinter 2013 - UOIT
Bits: smallest data item takes value of 0 or 1
Characters: decimal digits (0-9), letters (A-Z), and specialcharacters (i.e. $, %, @, #, &, *, (, ), , :,;,-,/)
Fields: Group of characters or bytes that convey meaning (i.e.persons name, car model, etc)
Records: Group of related fields
File: a group of related records (contains arbitrary data
and arbitrary format) Some OSs view files as sequence of bytes
ENGR 1200UWinter 2013 - UOIT
8/14/2019 Lecture 5 - Simple C++ Prog. Pt 1.pdf
19/201
ENGR 1200UWinter 2013 - UOIT
Memory snapshots are diagrams that show thetypes and contents of variables at a particularpoint in time.
ENGR 1200UWinter 2013 - UOIT
8/14/2019 Lecture 5 - Simple C++ Prog. Pt 1.pdf
20/20
ENGR 1200UWinter 2013 - UOIT
A type declaration statement defines new identifiers andallocates memory.
An initial value may be assigned to a memory location atthe time an identifier is defined.
Syntax[modifier] type specifier identifier [= initial value];[modifier] type specifier identifier[ initial value)];Examples
double x1, y1(0);int counter=0;const int MIN_SIZE=0;bool error(false);char comma(',');