Fundamentals of Computer Programming: Dr. Tejada WWW-BCF - Usc.edu/ Stejada Week 1 Basic Elements of C++
Fundamentals of Computer Programming: Dr. Tejada WWW-BCF - Usc.edu/ Stejada Week 1 Basic Elements of C++
Fundamentals
of Computer
Programming
Dr. Tejada
[email protected]
www-bcf.usc.edu/~stejada
Week 1
Basic Elements of C++
10 Stacks of Coins
You have 10 stacks with 10 coins each that
look and feel identical.
9 of the 10 stacks contain coins that weigh 1
gram each while the 10th stack has coins that
weigh 1.1 grams each
Difference is undistinguishable by humans
You have a digital scale which measures the
number of grams of the total items on the
scale, accurate to 0.1 grams. You can only
use the scale ONCE!
Select a set of coins to put on the scale all at
once in such a way that you can identify which
stack has the heavy coins
2
10 Stacks of Coins Solution
Choose 1 coin from the 1st stack, 2 coins from the 2nd
stack, etc. and weigh that collection
The non-integer portion of the weight returned should
indicate which stack
3
4
C++
Programming
Computer
program
Sequence
of
statements
whose
objec9ve
is
to
accomplish
a
task
Programming
Process
of
planning
and
crea9ng
a
program
4
5
A C++ Program
5
6
6
7
7
8
8
9
9
10
10
11
Comments
Comments
are
for
the
reader,
not
the
compiler
Two
types:
Single
line:
begin
with
//
// This is a C++ program.
// Welcome to C++ Programming.
Mul9ple
line:
enclosed
between
/*
and
*/
/*
You can include comments that can
occupy several lines.
*/
C++ Programming: Program Design Including
Data Structures, Sixth Edition
11
12
Special Symbols
12
13
13
14
Iden9fiers
Iden9fier:
the
name
of
something
that
appears
in
a
program
Consists
of
leRers,
digits,
and
the
underscore
character
(_)
Must
begin
with
a
leRer
or
underscore
C++
is
case
sensi9ve
NUMBER
is
not
the
same
as
number
Two
predefined
iden9fiers
are
cout
and
cin
Unlike
reserved
words,
predefined
iden9fiers
may
be
redefined,
but
it
is
not
a
good
idea
C++ Programming: Program Design Including
Data Structures, Sixth Edition
14
15
Iden9fiers
Legal
iden9fiers
in
C++:
first
conversion
payRate
15
16
Whitespaces
Every
C++
program
contains
whitespaces
Include
blanks,
tabs,
and
newline
characters
16
17
Data
Types
Data
type:
set
of
values
together
with
a
set
of
opera9ons
C++
data
types
fall
into
three
categories:
Simple
data
type
Structured
data
type
Pointers
17
18
18
19
19
20
20
21
21
22
22
23
23
24
24
25
25
26
26
27
27
28
28
29
29
30
30
31
Expressions
Integral
expression:
all
operands
are
integers
Yields
an
integral
result
Example:
2 + 3 * 5
Floa9ng-‐point
expression:
all
operands
are
floa9ng-‐point
Yields
a
floa9ng-‐point
result
Example:
12.8 * 17.5 - 34.50
31
32
Mixed
Expressions
Mixed
expression:
Has
operands
of
different
data
types
Contains
integers
and
floa9ng-‐point
32
33
Mixed
Expressions
Evalua9on
rules:
If
operator
has
same
types
of
operands
Evaluated
according
to
the
type
of
the
operands
33
34
34
35
string
Type
Programmer-‐defined
type
supplied
in
ANSI/ISO
Standard
C++
library
Sequence
of
zero
or
more
characters
enclosed
in
double
quota9on
marks
Null
(or
empty):
a
string
with
no
characters
Each
character
has
a
rela9ve
posi9on
in
the
string
Posi9on
of
first
character
is
0
Length
of
a
string
is
number
of
characters
in
it
Example:
length
of
"William Jacob"
is
13
35
36
Syntax
to
declare
a
named
constant:
In
C++,
const
is
a
reserved
word
36
37
37
38
Assignment Statement
Expression
is
evaluated
and
its
value
is
assigned
to
the
variable
on
the
le\
side
A
variable
is
said
to
be
ini9alized
the
first
9me
a
value
is
placed
into
it
In
C++,
=
is
called
the
assignment
operator
38
39
Assignment Statement
39
40
40
41
41
42
42
43
43
44
44
45
x = 5; x = 5;
y = ++x; yC++=Programming:
x++; Program Design Including
Data Structures, Sixth Edition
45
46
Output
The
syntax
of
cout
and
<<
is:
46
47
Output
A
manipulator,
like
endl,
is
used
to
format
the
output
47
48
Preprocessor
Direc9ves
Many
func9ons
and
symbols
needed
to
run
a
C++
program
are
provided
as
collec9on
of
libraries
Every
library
has
a
name
and
is
referred
to
by
a
header
file
Preprocessor
direc9ves
are
commands
supplied
to
the
preprocessor
program
All
preprocessor
commands
begin
with
#
No
semicolon
at
the
end
of
these
commands
48
49
Preprocessor
Direc9ves
Syntax to include a header file:
49
50
50
51
51
52
52
53
53
54
Syntax
Syntax
rules:
indicate
what
is
legal
and
what
is
not
legal
Errors
in
syntax
are
found
in
compila9on
int x; //Line 1
int y //Line 2: error
double z; //Line 3
y = w + x; //Line 4: error
C++ Programming: Program Design Including
Data Structures, Sixth Edition
54
55
55
56
56
57
Seman9cs
Seman9cs:
set
of
rules
that
gives
meaning
to
a
language
Possible
to
remove
all
syntax
errors
in
a
program
and
s9ll
not
have
it
run
Even
if
it
runs,
it
may
s9ll
not
do
what
you
meant
it
to
do
Ex:
2 + 3 * 5
and
(2 + 3) * 5
are
both
syntac9cally
correct
expressions,
but
have
different
meanings
C++ Programming: Program Design Including
Data Structures, Sixth Edition
57
58
Naming
Iden9fiers
Iden9fiers
can
be
self-‐documen9ng:
CENTIMETERS_PER_INCH
58
59
Prompt
Lines
Prompt
lines:
executable
statements
that
inform
the
user
what
to
do
cout << "Please enter a number between 1 and
10 and "
<< "press the return key" << endl;
cin >> num;
59
60
Documenta9on
A
well-‐documented
program
is
easier
to
understand
and
modify
You
use
comments
to
document
programs
Comments
should
appear
in
a
program
to:
Explain
the
purpose
of
the
program
Iden9fy
who
wrote
it
Explain
the
purpose
of
par9cular
statements
60
61
61
62
62
63
63
64
#include <iostream>
using
namespace
std;
int
main()
{
cout
<<
”Hello,
World"
<<
endl;
return
0;
}
Sample
Run:
C++ Programming: Program Design Including
Hello, World Data Structures, Sixth Edition
64
65
Algorithm
Development
Algorithm:
Get
length
of
the
rectangle
perimeter = 2 * (length +
width)
Find
the
area
using
the
following
equa9on:
65
66
Algorithm
Development
Problem:
Calculate
students’
grades
and
class
average
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
66
67
Algorithm
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
67
68
Algorithm
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 C++ Programming: Program Design Including
grade = F Data Structures, Sixth Edition
68
69
Algorithm
Main
algorithm
is
as
follows:
totalAverage
=
0;
Repeat
the
following
for
each
student:
Get
student’s
data
(name
and
test
scores)
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
C++ Programming: Program Design Including
Data Structures, Sixth Edition
69
Algorithm Development:
Fibonacci
Number
Consider
the
following
sequence
of
numbers:
1,
1,
2,
3,
5,
8,
13,
21,
34,
....
70
70