introduction to c++
DESCRIPTION
Summary Of C++ in an organized wayTRANSCRIPT
![Page 1: Introduction to C++](https://reader033.vdocuments.site/reader033/viewer/2022061202/546b474baf795919088b6de7/html5/thumbnails/1.jpg)
Introduction to C++
Computer Science I
![Page 2: Introduction to C++](https://reader033.vdocuments.site/reader033/viewer/2022061202/546b474baf795919088b6de7/html5/thumbnails/2.jpg)
Q: 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++
![Page 3: Introduction to C++](https://reader033.vdocuments.site/reader033/viewer/2022061202/546b474baf795919088b6de7/html5/thumbnails/3.jpg)
People & ProgramsUser: an individual who runs, or
executes, a programProgrammer: an individual who
creates, or writes, a program
![Page 4: Introduction to C++](https://reader033.vdocuments.site/reader033/viewer/2022061202/546b474baf795919088b6de7/html5/thumbnails/4.jpg)
C++ Program
Consists of…Declarations
Define the use of various identifiers, thus creating the elements used by the program (computer)
StatementsOr executable statements,
representing actions the computer will take on the user’s behalf
![Page 5: Introduction to C++](https://reader033.vdocuments.site/reader033/viewer/2022061202/546b474baf795919088b6de7/html5/thumbnails/5.jpg)
Identifiers 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
![Page 6: Introduction to C++](https://reader033.vdocuments.site/reader033/viewer/2022061202/546b474baf795919088b6de7/html5/thumbnails/6.jpg)
Simple C++ Program#include <iostream.h>
int main()
{
// Declarations
// Statements
return 0;
}
Compiler directive: Tells the compiler what to do before compiling
This one includes source code from another file
![Page 7: Introduction to C++](https://reader033.vdocuments.site/reader033/viewer/2022061202/546b474baf795919088b6de7/html5/thumbnails/7.jpg)
Simple C++ Program#include <iostream.h>
int main()
{
// Declarations
// Statements
return 0;
}
Main function
![Page 8: Introduction to C++](https://reader033.vdocuments.site/reader033/viewer/2022061202/546b474baf795919088b6de7/html5/thumbnails/8.jpg)
Simple C++ Program#include <iostream.h>
int main()
{
// Declarations
// Statements
return 0;
}
Header for main function
States… data type
for the return value
identifier for function
list of arguments between parenthesis(none for this function)
![Page 9: Introduction to C++](https://reader033.vdocuments.site/reader033/viewer/2022061202/546b474baf795919088b6de7/html5/thumbnails/9.jpg)
Simple C++ Program#include <iostream.h>
int main()
{
// Declarations
// Statements
return 0;
}
Braces enclose the body of the function
They represent the start and end of the function
![Page 10: Introduction to C++](https://reader033.vdocuments.site/reader033/viewer/2022061202/546b474baf795919088b6de7/html5/thumbnails/10.jpg)
Simple C++ Program#include <iostream.h>
int main()
{
// Declarations
// Statements
return 0;
}
Declarations and statements
Main body of function (or main part)
“//” represents the start of a comment
![Page 11: Introduction to C++](https://reader033.vdocuments.site/reader033/viewer/2022061202/546b474baf795919088b6de7/html5/thumbnails/11.jpg)
Simple C++ Program#include <iostream.h>
int main()
{
// Declarations
// Statements
return 0;
}
Return statement
specifies the value the function returns
All (almost) declarations and statements end with a semi-colon “;”
![Page 12: Introduction to C++](https://reader033.vdocuments.site/reader033/viewer/2022061202/546b474baf795919088b6de7/html5/thumbnails/12.jpg)
Simple C++ Program#include <iostream.h>
int main()
{
// Declarations
// Statements
return 0;
}
This program doesn’t do anything!
![Page 13: Introduction to C++](https://reader033.vdocuments.site/reader033/viewer/2022061202/546b474baf795919088b6de7/html5/thumbnails/13.jpg)
Sample C++ Program#include <iostream.h>
void main()
{
int number;
cout << “Enter a number” << endl;
cin >> number;
cout << “You entered: “ << number << endl;
}
Variable declaration
The identifier number is declared as being of data type int, or integer
![Page 14: Introduction to C++](https://reader033.vdocuments.site/reader033/viewer/2022061202/546b474baf795919088b6de7/html5/thumbnails/14.jpg)
Sample C++ Program#include <iostream.h>
void main()
{
int number;
cout << “Enter a number” << endl;
cin >> number;
cout << “You entered: “ << number << endl;
}
coutthe output statement for C++
Note the direction of “<<“
endl represents an end-of-line
![Page 15: Introduction to C++](https://reader033.vdocuments.site/reader033/viewer/2022061202/546b474baf795919088b6de7/html5/thumbnails/15.jpg)
Sample C++ Program#include <iostream.h>
void main()
{
int number;
cout << “Enter a number” << endl;
cin >> number;
cout << “You entered: “ << number << endl;
}
cinthe input statement for C++
Note the direction of “>>”
![Page 16: Introduction to C++](https://reader033.vdocuments.site/reader033/viewer/2022061202/546b474baf795919088b6de7/html5/thumbnails/16.jpg)
Sample C++ Program#include <iostream.h>
void main()
{
int number;
cout << “Enter a number” << endl;
cin >> number;
cout << “You entered: “ << number << endl;
}
Did you copy this down?
You had better, since this will be the first program you’ll try!
![Page 17: Introduction to C++](https://reader033.vdocuments.site/reader033/viewer/2022061202/546b474baf795919088b6de7/html5/thumbnails/17.jpg)
Sample C++ Program#include <iostream.h>
void main()
{
int number;
cout << “Enter a number” << endl;
cin >> number;
cout << “You entered: “ << number << endl;
}
That means right now!
![Page 18: Introduction to C++](https://reader033.vdocuments.site/reader033/viewer/2022061202/546b474baf795919088b6de7/html5/thumbnails/18.jpg)
AssignmentAssignment is an operation that
assigns the value of an expression to a variable
Ex.Total = 2 + 3 + 5
First, the expresssion “2 + 3 + 5” is evaluated
Then, this value is assigned to the variable “Total”
![Page 19: Introduction to C++](https://reader033.vdocuments.site/reader033/viewer/2022061202/546b474baf795919088b6de7/html5/thumbnails/19.jpg)
Assignment 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 - 2float - 4double - 8char, bool - 1
![Page 20: Introduction to C++](https://reader033.vdocuments.site/reader033/viewer/2022061202/546b474baf795919088b6de7/html5/thumbnails/20.jpg)
AssignmentWhen a variable
is assigned a value, the value is placed into the variable’s memory location
10
Total
Total = 2 + 3 + 5;
![Page 21: Introduction to C++](https://reader033.vdocuments.site/reader033/viewer/2022061202/546b474baf795919088b6de7/html5/thumbnails/21.jpg)
Arithmetic OperationsAddition: 2 + 3Subtraction: 5 - 2Multiplication: 10 * 4Division: 12 / 3
![Page 22: Introduction to C++](https://reader033.vdocuments.site/reader033/viewer/2022061202/546b474baf795919088b6de7/html5/thumbnails/22.jpg)
Order of OperationsArithmetic expressions are
evaluated according to the following order of operations
At each level, operations are evaluated left to right
(1) Parenthesis, Functions(2) Multiplication, Division(3) Addition, Subtraction
![Page 23: Introduction to C++](https://reader033.vdocuments.site/reader033/viewer/2022061202/546b474baf795919088b6de7/html5/thumbnails/23.jpg)
ParenthesisParenthesis are used to alter the
order with which operations are evaluated
Ex.4 + 5 * 2 equals 14(4 + 5) * 2 equals 18
![Page 24: Introduction to C++](https://reader033.vdocuments.site/reader033/viewer/2022061202/546b474baf795919088b6de7/html5/thumbnails/24.jpg)
Here we go! Problem: To determine the average of
three numbers Task: Request, from the user, three
numbers, compute the average and the three numbers, and print out the original values and the computed average
Do it! You have 20 minutes!
![Page 25: Introduction to C++](https://reader033.vdocuments.site/reader033/viewer/2022061202/546b474baf795919088b6de7/html5/thumbnails/25.jpg)
FunctionsComputer Science I
![Page 26: Introduction to C++](https://reader033.vdocuments.site/reader033/viewer/2022061202/546b474baf795919088b6de7/html5/thumbnails/26.jpg)
Q: What is a function?A programming unitSimilar to mathematical functionsExample:
f(x) = x2 + 5x + 7For x = 2, f(2) = (2)2 =5(2) + 7 = 21
![Page 27: Introduction to C++](https://reader033.vdocuments.site/reader033/viewer/2022061202/546b474baf795919088b6de7/html5/thumbnails/27.jpg)
Q: What is a function? It has...
... arguments ... a name
(identifier) ... a value it
returns ... a bodyint foo(int x)
{int result;result = x*x + 5*x + 7;return result;}
![Page 28: Introduction to C++](https://reader033.vdocuments.site/reader033/viewer/2022061202/546b474baf795919088b6de7/html5/thumbnails/28.jpg)
Procedural AbstractionThink “Black Box” !When using a function, you only
need to be concerned with what it does, not how it does it
When writing a function, you need to be concerned with the how
![Page 29: Introduction to C++](https://reader033.vdocuments.site/reader033/viewer/2022061202/546b474baf795919088b6de7/html5/thumbnails/29.jpg)
Example: Cube it!int cubeIt(int x){
int result;result = x*x*x;return result;
}
int cubeIt(int x){
int result;result = x;result = x*result;result = x*result;return result;
}
![Page 30: Introduction to C++](https://reader033.vdocuments.site/reader033/viewer/2022061202/546b474baf795919088b6de7/html5/thumbnails/30.jpg)
PseudocodeLooks like a programming
languageHas all the structure of a
programming languageHas a verrrrrry loose syntax
![Page 31: Introduction to C++](https://reader033.vdocuments.site/reader033/viewer/2022061202/546b474baf795919088b6de7/html5/thumbnails/31.jpg)
PseudocodeExample:
function foo (x)result x2 + 5x + 7return result
That’s it!Sloppy, ain’t it?
![Page 32: Introduction to C++](https://reader033.vdocuments.site/reader033/viewer/2022061202/546b474baf795919088b6de7/html5/thumbnails/32.jpg)
Decision StatementsComputer Science I
![Page 33: Introduction to C++](https://reader033.vdocuments.site/reader033/viewer/2022061202/546b474baf795919088b6de7/html5/thumbnails/33.jpg)
Q: What is a decision?Something that represents a
branching point in a solutionOutcomes are often dependent on
initial conditions
![Page 34: Introduction to C++](https://reader033.vdocuments.site/reader033/viewer/2022061202/546b474baf795919088b6de7/html5/thumbnails/34.jpg)
Decisions in ProgramsWithout 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
![Page 35: Introduction to C++](https://reader033.vdocuments.site/reader033/viewer/2022061202/546b474baf795919088b6de7/html5/thumbnails/35.jpg)
Boolean AlgebraBased on values that are either
True or FalseTrue and False values are often
represented by 1’s and 0’s, respectively
![Page 36: Introduction to C++](https://reader033.vdocuments.site/reader033/viewer/2022061202/546b474baf795919088b6de7/html5/thumbnails/36.jpg)
Logical Operations: AndA BExpression is
True iff A and B are both true
T FT T FF F F
![Page 37: Introduction to C++](https://reader033.vdocuments.site/reader033/viewer/2022061202/546b474baf795919088b6de7/html5/thumbnails/37.jpg)
Logical Operations: OrA BExpression is
True if either A or B are True
Note: Also True when A and B are both True
T FT T TF T F
![Page 38: Introduction to C++](https://reader033.vdocuments.site/reader033/viewer/2022061202/546b474baf795919088b6de7/html5/thumbnails/38.jpg)
Logical Operations: Exercises
A = True, B = True, C = False1. A B2. A C3. A B C4. (A B) (A C)
![Page 39: Introduction to C++](https://reader033.vdocuments.site/reader033/viewer/2022061202/546b474baf795919088b6de7/html5/thumbnails/39.jpg)
Relational 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”
![Page 40: Introduction to C++](https://reader033.vdocuments.site/reader033/viewer/2022061202/546b474baf795919088b6de7/html5/thumbnails/40.jpg)
Relational Operations: Exercises
A = 5, B = 3, C = -71. A < B2. A C3. (A < C) (B < C)
![Page 41: Introduction to C++](https://reader033.vdocuments.site/reader033/viewer/2022061202/546b474baf795919088b6de7/html5/thumbnails/41.jpg)
Boolean Operations: C++ A B A B A < B A > B A = B A B A B A B
A && B A | | B A < B A > B A = = B A > = B A < = B A < > B
![Page 42: Introduction to C++](https://reader033.vdocuments.site/reader033/viewer/2022061202/546b474baf795919088b6de7/html5/thumbnails/42.jpg)
Try 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
![Page 43: Introduction to C++](https://reader033.vdocuments.site/reader033/viewer/2022061202/546b474baf795919088b6de7/html5/thumbnails/43.jpg)
Know?Movie costs $8.00Soda costs $2.50Popcorn costs $4.50How much money I have in my
![Page 44: Introduction to C++](https://reader033.vdocuments.site/reader033/viewer/2022061202/546b474baf795919088b6de7/html5/thumbnails/44.jpg)
Need?Cost of movie and sodaCost of movie, soda and popcornWay to select one of the three
options(that is, make a decision!)
![Page 45: Introduction to C++](https://reader033.vdocuments.site/reader033/viewer/2022061202/546b474baf795919088b6de7/html5/thumbnails/45.jpg)
Do?Option (a) costs $10.50Option (b) costs $15.00Option (c) costs nothingWhat next?
![Page 46: Introduction to C++](https://reader033.vdocuments.site/reader033/viewer/2022061202/546b474baf795919088b6de7/html5/thumbnails/46.jpg)
How about a diagram? This is
called a flowchart
Money < $15.00Stay home
Movie & sodaMovie, soda & popcorn
Money < $10.50
![Page 47: Introduction to C++](https://reader033.vdocuments.site/reader033/viewer/2022061202/546b474baf795919088b6de7/html5/thumbnails/47.jpg)
How about a diagram? Boxes
represent actions
Money < $15.00Stay home
Movie & sodaMovie, soda & popcorn
Money < $10.50
![Page 48: Introduction to C++](https://reader033.vdocuments.site/reader033/viewer/2022061202/546b474baf795919088b6de7/html5/thumbnails/48.jpg)
How about a diagram? Diamonds
represent decision points
Money < $15.00Stay home
Movie & sodaMovie, soda & popcorn
Money < $10.50
![Page 49: Introduction to C++](https://reader033.vdocuments.site/reader033/viewer/2022061202/546b474baf795919088b6de7/html5/thumbnails/49.jpg)
How about a diagram? Arrows
show flow
Money < $15.00Stay home
Movie & sodaMovie, soda & popcorn
Money < $10.50
![Page 50: Introduction to C++](https://reader033.vdocuments.site/reader033/viewer/2022061202/546b474baf795919088b6de7/html5/thumbnails/50.jpg)
How about a diagram? The
arrow at the top tells us there were previous steps
The arrow at the bottom tells us there are subsequent steps
Money < $15.00Stay home
Movie & sodaMovie, soda & popcorn
Money < $10.50
![Page 51: Introduction to C++](https://reader033.vdocuments.site/reader033/viewer/2022061202/546b474baf795919088b6de7/html5/thumbnails/51.jpg)
How would I write this?Using PseudocodeWait!What the CENSORED is
Pseudocode?
![Page 52: Introduction to C++](https://reader033.vdocuments.site/reader033/viewer/2022061202/546b474baf795919088b6de7/html5/thumbnails/52.jpg)
PseudocodeLooks like a programming
languageHas all the structure of a
programming languageHas a verrrrrry loose syntax
![Page 53: Introduction to C++](https://reader033.vdocuments.site/reader033/viewer/2022061202/546b474baf795919088b6de7/html5/thumbnails/53.jpg)
PseudocodeExample:
get xresult <- x2 + 5x + 7print result
That’s it!Sloppy, ain’t it?
![Page 54: Introduction to C++](https://reader033.vdocuments.site/reader033/viewer/2022061202/546b474baf795919088b6de7/html5/thumbnails/54.jpg)
One more time!Pseudocode...
If (Money < $10.50) thenStay homeelse If (Money < $15.00) then
Movie, sodaelse Movie, soda, popcorn
![Page 55: Introduction to C++](https://reader033.vdocuments.site/reader033/viewer/2022061202/546b474baf795919088b6de7/html5/thumbnails/55.jpg)
How would I write this?First, we need to decide how to
organize our solutionShould 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!
![Page 56: Introduction to C++](https://reader033.vdocuments.site/reader033/viewer/2022061202/546b474baf795919088b6de7/html5/thumbnails/56.jpg)
How 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
![Page 57: Introduction to C++](https://reader033.vdocuments.site/reader033/viewer/2022061202/546b474baf795919088b6de7/html5/thumbnails/57.jpg)
How 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 optionMovie: $8.00Movie & soda: $10.50All three: $15.00
![Page 58: Introduction to C++](https://reader033.vdocuments.site/reader033/viewer/2022061202/546b474baf795919088b6de7/html5/thumbnails/58.jpg)
How would I write this?Next, we need to make sure we
have a complete algorithmInput MoneyIf (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!
![Page 59: Introduction to C++](https://reader033.vdocuments.site/reader033/viewer/2022061202/546b474baf795919088b6de7/html5/thumbnails/59.jpg)
How would I write this?Determine how we wish to
organize our programDo 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
![Page 60: Introduction to C++](https://reader033.vdocuments.site/reader033/viewer/2022061202/546b474baf795919088b6de7/html5/thumbnails/60.jpg)
How would I write this?Here’s the prototypes for the
functions
int getMoney()
void showResults(int myMoney)
![Page 61: Introduction to C++](https://reader033.vdocuments.site/reader033/viewer/2022061202/546b474baf795919088b6de7/html5/thumbnails/61.jpg)
ProgramOkay, now we get to use our
algorithm and program design to create a program
Well, what are you waiting for?
Do It!!!
![Page 62: Introduction to C++](https://reader033.vdocuments.site/reader033/viewer/2022061202/546b474baf795919088b6de7/html5/thumbnails/62.jpg)
Multiway BranchingIf statements can be used for
multiway branchingThat is, choosing one of n mutually
exclusive outcomesBut what about n outcomes that
are not totally unique?
![Page 63: Introduction to C++](https://reader033.vdocuments.site/reader033/viewer/2022061202/546b474baf795919088b6de7/html5/thumbnails/63.jpg)
Multiway BranchingConsider 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.
![Page 64: Introduction to C++](https://reader033.vdocuments.site/reader033/viewer/2022061202/546b474baf795919088b6de7/html5/thumbnails/64.jpg)
Multiway 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/orthe outcomes are chosen based upon
unique scalar values for a control expression,
we can use a Case (switch) structure
![Page 65: Introduction to C++](https://reader033.vdocuments.site/reader033/viewer/2022061202/546b474baf795919088b6de7/html5/thumbnails/65.jpg)
Multiway BranchingCase
When Grade = 8th3-ring binderloose leaf paper
When Grade = 7thcalculator
When Grade = 6thpencils5 notebooks
![Page 66: Introduction to C++](https://reader033.vdocuments.site/reader033/viewer/2022061202/546b474baf795919088b6de7/html5/thumbnails/66.jpg)
Multiway Branching In C++ ...switch (grade){
case 8:cout << “3-ring binder, loose leaf, “;
case 7:cout << “calculator, “;
case 6:cout << “5 notebooks, & pencils.” <<
endl;} 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
![Page 67: Introduction to C++](https://reader033.vdocuments.site/reader033/viewer/2022061202/546b474baf795919088b6de7/html5/thumbnails/67.jpg)
Multiway BranchingHere’s an example with a few break’s
cout << “Your lunch period comes “;switch (grade) {
case 8:cout << “first.” << endl;break;
case 7:cout << “second.” << endl;break;
case 6:cout << “third.” << endl;
}
No final break
![Page 68: Introduction to C++](https://reader033.vdocuments.site/reader033/viewer/2022061202/546b474baf795919088b6de7/html5/thumbnails/68.jpg)
Exercise:Create a program that will inform
the user which advisor they should go to based on their major code number
Well? Get started!
![Page 69: Introduction to C++](https://reader033.vdocuments.site/reader033/viewer/2022061202/546b474baf795919088b6de7/html5/thumbnails/69.jpg)
LoopsComputer Science I
![Page 70: Introduction to C++](https://reader033.vdocuments.site/reader033/viewer/2022061202/546b474baf795919088b6de7/html5/thumbnails/70.jpg)
Q: 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
![Page 71: Introduction to C++](https://reader033.vdocuments.site/reader033/viewer/2022061202/546b474baf795919088b6de7/html5/thumbnails/71.jpg)
Q: What is a Loop?There are three basic loop
structures in programming:ForWhileRepeat
![Page 72: Introduction to C++](https://reader033.vdocuments.site/reader033/viewer/2022061202/546b474baf795919088b6de7/html5/thumbnails/72.jpg)
While loop
Condition
Body
T
F
Look familiar?
What if you added a change step to the end of the body?
![Page 73: Introduction to C++](https://reader033.vdocuments.site/reader033/viewer/2022061202/546b474baf795919088b6de7/html5/thumbnails/73.jpg)
For loop
Condition
Body
T
F
![Page 74: Introduction to C++](https://reader033.vdocuments.site/reader033/viewer/2022061202/546b474baf795919088b6de7/html5/thumbnails/74.jpg)
While loop
Condition
Body
T
F
A while loop is a control structure where the body is repeated as long as the condition is true
![Page 75: Introduction to C++](https://reader033.vdocuments.site/reader033/viewer/2022061202/546b474baf795919088b6de7/html5/thumbnails/75.jpg)
While loop
Condition
Body
T
F
When the condition is false, the body is bypassed, and flow continues with the next part of the algorithm
![Page 76: Introduction to C++](https://reader033.vdocuments.site/reader033/viewer/2022061202/546b474baf795919088b6de7/html5/thumbnails/76.jpg)
Example: Sequential search
k 0found Falsewhile (k<size) (found)
do if A[k] = target
then found True
else k = k + 1
(k<size) (found)
if A[k] = targetthen found Trueelse k = k + 1
T
F
![Page 77: Introduction to C++](https://reader033.vdocuments.site/reader033/viewer/2022061202/546b474baf795919088b6de7/html5/thumbnails/77.jpg)
Example: Sequential search
k = 0;
found = False;
while ((k<size) && (!found))
if (A[k] == target)
found = True;
else k = k + 1;
(k<size) (found)
if A[k] = targetthen found Trueelse k = k + 1
T
F
![Page 78: Introduction to C++](https://reader033.vdocuments.site/reader033/viewer/2022061202/546b474baf795919088b6de7/html5/thumbnails/78.jpg)
ExerciseStrings are arrays of charactersHow would you determine if a
string fragment is part of a larger string?(needle in a haystack?)
![Page 79: Introduction to C++](https://reader033.vdocuments.site/reader033/viewer/2022061202/546b474baf795919088b6de7/html5/thumbnails/79.jpg)
ArraysComputer Science I
![Page 80: Introduction to C++](https://reader033.vdocuments.site/reader033/viewer/2022061202/546b474baf795919088b6de7/html5/thumbnails/80.jpg)
Q: 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)
![Page 81: Introduction to C++](https://reader033.vdocuments.site/reader033/viewer/2022061202/546b474baf795919088b6de7/html5/thumbnails/81.jpg)
An array could be drawn like …
g d a f c z l
0 1 2 3 4 5 6
![Page 82: Introduction to C++](https://reader033.vdocuments.site/reader033/viewer/2022061202/546b474baf795919088b6de7/html5/thumbnails/82.jpg)
An array could be drawn like …
g d a f c z l
0 1 2 3 4 5 6
Each box is numbered in sequence
![Page 83: Introduction to C++](https://reader033.vdocuments.site/reader033/viewer/2022061202/546b474baf795919088b6de7/html5/thumbnails/83.jpg)
An array could be drawn like …
g d a f c z l
0 1 2 3 4 5 6
Each box has the same datatype
![Page 84: Introduction to C++](https://reader033.vdocuments.site/reader033/viewer/2022061202/546b474baf795919088b6de7/html5/thumbnails/84.jpg)
In 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
![Page 85: Introduction to C++](https://reader033.vdocuments.site/reader033/viewer/2022061202/546b474baf795919088b6de7/html5/thumbnails/85.jpg)
An array is declared by …
int X[10]; Where int is the common datatype for
all elements in the array, X is the name of the array (identifier),
and … 10 is the size of the array, or how many
elements are in the array Indices for a C++ array always begin
with 0
![Page 86: Introduction to C++](https://reader033.vdocuments.site/reader033/viewer/2022061202/546b474baf795919088b6de7/html5/thumbnails/86.jpg)
Example: Student Grades
// Declare array
double stGrades[7];
:
// Assign value to array element
stGrades[5] = 87;
:
// Display array element
cout << stGrades[3] << endl;
![Page 87: Introduction to C++](https://reader033.vdocuments.site/reader033/viewer/2022061202/546b474baf795919088b6de7/html5/thumbnails/87.jpg)
Problem: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!
![Page 88: Introduction to C++](https://reader033.vdocuments.site/reader033/viewer/2022061202/546b474baf795919088b6de7/html5/thumbnails/88.jpg)
StringsComputer Science I
![Page 89: Introduction to C++](https://reader033.vdocuments.site/reader033/viewer/2022061202/546b474baf795919088b6de7/html5/thumbnails/89.jpg)
What 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
![Page 90: Introduction to C++](https://reader033.vdocuments.site/reader033/viewer/2022061202/546b474baf795919088b6de7/html5/thumbnails/90.jpg)
StringsA literal is a string bounded by
quotation marksExample:
“nc9*hNB 98B&^v*&G”Notice the blanks spaces in the
sequence - they are characters!
![Page 91: Introduction to C++](https://reader033.vdocuments.site/reader033/viewer/2022061202/546b474baf795919088b6de7/html5/thumbnails/91.jpg)
StringsIn C++, we declare string variables
as followschar string_identifier[length];
string_identifier is the name of the string variable
length represents the length of the string, or how many characters are in the sequence
![Page 92: Introduction to C++](https://reader033.vdocuments.site/reader033/viewer/2022061202/546b474baf795919088b6de7/html5/thumbnails/92.jpg)
StringsExample:
void main()
{
char name[24];
cout << “Enter your name: “;
cin >> name;
cout << “Your name is “ << name << endl;
}
![Page 93: Introduction to C++](https://reader033.vdocuments.site/reader033/viewer/2022061202/546b474baf795919088b6de7/html5/thumbnails/93.jpg)
Streams
A stream …Is a flow of charactersIs an object that represents either on
input or an output to a programCan be associated with the keyboard,
monitor, or files cout is an output stream cin is an input stream
![Page 94: Introduction to C++](https://reader033.vdocuments.site/reader033/viewer/2022061202/546b474baf795919088b6de7/html5/thumbnails/94.jpg)
How about file I/O?
#include <fstream.h>
:
ifstream in_stream;
ofstream out_stream;
:
Where definitions for datatypes ifstream and ofstream are located
Input stream declaration
Output stream declaration
![Page 95: Introduction to C++](https://reader033.vdocuments.site/reader033/viewer/2022061202/546b474baf795919088b6de7/html5/thumbnails/95.jpg)
How about file I/O?
#include <fstream.h>:ifstream in_stream;ofstream out_stream;:in_stream.open(“infile.txt”);out_stream.open(“outfile.txt”);instream >> number;outstream << number << endl;instream.close();outstream.close();:
Associates files with streams & opens files for use
Stream use
Closes files
![Page 96: Introduction to C++](https://reader033.vdocuments.site/reader033/viewer/2022061202/546b474baf795919088b6de7/html5/thumbnails/96.jpg)
What if a file doesn’t exist?
In_stream.open(“notthere.txt”);
If (in_stream.fail()){
cout << “Input file opening failed” << endl;
exit(1);}
![Page 97: Introduction to C++](https://reader033.vdocuments.site/reader033/viewer/2022061202/546b474baf795919088b6de7/html5/thumbnails/97.jpg)
How about formatting?
out_stream.setf(ios::fixed);
out_stream.setf(ios::showpoint);
out_stream.precision(2);
out_stream.width(10);
:
Set flag:
fixed/scientificshowpointshowposright/left
Number of decimal places
Number of digits
![Page 98: Introduction to C++](https://reader033.vdocuments.site/reader033/viewer/2022061202/546b474baf795919088b6de7/html5/thumbnails/98.jpg)
More member functionsget()put()eof()
![Page 99: Introduction to C++](https://reader033.vdocuments.site/reader033/viewer/2022061202/546b474baf795919088b6de7/html5/thumbnails/99.jpg)