Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

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 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 l 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

2 CS201 – Introduction to Computing – Sabancı University 2 Built-in Array declaration l 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 preprocessed const int CLASSSIZE = 100; //constant declaration string names[CLASSIZE]; // array of 100 strings double grades[CLASSIZE*5]; // array of 500 doubles int list[200]; // array of 200 integers char abc[ALPHABETSIZE]; // array of 26 characters l The following array declaration is INVALID int size; cout "Enter how many students ? "; cin >> size; string names[size]; // array size cannot be a variable

3 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"};

4 CS201 – Introduction to Computing – Sabancı University 4 Assignment rules in arrays l 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 l Built-in arrays cannot be assigned to each other by = int coins[] ={1,5,10,25}; int temp[4]; temp = coins; // illegal temp[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];

5 CS201 – Introduction to Computing – Sabancı University 5 Passing built-in arrays as parameters l 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

6 CS201 – Introduction to Computing – Sabancı University 6 Built-in array demo l 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

7 CS201 – Introduction to Computing – Sabancı University 7 Example – Fibonacci numbers l Used in many areas of Mathematics and Computer Science F 0 = 1 F 1 = 1 F n = F n-1 + F n-2 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 l 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 exampleshttp://www.maths.surrey.ac.uk/hosted-sites/R.Knott/Fibonacci/fibnat.html 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]; }

8 CS201 – Introduction to Computing – Sabancı University 8 Use of strings as arrays l 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] << " "; l In general, s[k] means s.at(k)

9 CS201 – Introduction to Computing – Sabancı University 9 The Matrix To represent two dimensional arrays – We define a matrix as a vector of vectors vector > mat(rows,vector (cols)); vector > mat(3, vector (5)); mat[2][3] = 100; First index is for row, second is for column 100 0 1 2 3 4 012012 Number of rows Number of columns

10 CS201 – Introduction to Computing – Sabancı University 10 Possible Matrix definitions l Possible matrix declarations ä 4 different declarations vector > matrix_variable_name ; empty matrix (zero rows, zero columns) vector > matrix_variable_name ( rows ); matrix with rows rows; each row is an empty vector vector > matrix_variable_name ( rows, vector ( cols )); matrix with rows*cols elements (initialized via default constructor; if type is int, initialized to zero) vector > matrix_variable_name ( rows, vector ( cols, init_value )); matrix with rows*cols elements all initialized to init_value Use push_back to fill up

11 CS201 – Introduction to Computing – Sabancı University 11 To get the size of rows and columns matrix_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 l Example: Let’s briefly check out matdemo.cpp; more detailed explanation will be in labs.


Download ppt "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."

Similar presentations


Ads by Google