States… data type for the return value identifier for function list of arguments between parenthesis none for this function... a See the movie and buy a soda, b See the movie,
Trang 1Introduction to C++
Computer Science I
Trang 2instrument of science….”
Samuel Johnson
Trang 3Q: What is C++
C++ is a compiled, object-oriented language
It is the “successor” to C, a procedural
language
(the “++” is called the successor operator in C++)
C was derived from a language called B which was in turn derived from BCPL
C was developed in the 1970’s by Dennis
Ritchie of AT&T Bell Labs
C++ was developed in the early 1980’s by
Bjarne Stroustrup of AT&T Bell Labs.
Most of C is a subset of C++
Trang 4People & Programs
User: an individual who runs, or
executes, a program
Programmer: an individual who creates,
or writes, a program
Trang 5 Statements
Or executable statements, representing
actions the computer will take on the user’s behalf
Trang 6 Names for various entities used in a program; used for
Variables : values that can change frequently
Constants : values that never changes
Functions : programming units that represents complex operations
Parameters : values that change infrequently
Trang 7Tells the compiler what to do before
compiling
This one includes source code from another file
Trang 9 States…
data type for the return value
identifier for function
list of arguments between parenthesis (none for this
function)
Trang 10 They represent the start and end
of the function
Trang 11 Main body of function (or main part)
“//”
represents the start of a comment
Trang 12 specifies the value the function returns
All (almost) declarations and
statements end with a semi-colon
“;”
Trang 13do anything
!
Trang 14 The identifier number is declared as being of data type int, or integer
Trang 15 Note the direction of
“<<“
endl represents
an line
Trang 16 Note the direction of
“>>”
Trang 17 You had better, since this will be the first
program you’ll try!
Trang 18ns right now!
Trang 20 When a variable is declared, space is
allocated in the computer’s memory for the variable
Each data type requires a different number of bytes in memory for storing a variable
int - 2
float - 4
double - 8
char, bool - 1
Trang 21 When a variable is
assigned a value,
the value is placed
into the variable’s
memory location
10
Total
Total = 2 + 3 + 5;
Trang 25computed average
Do it!
You have 20 minutes!
Trang 26Computer Science I
Trang 28Q: What is a function?
It has
arguments
a name (identifier)
a value it
returns
a body int foo(int x)
{ int result;
result = x*x + 5*x + 7; return result;
}
Trang 29Procedural Abstraction
Think “ Black Box” !
When using a function, you only need
to be concerned with what it does, nothow it does it
When writing a function, you need to
be concerned with the how
Trang 30Example: Cube it!
int result;
result = x;
result = x*result; result = x*result; return result;
}
Trang 31 Looks like a programming language
Has all the structure of a programming language
Has a verrrrrry loose syntax
Trang 32 Example:
function foo (x)
result ← x 2 + 5x + 7 return result
That’s it!
Sloppy, ain’t it?
Trang 33Decision Statements
Computer Science I
Trang 35Decisions in Programs
Without decision statements (or other dynamic control structures), programs are static
Static programs do exactly the same things each time they are executed
Dynamic programs do not
Trang 36Boolean Algebra
Based on values that are either True or
False
True and False values are often
represented by 1’s and 0’s, respectively
Trang 37Logical Operations: And
Trang 38 Note: Also True
when A and B are
both True
T F
T T T
F T F
Trang 39Logical Operations: Exercises
A = True, B = True, C = False
1 A ∨ B
2 A ∧ C
3 A ∨ B ∧ C
4 (A ∧ B) ∨ (A ∧ C)
Trang 40Relational Operations
A < B “A less than B”
A > B “A greater than B”
A = B “A equal to B”
A ≤ B “A less than or equal to B”
“A not greater than B”
A ≥ B “A greater than or equal to B”
“A not less than B”
A ≠ B “A not equal to B”
“A less than or greater than B”
Trang 43Try this!
Problem:
❏ You’d like to go see a movie.
❏ The movie costs $8.00, a soda costs $2.50 and a large popcorn costs $4.50.
❏ Based on the amount of money in your
pocket, determine whether you could
(a) See the movie and buy a soda,
(b) See the movie, and buy soda and
popcorn, or
(c) Stay home
Trang 45 Cost of movie and soda
Cost of movie, soda and popcorn
Way to select one of the three options(that is, make a decision!)
Trang 47How about a diagram?
This is
called a
flowchart
Money < $15.00 Stay home
Movie & soda Movie, soda & popcornMoney < $10.50
Trang 48How about a diagram?
Boxes
represent
actions
Money < $15.00 Stay home
Movie & soda Movie, soda & popcornMoney < $10.50
Trang 49How about a diagram?
Movie & soda Movie, soda & popcornMoney < $10.50
Trang 50How about a diagram?
Arrows
show flow
Money < $15.00 Stay home
Movie & soda Movie, soda & popcornMoney < $10.50
Trang 51How about a diagram?
Movie & soda Movie, soda & popcornMoney < $10.50
Trang 52How would I write this?
Using Pseudocode
Wait!
What the CENSORED is Pseudocode?
Trang 53 Looks like a programming language
Has all the structure of a programming language
Has a verrrrrry loose syntax
Trang 54 Example:
get x result <- x 2 + 5x + 7 print result
That’s it!
Sloppy, ain’t it?
Trang 55One more time!
Pseudocode
If (Money < $10.50) then
Stay homeelse If (Money < $15.00) then
Movie, sodaelse Movie, soda, popcorn
Trang 56How would I write this?
First, we need to decide how to
organize our solution
Should we “hard code” the costs of the movie, soda and popcorn into the
algorithm?
Should we input these values?
Let’s take another look at that problem!
Trang 57How would I write this?
The problem
statement tells us
the individual costs
So, let’s assume
they’re fixed or
constant
No need to ask the
user for them
Problem:
❏ You’d like to go see a movie.
❏ The movie costs $8.00, a soda costs $2.50 and a large
popcorn costs $4.50.
❏ Based on the amount of money
in your pocket, determine whether you could
(a) See the movie and buy a soda,
(b) See the movie, and buy soda and popcorn, or
(c) Stay home
Trang 58How would I write this?
Another question: Should we pre-compute
the cost of each option?
Or, should we let the program do this?
Since we’ve already stated that the item costs are fixed, it would seem logical to pre-
compute the cost of each option
Movie: $8.00
Movie & soda: $10.50
All three: $15.00
Trang 59How would I write this?
Next, we need to make sure we have a complete algorithm
Input Money
If (Money < $10.50) then
Display “Stay home.”
else If (Money < $15.00) then
Display “Go to a movie;buy a soda.” else Display “Go to a movie; buy a
soda and popcorn.”
Almost done!
Trang 60How would I write this?
Determine how we wish to organize our program
Do we want one function?
Or, should we create a few functions?
Let’s two functions: One to input Money from the user
And a second to determine the outcome
Trang 61How would I write this?
Here’s the prototypes for the functionsint getMoney()
void showResults(int myMoney)
Trang 64Multiway Branching
Consider the following problem:
Each year, a local middle school
requires students to purchase supplies based on their grade level 6th graders need pencils and five notebooks 7th
graders also need a calculator 8th
graders add to this a 3-ring binder with loose leaf paper
Trang 65Multiway Branching
We could use a nested If statement to handle this, but there is an alternative
Whenever we need to represent a decision
step, with n possible outcomes, where
the outcomes form subsets of each other,
and/or
the outcomes are chosen based upon
unique scalar values for a control
expression,
we can use a Case (switch) structure
Trang 66Multiway Branching
Case
When Grade = 8th
3-ring binderloose leaf paperWhen Grade = 7th
calculatorWhen Grade = 6th
pencils
5 notebooks
Trang 67 When the switch is encountered, control jumps to the
matching case statement and continues until either a break is found or the end of the switch
Trang 68Multiway Branching
Here’s an example with a few break’s
cout << “Your lunch period comes “;
Trang 69Exercise :
Create a program that will inform the user which advisor they should go to based on their major code number
Well? Get started!
Trang 70Computer Science I
Trang 71Q: What is a Loop?
A control structure that allows for a sequence of steps to be repeated a certain number of times
This sequence of steps is called the
body of the loop
Trang 75repeated as long
as the condition
is true
Trang 76is bypassed, and flow continues with the next part of the
algorithm
Trang 77Example: Sequential search
k ← 0 found ← False while (k<size) ∧ ( ¬ found)
do if A[k] = target
then found ←
True
else k = k + 1
Trang 78Example: Sequential search
Trang 79 Strings are arrays of characters
How would you determine if a string fragment is part of a larger string?
(needle in a haystack?)
Trang 80Computer Science I
Trang 81Q: What is an array?
An array is a data structure consisting
of one or more indexed members
An array is like a row of mailboxes at the post office
Each box is numbered in sequence
(indices), and …
Each box contains the same type of stuff (datatype)
Trang 82An array could be drawn like
…
Trang 83An array could be drawn like
Trang 84An array could be drawn like
Trang 85In pseudocode we use …
X[j]
Where X is the name of the array
(identifier), and …
j is an index indicating which position
in the array we wish to address
Trang 86An array is declared by …
int X[10];
Where int is the common datatype for all
elements in the array,
elements are in the array
Indices for a C++ array always begin with 0
Trang 87Example: Student Grades
// Declare array
double stGrades[7];
: // Assign value to array element stGrades[5] = 87;
: // Display array element
cout << stGrades[3] << endl;
Trang 88 Create a program that will ask the user for three (3) numbers, determine the average, and then display the original numbers and the average
Hint: You might wish to use a loop as well as an array!
Trang 89Computer Science I
Trang 90What is a string?
A string is a sequence of characters
Example :
nc9*hNB98B&^v*&G
Blank spaces are characters
Each character requires one byte of storage
in memory
Each character is represented by a one byte character code, usually an ASCII code
Trang 92 length represents the length of the
string, or how many characters are in the sequence
Trang 94 cout is an output stream
cin is an input stream
Trang 95How about file I/O?
Input stream declaration
Output stream declaration
Trang 96How about file I/O?
Stream use
Closes files
Trang 97What if a file doesn’t exist?
Trang 98How about formatting?
showpos right/left
Number of decimal places Number of digits
Trang 99More member functions
get()
put()
eof()