lecture 9&10 Flashcards
what’s an array ?
Array is a variable that can store multiple values of the same type
how are the values in the array stored in the memory ?
they are stored in adjacent memory locations
label the following array definition :
int tests[5];
- int is the data type of the array elements
- tests is the name of the array
- 5, in [5], is the size declarator. It shows the NUMBER of elements in the array.
what is the difference between the size of an array and the size declarator of an array ?
- the size declarator shows the NUMBER of elements in the array. ex:- int array [ 6 ] ; is an array with size declaraor of , meaning the array has 6 elements.
- the size of an array is the total number of bytes
= (number of elements) * (size of each element)
ex:- int array [ 6 ] ; is an array of size (6 * 4 bytes ) = 24 bytes
cout «_space;tests ; // why would this give an error ?
what is the only case in which this cout - statement wont give an error ?
Arrays must be accessed via individual elements ex:-
cout «_space;tests[0];
cin»_space; tests[1];
the ONLY case in which we can display the contents of the whole array by sending only its name to cout is if the tests [ ] array is a character array
ex:-
char tests [ ] = “henry”;
cout «_space;tests ;
Default Initialization for arrays :
- Global array → all elements ………. by default
- Local array → all elements ………….by default
- initialized to 0
- uninitialized
Arrays can be initialized with an…………… that cannot exceed the array size.
If array is initialized with fewer initial values than the size declarator, the remaining elements…………..
- initialization list
- will be set to 0
we Can determine array size by the size of the initialization list. ex:-
int quizzes [ ] ={12,17,15,11};
we Must use either ……………… or………. at array definition
array size declarator or initialization list
what is the difference between
tests[ i ] ++ ; and tests [ i ++] ; ??
tests[i]++; // add 1 to tests[i]
tests[i++]; // increment i, no effect on tests[ i ]
to copy an array , assign each element of array 1 to the corrosponding element in array 2 by using loop
ex:-
newTests = tests ; // Won’t work ERROR
for (i = 0 ; i < ARRAY_SIZE ; i++)
newTests [ i ] = tests[ i ];
Most compilers do not allow the use of a variable to declare the size of an array
ex:-
cout «_space;“Enter number of students: “;
cin»_space; number;
int score[number]; // IS ILLELGAL
solution :
Make the array large enough to hold the
largest expected number of elements.
*Use a counter variable to keep track of the
number of items stored in the array
what are parallel arrays ?
- Parallel arrays are two or more arrays that contain related data
- elements at same subscript are related
- Arrays may be of different types
changes made to array in a function are reflected in actual array in calling function like reference variables
Functions can NOT return array type . instead we return a ………..
pointer to an array
array can store objects as its elements
and the ……………… for the object is used when array is defined .
or use ………. to invoke constructor that takes arguments
Default constructor
the initializer list
Objects in an array are referenced using ………….
Member functions are referenced using ……….
- subscripts
- dot notation
*Declare a vector to hold int element:
……………………………….
*Declare a vector with initial size 30:
………………………………..
*Declare a vector and initialize all elements to 0:
……………………………….
*Declare a vector initialized to size and contents of
another vector:
…………………………….
- vector<int> scores ;</int>
- vector<int> scores(30) ;</int>
- vector<int> scores(30, 0) ;</int>
- vector<int> finals(scores) ;</int>
- Use …………. member function to add element to a full array or to an array that had no defined size
- Use …………. member function to determine size of a vector:
- push_back
ex:-
scores.push_back(75) ; - size
ex:-
howbig = scores . size() ;
*Use …………. member function to remove last
element from vector
- To remove all contents of vector, use ……..member function
- To determine if vector is empty, use ………member function
- pop_back ex:- scores . pop_back() ;
- clear ex:- scores . clear() ;
- empty ex:- while ( ! scores . empty () )
…………..Returns the value of the element at
position elt in the vector
…………….Returns the maximum number ofelements a vector can store without allocating more memory
…………..Reverse the order of the elements in a vector
………….Add elements to a vector, optionally initializes them
…………. Exchange the contents of two vectors
- at(elt) ex:- cout «_space;vec1.at(i);
- capacity() ex:- maxelts =vec1.capacity();
- reverse() ex:- vec1.reverse();
- resize(elts,val) ex:- vec1.resize(5,0);
- swap(vec2) ex:- vec1.swap(vec2);