You are on page 1of 13

EEE 241 Computer Programming

http://eee241.gantep.edu.tr/

Lecture 8

Vectors

Sayfa 1

Content of this Week


This week we will study the basics of Vectors:
Concept
Declaration
Initialisation
Assignment
Processing
Functions
Dynamic Memory Management

Sayfa 2

Concept of Vectors
Up to now, a variable has represented a single value,
we call this a scalar variable; it has a type and a name.
A vector variable can represent a group of values, all with the
same name and data type.
To declare and use vectors we must include the vector header:
#include <vector>
The vector data class provides many powerful methods for
vector processing and dynamic memory management.
We will study the basics of vectors in this lecture.

Sayfa 3

Vector Declaration
The general form of the declaration of a vector array is:
vector<type> name(numberOfElements);
For example, consider a variable named mass :
Scalar declaration:
double mass;
This variable can
represent only one
value at a time.

Vector declaration:
vector<double>

mass(4);

This variable can represent four values,


they are referenced as:
mass[0], mass[1], mass[2], mass[3]
Note that the index begins with 0.

Sayfa 4

Empty vectors
vector<type> name(numberOfElements);
optional
If you do not specify numberOfElements then
the vector will be created with no (zero) elements
vector<int> scores;
This is an empty vector!

Thats ok, because you can add elements later.

Sayfa 5

Declaration of a vector containing 118 names.


vector<string> name(118);
The elements are:
name[0]
name[7]
name[14]
name[21]
name[28]
name[35]
name[42]
name[49]
name[56]
name[63]
name[70]
name[77]
name[84]
name[91]
name[98]
name[105]
name[112]

name[1]
name[8]
name[15]
name[22]
name[29]
name[36]
name[43]
name[50]
name[57]
name[64]
name[71]
name[78]
name[85]
name[92]
name[99]
name[106]
name[113]

name[2]
name[9]
name[16]
name[23]
name[30]
name[37]
name[44]
name[51]
name[58]
name[65]
name[72]
name[79]
name[86]
name[93]
name[100]
name[107]
name[114]

name[3]
name[10]
name[17]
name[24]
name[31]
name[38]
name[45]
name[52]
name[59]
name[66]
name[73]
name[80]
name[87]
name[94]
name[101]
name[108]
name[115]

name[4]
name[11]
name[18]
name[25]
name[32]
name[39]
name[46]
name[53]
name[60]
name[67]
name[74]
name[81]
name[88]
name[95]
name[102]
name[109]
name[116]

name[5]
name[12]
name[19]
name[26]
name[33]
name[40]
name[47]
name[54]
name[61]
name[68]
name[75]
name[82]
name[89]
name[96]
name[103]
name[110]
name[117]

name[6]
name[13]
name[20]
name[27]
name[34]
name[41]
name[48]
name[55]
name[62]
name[69]
name[76]
name[83]
name[90]
name[97]
name[104]
name[111]

Clearly it is easier to declare one vector (size 118) than it is to declare 118 scalars!
Sayfa 6

Vector Initialisation
The general form of vector declaration:
vector<type> name(numberOfElements);
initialises all elements of the vector to zero.
Alternatively an initialiser can be given at declaration:
vector<type> name(numberOfElements, value);
initialises all elements of the vector to value.
Examples

vector<double>

mass(6);

all elements of mass are initialised to 0.


vector<double>

mass(6,1.8);

all elements of mass are initialised to 1.8


Sayfa 7

Vector Assignment
Consider the vector declaration: vector<double>

a(5);

At this time, the elements are all automatically initialised to zero.

0.0

0.0

0.0

0.0

0.0

Elements of a vector array can be assigned (at any time) as follows:


a[0]
a[1]
a[2]
a[4]

=
=
=
=

8.4;
3.6;
9.1;
3.9;

The green values are the array indices.


Note that vector assignment is performed
in the same way as array assignment.

The values of the elements are now re-defined (except for a[3]):

8.4

3.6

9.1

0.0

3.9

Note that a[3] is still 0.0


Sayfa 8

Assignment can be performed directly from input:


