cs201 – introduction to computing – sabancı university 1 built-in arrays l c++ native array...

11
CS201 – Introduction to Computing – Sabancı University 1 Built-in Arrays C++ native array type (not the class version) Two versions fixed size arrays array size is fixed and must be specified as a constant expression at the declaration (determined during compile time) we will see this type now array pointers array size is dynamically allocated will not see in this course (will be covered in CS204) use of both types are the same except definition vector versus built-in arrays vector is a class that is based on built-in arrays vector has member functions and operators, built-in arrays do NOT vector is more flexible, but slower

Upload: robyn-wells

Post on 17-Jan-2018

219 views

Category:

Documents


0 download

DESCRIPTION

CS201 – Introduction to Computing – Sabancı University 3 Built-in array initialization at declaration l You may specify a list of initial values at declaration. See following example string dayNames [] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};  dayNames is an array with 7 elements of type string 0 th element is “Sunday”, 1 st is “Monday”,... ä not necessary to specify size (which is 7), since the number of elements make the size clear but you can specify the size, if you wish string dayNames [7] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

TRANSCRIPT

Page 1: CS201 – Introduction to Computing – Sabancı University 1 Built-in Arrays l C++ native array type (not the class version) l Two versions ä fixed size arrays

CS201 – Introduction to Computing – Sabancı University 1

Built-in Arrays C++ native array type (not the class version) Two versions

fixed size arrays• array size is fixed and must be specified as a constant

expression at the declaration (determined during compile time)• we will see this type now

array pointers• array size is dynamically allocated• will not see in this course (will be covered in CS204)

use of both types are the same except definition vector versus built-in arrays

vector is a class that is based on built-in arrays vector has member functions and operators, built-in arrays

do NOT• vector is more flexible, but slower

Page 2: CS201 – Introduction to Computing – Sabancı University 1 Built-in Arrays l C++ native array type (not the class version) l Two versions ä fixed size arrays

CS201 – Introduction to Computing – Sabancı University 2

Built-in Array declaration As we said, we will discuss fixed size built-in arrays, not the pointer

version with dynamic allocation size must be able to be determined at compile time

• constant, literal or an expression that involves constants and literals only

#define ALPHABETSIZE 26 //compile directive preprocessedconst int CLASSSIZE = 100; //constant declaration

string names[CLASSIZE]; // array of 100 stringsdouble grades[CLASSIZE*5]; // array of 500 doublesint list[200]; // array of 200 integerschar abc[ALPHABETSIZE]; // array of 26 characters

The following array declaration is INVALIDint size;cout "Enter how many students ? ";cin >> size;string names[size]; // array size cannot be a variable

Page 3: CS201 – Introduction to Computing – Sabancı University 1 Built-in Arrays l C++ native array type (not the class version) l Two versions ä fixed size arrays

CS201 – Introduction to Computing – Sabancı University 3

Built-in array initialization at declaration You may specify a list of initial values at declaration. See following

example

string dayNames [] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

dayNames is an array with 7 elements of type string• 0th element is “Sunday”, 1st is “Monday”, ...

not necessary to specify size (which is 7), since the number of elements make the size clear

• but you can specify the size, if you wish

string dayNames [7] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

Page 4: CS201 – Introduction to Computing – Sabancı University 1 Built-in Arrays l C++ native array type (not the class version) l Two versions ä fixed size arrays

CS201 – Introduction to Computing – Sabancı University 4

Assignment rules in arrays vectors with the same element type can be assigned to each other by =

LHS vector becomes the same as the RHS vector• size and capacity also become the same

Built-in arrays cannot be assigned to each other by =

int coins[] ={1,5,10,25};int temp[4];

temp = coins; // illegaltemp[1] = coins[2]; // legal – array element assignment

How can we assign coins to temp? element by element

for (i=0; i<4; i++) temp[i] = coins[i];

Page 5: CS201 – Introduction to Computing – Sabancı University 1 Built-in Arrays l C++ native array type (not the class version) l Two versions ä fixed size arrays

CS201 – Introduction to Computing – Sabancı University 5

Passing built-in arrays as parameters A built-in array can be passed only as reference parameter or

