110 likes | 158 Views
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
E N D
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
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 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 • The following array declaration is INVALID int size; cout "Enter how many students ? "; cin >> size; string names[size]; // array size cannot be a variable
Built-in array initialization at declaration • You may specify a list of initialvalues 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"};
Assignment rules in arrays • vectors withthesame element typecan 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; // 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];
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
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 numEltsas 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
Example – Fibonacci numbers • Used in many areas of Mathematics and Computer Science F0 = 1 F1 = 1 Fn = 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]; }
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)
Torepresenttwodimensionalarrays We define a matrixas 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 forrow, second is forcolumn TheMatrix Number of rows Number of columns 0 1 2 3 4 0 1 2
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 rowsrows; each row is anempty vector<type> vector<vector< type >> matrix_variable_name(rows, vector< type>(cols)); matrix with rows*colselements (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*colselements all initialized to init_value Possible Matrix definitions Usepush_backtofillup
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 Example: Let’s briefly check out matdemo.cpp; more detailed explanation will be in labs. To get the size of rows and columns