#include <iostream>
#include <vector>
using namespace std;
int main () {
cout << "Input n: ";
int n; cin >> n;
vector<double> a(n);

This program declares a vector of size n


(with n input from the keyboard), assigns
the elements with values input from the
keyboard, and then outputs the values in
reverse order.

cout << "Input " << n << " real numbers:" << endl;
for(int i=0; i<n; i++) cin >> a[i];
cout << "In reverse order: " << endl;
for(int i=n-1; i>=0; i--) cout << a[i] << " ";
cout << endl;
return 0;
}

Output
Input n: 5
Input 5 real numbers:
1.2 3.5 -0.4 10.2 7.1
In reverse order:
7.1 10.2 -0.4 3.5 1.2
Sayfa 9

Processing Vectors
Vector processing often involves looping over elements.
#include <iostream>
#include <vector>
using namespace std;

Sum of the
values of the
elements of
a vector:

int main () {

int n = 5;
vector<double> a(n);
a[0]=1.7; a[1]=4.1; a[2]=5.6; a[3]=3.4; a[4]=3.1;
double s=0.0;
for (int i=0; i<n; i++)
s = s + a[i];
cout << "The sum is " << s << endl;
}

Note the vector size n in the loop.

Output

The sum is 17.9


Sayfa 10

The .size() method


The .size() method provides good alternative:
#include <iostream>
#include <vector>
using namespace std;

Sum of the
values of the
elements of
a vector:

int main () {

int n = 5;
vector<double> a(n);
a[0]=1.7; a[1]=4.1; a[2]=5.6; a[3]=3.4; a[4]=3.1;
double s=0.0;
for (int i=0; i<a.size(); i++)
s = s + a[i];
cout << "The sum is " << s << endl;
}

Note the vector size a.size().

Output

The sum is 17.9


Sayfa 11

Passing Vectors to the Functions


The previous example is rewritten using a function to perform the sum.
#include <iostream>
#include <vector>
using namespace std;

Sum of the
values of the
elements of
a vector:

double sum(vector<double> x) {
double s=0.0;
for (int i=0; i<x.size(); i++)
s = s + x[i];
return s;
}

Passing a vector to a
function works just
the same as passing
a scalar.

You can also pass by


int main () {
reference (good idea
int n = 5;
for vey large vectors).
vector<double> a(n);
a[0]=1.7; a[1]=4.1; a[2]=5.6; a[3]=3.4; a[4]=3.1;
cout << "The sum is " << sum(a) << endl;
}

You need to use the .size() method.

Output

The sum is 17.9


Sayfa 12

Passing by reference
A function to square each element of a vector.
#include <iostream>
#include <vector>
using namespace std;

Vector x is passed by
reference so that the
modification to each
element is preserved.

void vsquare(vector <double> &x) {


for (int i=0; i<x.size(); i++)
x[i] = x[i]*x[i];
return;
}

int main () {
vector<double> a(3);
a[0]=1.7; a[1]=4.1; a[2]=5.6;
cout << a[0] << "\t" << a[1] << "\t" << a[2] << endl;
vsquare(a);
cout << a[0] << "\t" << a[1] << "\t" << a[2] << endl;
}
Output

1.7
2.89

4.1
16.81

5.6
31.36
Sayfa 13

Dynamic Memory Management


The size of a vector (the number of elements) can be:
1) defined at any time, and

2) change at any time.

Vectors are therefore a type of dynamic memory storage.


As well as the .size() method, we have four methods for
dynamic memory management of vectors:
name.push_back(x);

adds an element with value x


to the end of the vector
(increasing the vector size by one).

name.pop_back();

removes an element from the end of


the vector (decreasing the size by one).

name.clear();

removes all elements from the vector


(leaving a vector of size zero)

name.resize(s);

resizes the vector to size

s
Sayfa 14

Using the .push_back() and .pop_back() methods


A vector can be considered as a stack of values.
remove a value
from the stack
The top of the
stack is the end
of the vector

add a value
to the stack
6

Sayfa 15

Using the .push_back() method