const-reference parameter cannot be passed as value parameter

But, we do not use ampersand character, &, at parameter declaration and we do not specify the array size in array parameter however array size is generally passed as another integer

parameter since we do not have a size() member function for built-in arrays

void Change(int list[], int numElts);

void Print(const int list[], int numElts);

reference parameter

const-reference parameter

Page 6: CS201 – Introduction to Computing – Sabancı University 1 Built-in Arrays l C++ native array type (not the class version) l Two versions ä fixed size arrays

CS201 – Introduction to Computing – Sabancı University 6

Built-in array demo See fixlist.cpp (slightly modified from the version in book) Why did we use const in Print?

to avoid accidental changes in array list Why did we pass numElts as parameter for the number of

elements in the array? because we don’t know the total number of elements in

the array while writing the functions

Page 7: CS201 – Introduction to Computing – Sabancı University 1 Built-in Arrays l C++ native array type (not the class version) l Two versions ä fixed size arrays

CS201 – Introduction to Computing – Sabancı University 7

Example – Fibonacci numbers Used in many areas of Mathematics and Computer Science

F0 = 1F1 = 1Fn = Fn-1 + Fn-2

1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 You can see many examples of Fibonacci numbers in nature

E.g. Increase of number of branches of trees in time See http://www.maths.surrey.ac.uk/hosted-sites/R.Knott/Fibonacci/fibnat.html for more examples

const int MAX_SIZE = 100; int list[MAX_SIZE];

int k; list[0] = list[1] = 1; for (k=2; k < MAX_SIZE, k++) {

list[k] = list[k-1]+list[k-2]; }

Page 8: CS201 – Introduction to Computing – Sabancı University 1 Built-in Arrays l C++ native array type (not the class version) l Two versions ä fixed size arrays

CS201 – Introduction to Computing – Sabancı University 8

Use of strings as arrays Characters in a string can be referred as an array using [ ]

string s="cs201";...s[0] = 'n'; // makes 0th character of s 'n'... for (k=0; k<s.length(); k++)

cout << s[k] << " "; In general,

s[k] means s.at(k)

Page 9: CS201 – Introduction to Computing – Sabancı University 1 Built-in Arrays l C++ native array type (not the class version) l Two versions ä fixed size arrays

CS201 – Introduction to Computing – Sabancı University 9

The Matrix• To represent two dimensional arrays

– We define a matrix as a vector of vectors

vector<vector<int>> mat(rows,vector<int>(cols));vector<vector<int>> mat(3, vector<int>(5));

mat[2][3] = 100;

• First index is for row, second is for column

100

0 1 2 3 4

0

1

2

Number of rows

Number of columns

Page 10: CS201 – Introduction to Computing – Sabancı University 1 Built-in Arrays l C++ native array type (not the class version) l Two versions ä fixed size arrays

CS201 – Introduction to Computing – Sabancı University 10

Possible Matrix definitions Possible matrix declarations

4 different declarations

vector<vector<type>> matrix_variable_name;empty matrix (zero rows, zero columns)

vector<vector< type >> matrix_variable_name(rows); matrix with rows rows; each row is an empty vector<type>

vector<vector< type >> matrix_variable_name(rows, vector< type>(cols)); matrix with rows*cols elements (initialized via default constructor; if type is int, initialized to zero)

vector<vector< type >> matrix_variable_name(rows, vector< type>(cols, init_value));

matrix with rows*cols elements all initialized to init_value

Use push_back to fill up

Page 11: CS201 – Introduction to Computing – Sabancı University 1 Built-in Arrays l C++ native array type (not the class version) l Two versions ä fixed size arrays

CS201 – Introduction to Computing – Sabancı University 11

To get the size of rows and columnsmatrix_variable_name.size()e.g. mymatrix.size()

number of rows in matrix

matrix_variable_name [0].size()mymatrix[0].size()

number of columns in matrix Instead of 0, any valid row index can be used, if each

row has equal number of elements. Otherwise, the structure is not a matrix and it is out of scope of CS201

Example: Let’s briefly check out matdemo.cpp; more detailed explanation will be in labs.