lecture 9&10 Flashcards

1
Q

what’s an array ?

A

Array is a variable that can store multiple values of the same type

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

how are the values in the array stored in the memory ?

A

they are stored in adjacent memory locations

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

label the following array definition :

int tests[5];

A
  • 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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

what is the difference between the size of an array and the size declarator of an array ?

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

cout &laquo_space;tests ; // why would this give an error ?

what is the only case in which this cout - statement wont give an error ?

A

Arrays must be accessed via individual elements ex:-
cout &laquo_space;tests[0];
cin&raquo_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 &laquo_space;tests ;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Default Initialization for arrays :

  • Global array → all elements ………. by default
  • Local array → all elements ………….by default
A
  • initialized to 0
  • uninitialized
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

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…………..

A
  • initialization list
  • will be set to 0
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

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

A

array size declarator or initialization list

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

what is the difference between
tests[ i ] ++ ; and tests [ i ++] ; ??

A

tests[i]++; // add 1 to tests[i]
tests[i++]; // increment i, no effect on tests[ i ]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

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

A

for (i = 0 ; i < ARRAY_SIZE ; i++)
newTests [ i ] = tests[ i ];

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Most compilers do not allow the use of a variable to declare the size of an array
ex:-
cout &laquo_space;“Enter number of students: “;
cin&raquo_space; number;
int score[number]; // IS ILLELGAL

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

what are parallel arrays ?

A
  • Parallel arrays are two or more arrays that contain related data
  • elements at same subscript are related
  • Arrays may be of different types
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

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 ………..

A

pointer to an array

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

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

A

Default constructor
the initializer list

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Objects in an array are referenced using ………….
Member functions are referenced using ……….

A
  • subscripts
  • dot notation
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

*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:
…………………………….

A
  • vector<int> scores ;</int>
  • vector<int> scores(30) ;</int>
  • vector<int> scores(30, 0) ;</int>
  • vector<int> finals(scores) ;</int>
17
Q
  • 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:
A
  • push_back
    ex:-
    scores.push_back(75) ;
  • size
    ex:-
    howbig = scores . size() ;
18
Q

*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
A
  • pop_back ex:- scores . pop_back() ;
  • clear ex:- scores . clear() ;
  • empty ex:- while ( ! scores . empty () )
19
Q

…………..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

A
  • at(elt) ex:- cout &laquo_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);