#include <iostream>
#include <vector>
using namespace std;
int main () {

The
The
The
The

size is
content
size is
content

3
is: 8.3 8.3 8.3
4
is: 8.3 8.3 8.3 5.9

vector<double> x(3, 8.3);

cout << "The size is " << x.size() << endl;


cout << "The content is: ";
for (unsigned int i=0; i<x.size(); i++) cout << x[i] << " ";
cout << endl;
x.push_back(5.9);
cout << "The size is " << x.size() << endl;
cout << "The content is: ";
for (unsigned int i=0; i<x.size(); i++) cout << x[i] << " ";
cout << endl;
}
Sayfa 16

Using the .pop_back() method


#include <iostream>
#include <vector>
using namespace std;
int main () {

The
The
The
The

size is
content
size is
content

3
is: 8.3 8.3 8.3
2
is: 8.3 8.3

vector<double> x(3, 8.3);


cout << "The size is " << x.size() << endl;
cout << "The content is: ";
for (unsigned int i=0; i<x.size(); i++) cout << x[i] << " ";
cout << endl;
x.pop_back();
cout << "The size is " << x.size() << endl;
cout << "The content is: ";
for (unsigned int i=0; i<x.size(); i++) cout << x[i] << " ";
cout << endl;
}
Sayfa 17

Using the .clear() method


#include <iostream>
#include <vector>
using namespace std;
int main () {

The
The
The
The

size is
content
size is
content

3
is: 8.3 8.3 8.3
0
empty vector
is:

vector<double> x(3, 8.3);

cout << "The size is " << x.size() << endl;


cout << "The content is: ";
for (unsigned int i=0; i<x.size(); i++) cout << x[i] << " ";
cout << endl;
x.clear();
cout << "The size is " << x.size() << endl;
cout << "The content is: ";
for (unsigned int i=0; i<x.size(); i++) cout << x[i] << " ";
cout << endl;
}
Sayfa 18

Using the .resize() method


#include <iostream>
#include <vector>
using namespace std;
int main () {

The
The
The
The

size is
content
size is
content

3
is: 8.3 8.3 8.3
5
is: 8.3 8.3 8.3 0.0 0.0

vector<double> x(3, 8.3);


cout << "The size is " << x.size() << endl;
cout << "The content is: ";
for (unsigned int i=0; i<x.size(); i++) cout << x[i] << " ";
cout << endl;
x.resize(5);
cout << "The size is " << x.size() << endl;
cout << "The content is: ";
for (unsigned int i=0; i<x.size(); i++) cout << x[i] << " ";
cout << endl;
}
Sayfa 19

This program builds a vector from values input from the keyboard.
The size of the vector (initially zero) increases until a zero is input.
#include <iostream>
#include <vector>
using namespace std;

Output

int main() {
vector<int> iv;

56

int n;
while(true) {
cout << "Input: ";
cin >> n;
if (n==0) break;
iv.push_back(n);
}

23
89
65
34

Input: 34
Input: 65
Input: 89
Input: 23
Input: 56
Input: 0
iv is:
iv[0] = 34
iv[1] = 65
iv[2] = 89
iv[3] = 23
iv[4] = 56

cout << "iv is:" << endl;


for(unsigned int i=0; i<iv.size(); i++)
cout << "iv[" << i << "] = " << iv[i] << endl;
}
Sayfa 20

10

Solved problems
Create a program that inputs the atomic number Z, and
outputs the corresponding elements Name, Mass and Classification.
Z
1
2
3
4
5
6
7
8
9
10
11
12

Symbol
H
He
Li
Be
B
C
N
O
F
Ne
Na
Mg

Name
Hydrogen
Helium
Lithium
Beryllium
Boron
Carbon
Nitrogen
Oxygen
Fluorine
Neon
Sodium
Magnesium

Mass (amu)
1.00794
4.002602
6.941
9.012182
10.811
12.0107
14.00674
15.9994
18.998404
20.1797
22.9898
24.305

Classification
Non-metal
Noble Gas
Alkali Metal
Alkaline Earth
Metaloid
Non-Metal
Non-Metal
Non-Metal
Halogen
Noble Gas
Alkali Metal
Alkaline Earth

