Beruflich Dokumente
Kultur Dokumente
Iteration
how to iterate over a series of values
Function
how a particular sub-computation can be named and
specified separately as a function.
Vector
how to use the vector type to hold sequences of values. To
be able to perform more realistic computations.
Expressions
// a multiplication
Operators
Operators
a * b + c /d
Operators
10
arithmetic operators: +, -, *, /
11
Concise Operators
a += c
means
a = a+c
a *= scale
means
a = a*scale
++a
means
or
a += 1
a = a+1
Conversions
2.5/2 = ?
is a double divided by an int
is a Floating-point division or integer division?
5/2 = ?
is an int divided by an int
12
14
Conversions
Quiz
double d = 2.5;
int i = 2;
free upgrade!
double d2 = d/i;
int i2 = d/i;
- otherwise
- we use integer arithmetic yielding an int result
(Integer division throws away the remainder)
then, d2 = ?
i2= ?
15
Selection
Selection
The syntax is
if (condition)
statement1
else
statement2
16
if (a<b)
// Note: No semicolon here
cout<<"max is "<< b<<\n;
else
// Note: No semicolon here
cout<<"max is "<< a<<\n;
E.g, Cross the street at a traffic light, you had to wait for the light to tum
green
if (traffic_light ==green )
go();
else
wait();
18
19
Selection
Selection
Switch-statement
comparison of a value against constants
a statement is chosen
//1st alternative
//2nd alternative
//3rd alternative
if(traffic_light == green)
walk();
else if(traffic_light == yellow)
run();
else
wait();
char c;
cin>>c;
switch(c)
{
case y:
//.
break;
case n:
//
break;
default:
//
break;
}
20
Selection
21
Selection
string s;
cin>>s;
switch(s)
{
case no:
//.
break;
case yes:
//
break;
default:
//
break;
}
Error!
The value on which we switch must be of
an integer, char, or enumeration type.
22
Selection
23
Iteration
We rarely do something only once. Therefore,
24
25
for loop
You can collect all the control information in one
place, at the top, where its easy to see.
here: i
here: int i = 0
here: if i<100 is false, terminate
here: ++i
here: cout <<
int i=0;
While(i<100)
{
cout<<++i;
}
or
int i = 0;
for ( ; i<100 ; ++i)
or
int i = 0;
for ( ; i++<100 ; )
26
27
Functions
/*
The worlds first real program running on a stored-program computer
(David Wheeler, Cambridge, May 6, 1949)
(No, it wasnt actually written in C++ J.)
*/
28
29
Control Flow
Functions
Our function
int main()
int square(int x)
{
return x*x;
}
int square(int x)
i=0;
return x * x;
while (i<100)
is an example of
31
Another Example
Control Flow
function that compares the two values and returns the larger value.
int main()
{
i=0;
int square(int x)
{
if (a<b)
return b;
return x * x;
while (i<100)
else
return a;
}
int x = max(7, 9);
int y = max(19, -27);
int z = max(20, 20);
i<100
i==100
++i;
// x becomes 9
// y becomes 19
// z becomes 20
32
33
Exercise
words
E.g.,
Input:
Stroustrup/Programming
Vector
If we have a collection of data to work on.
We can store this data in a vector
The most useful, simplest ways of storing a collection
data
E.g. a list of student ID, a list of student grades, etc.
36
Vector
Vector
size
to make a vector we need to specify:
the type of element
A default value
Or.specify another
vector<int> v(10,-3);
37
Growing a vector
Growing a vector
start a vector empty and grow it to its desired size
Using a member function of vector push_back(element);
vector<int> v;
// start off empty
v.push_back(1);
// add an element with the value 1
// v[0] = 1
v.push_back(4);
// add an element with the value 4
// v[1] = 4
v.push_back(3);
// add an element with the value 3
// v[2] = 3
Size
(Empty vector)
v:
v:
v:
v:
39
Example(3)
Example(3)
int main()
// compute mean (average) temperatures:
{
vector<double> temps;
// temperatures in Fahrenheit, e.g. 64.6
double temp;
while (cin>>temp)
temps.push_back(temp); // read and put into vector
int main()
// compute mean (average) temperatures:
{
vector<double> temps;
// temperatures in Fahrenheit, e.g. 64.6
double temp;
while (cin>>temp)
temps.push_back(temp); // read and put into vector
double mean = 0;
//implement your code here.
double sum = 0;
for (int i = 0; i < temps.size(); ++i)
sum += temps[i]; // sums temperatures
}
41
Example(3)
42
Vector
int main()
// compute median temperatures:
{
vector<double> temps;
// temperatures in Fahrenheit, e.g. 64.6
double temp;
while (cin>>temp)
temps.push_back(temp); // read and put into vector
sort(temps.begin(), temps.end());
//sorting the contained numbers from small to large. A STL algorithm
cout << "Median temperature: " << temps[temps.size()/2] << endl;
http://www.cplusplus.com/reference/stl/vector/
43
44
46