Sayfa 21

The solution (except for the part where we assign data to the vectors) .
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<string> name(12), symbol(12), clas(12);
vector<double> mass(12);
Assign all elements of name, symbol, clas and mass from the table.
int Z; cout << "Input atomic number Z: "; cin >> Z;
cout
cout
cout
cout
cout
cout

<<
<<
<<
<<
<<
<<

"
"
"
"
"
"

+-------------------"
| Z = " << Z
| " << name[Z-1] << " ("
| Mass = " << mass[Z-1]
| Class: " << clas[Z-1]
+-------------------"

<<
<<
<<
<<
<<
<<

endl;
endl;
symbol[Z-1] << ")" << endl;
" amu." << endl;
endl;
endl;

Sayfa 22

11

#include <iostream>
#include <vector>
using namespace std;

Check the output for yourself.


Add checks for Z<1 and Z>12!

int main() {
vector<string> name(12), symbol(12), clas(12);
vector<double> mass(12);
name[ 0]="Hydrogen";
name[ 1]="Helium";
name[ 2]="Lithium";
name[ 3]="Beryllium";
name[ 4]="Boron";
name[ 5]="Carbon";
name[ 6]="Nitrogen";
name[ 7]="Oxygen";
name[ 8]="Fluorine";
name[ 9]="Neon";
name[10]="Sodium";
name[11]="Magnesium";

symbol[ 0]="H";
symbol[ 1]="He";
symbol[ 2]="Li";
symbol[ 3]="Be";
symbol[ 4]="B";
symbol[ 5]="C";
symbol[ 6]="N";
symbol[ 7]="O";
symbol[ 8]="F";
symbol[ 9]="Ne";
symbol[10]="Na";
symbol[11]="Mg";

clas[ 0]="Non-Metal";
clas[ 1]="Noble Gas";
clas[ 2]="Alkali Metal";
clas[ 3]="Alkaline Earth";
clas[ 4]="Metaloid";
clas[ 5]="Non-Metal";
clas[ 6]="Non-Metal";
clas[ 7]="Non-Metal";
clas[ 8]="Halogen";
clas[ 9]="Noble Gas";
clas[10]="Alkali Metal";
clas[11]="Alkaline Earth";

mass[ 0]=1.00794;
mass[ 1]=4.002602;
mass[ 2]=6.941;
mass[ 3]=9.012182;
mass[ 4]=10.811;
mass[ 5]=12.0107;
mass[ 6]=14.00674;
mass[ 7]=15.9994;
mass[ 8]=18.998404;
mass[ 9]=20.1797;
mass[10]=22.9898;
mass[11]=24.305;

int Z; cout << "Input atomic number Z: "; cin >> Z;


cout
cout
cout
cout
cout
cout

<<
<<
<<
<<
<<
<<

"
"
"
"
"
"

+-------------------"
| Z = " << Z
| " << name[Z-1] << " ("
| Mass = " << mass[Z-1]
| Class: " << clas[Z-1]
+-------------------"

<<
<<
<<
<<
<<
<<

endl;
endl;
symbol[Z-1] << ")" << endl;
" amu." << endl;
endl;
endl;

In the next
lecture we will
assign this data
by reading it
from a file
Sayfa 23

Finally
In this lecture we have looked at vectors.
You can find the lecture slides in the course website:
http://eee241.gantep.edu.tr/
For further reading, search for the relevant sections at:
http://cpp.gantep.edu.tr/
and
http://www.learncpp.com/
Consider this as your course text book.

Next week we will look at C++ File Processing.

Sayfa 24

12

TO DO BEFORE THE NEXT LECTURE


1. Browse through the course website
http://eee241.gantep.edu.tr/
2. Do your homework
Try the related exercises in the basic tutorial at
http://cpp.gantep.edu.tr/

3. Prepare for this weeks lab exercise


http://www1.gantep.edu.tr/~eee241/labs.php
4. Print next weeks lecture notes from:
http://www1.gantep.edu.tr/~eee241/lectures.php

Sayfa 25

13

You might also like