Data Structures Test 2 Review Dynamic Arrays Flashcards

1
Q

In the code below, which is the size member variable? class Pitcher { private: int m_capacity; int m_size; : public: Pitcher(int capacity = 4); : };

A

int m_size;

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

In the code below, what is the default initial capacity? class Pitcher { private: int m_capacity; int m_size; : public: Pitcher(int capacity = 4); : };

A

the default initial capacity is 4

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

initialize the private member variables Pitcher::Pitcher(int capacity) { [WHAT GOES HERE?] }

A

m_capacity = capacity;m_size = 0; //init size to 0

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

// Pitcher.h file class Pitcher { private: : : public: Pitcher(int capacity = 4); [WHAT GOES HERE?] : };

A

void addTea();

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

// Pitcher.cpp file void Pitcher::addTea() { [WHAT GOES HERE?] }

A

++m_size;

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

// Pitcher.h file class Pitcher { private: int m_capacity; int m_size; : public: Pitcher(int capacity = 4); void addTea(); [WHAT GOES HERE?] : };

A

int getCapacity();int getSize();

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

// Pitcher.cpp file int Pitcher::getCapacity() { [WHAT GOES HERE?] }

A

return m_capacity;

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

// Pitcher.cpp file int Pitcher::getSize() { [WHAT GOES HERE?] }

A

return m_size;

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

// Pitcher.h file class Pitcher { : public: Pitcher(int capacity = 4); [WHAT GOES HERE?] : };

A

Pitcher(Pitcher& copyMe);

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

// Pitcher.cpp file Pitcher::Pitcher(Pitcher& copyMe) { [WHAT GOES HERE?] }

A

m_capacity = copyMe.m_capacity;m_size = copyMe.m_size;

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

// Pitcher.h file class Pitcher { : public: [WHAT GOES HERE?] : };

A

void operator = (const Pitcher& rhs);

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

// Pitcher.cpp file void Pitcher::operator= (const Pitcher& rhs) { [WHAT GOES HERE?] }

A

m_capacity = rhs.m_capacity;m_size = rhs.m_size;

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

// Pitcher.h file class Pitcher { : public: [WHAT GOES HERE?] };

A

friend bool operator==(const Pitcher& lhs, const Pitcher& rhs);

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

// Pitcher.cpp file bool operator== (const Pitcher& lhs, const Pitcher& rhs) { [WHAT GOES HERE?] }

A

bool retVal = false; if (lhs.m_size == rhs.m_size) retVal = true; return retVal;

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

// Pitcher.h file [WHAT GOES HERE?] class Pitcher { private: int m_capacity; int m_size; : public: Pitcher(int capacity = 4); : };

A

enum Flavor { noFlavorSet = -1, raspberry, peach, lemon, grape, strawberry };

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

// Pitcher.h file class Pitcher { private: int m_capacity; int m_size; [WHAT GOES HERE?] : public: Pitcher(int capacity = 4); : };

A

Flavor* mp_serving;

17
Q

In the code below, which is the capacity member variable? class Pitcher { private: int m_capacity; int m_size; : public: Pitcher(int capacity = 4); : };

A

int m_capacity;

18
Q

// Pitcher.cpp file void Pitcher::growPitcher(int n, bool copy) { [WHAT GOES HERE?] // if copy is true, loop to copy elements in mp_array to newArr array if (true == copy) { for(int i=0, i < m_size; i++) { newArr[i] = mp_serving[i]; } } delete [] mp_serving; // free memory used // Assign newArr to mp_serving and update capacity to n mp_serving = newArr; m_capacity = n; }

A

Flavor& newArr = new Flavor[n]; // alloc memory

19
Q

// Pitcher.cpp file void Pitcher::growPitcher(int n, bool copy) { Flavor& newArr = new Flavor[n]; // alloc memory [WHAT GOES HERE?] delete [] mp_serving; // free memory used // Assign newArr to mp_serving and update capacity to n mp_serving = newArr; m_capacity = n; }

A

// if copy is true, loop to copy elements in mp_array to newArr array if (true == copy) { for(int i=0, i < m_size; i++) { newArr[i] = mp_serving[i]; } }

20
Q

// Pitcher.cpp file void Pitcher::growPitcher(int n, bool copy) { Flavor& newArr = new Flavor[n]; // alloc memory // if copy is true, loop to copy elements in mp_array to newArr array if (true == copy) { for(int i=0, i < m_size; i++) { newArr[i] = mp_serving[i]; } } [WHAT GOES HERE?] // Assign newArr to mp_serving and update capacity to n mp_serving = newArr; m_capacity = n; }

A

delete [] mp_serving; // free memory used

21
Q

// Pitcher.cpp file void Pitcher::growPitcher(int n, bool copy) { Flavor& newArr = new Flavor[n]; // alloc memory // if copy is true, loop to copy elements in mp_array to newArr array if (true == copy) { for(int i=0, i < m_size; i++) { newArr[i] = mp_serving[i]; } } delete [] mp_serving; // free memory used [WHAT GOES HERE?] }

A

// Assign newArr to mp_serving and update capacity to nmp_serving = newArr;m_capacity = n;

22
Q

// Old Pitcher.cpp file Pitcher::Pitcher(int capacity) { [WHAT GOES HERE?] }

A

m_capacity = capacity;m_size = 0; // init size to 0

23
Q

// New Pitcher.cpp file Pitcher::Pitcher(int capacity) { [WHAT GOES HERE?] }

A

mp_serving = NULL; // always init ptrs growPitcher(capacity, false); m_size = 0; // init size to 0 // init all servings to notSet for (int j=0; j < m_capacity; j++) { mp_serving[j] = noFlavorSet; }

24
Q

// Old Pitcher.h file class Pitcher { private: : : public: [WHAT GOES HERE?] : };

A

void addTea();

25
Q

// New Pitcher.h file class Pitcher { private: : : public: [WHAT GOES HERE?] : };

A

void addTea(Flavor flav);

26
Q

// Old Pitcher.cpp file void Pitcher::addTea() { [WHAT GOES HERE?] }

A

if ( (m_size + 1) > m_capacity ) { m_capacity = m_capacity * 2; // go with doubling idea here } ++m_size;

27
Q

// New Pitcher.cpp file void Pitcher::addTea(Flavor flav) { [WHAT GOES HERE?] }

A

if ( (m_size + 1) > m_capacity ) { growPitcher(m_capacity * 2, true); // go with doubling idea here } mp_serving[m_size] = flav; // set the new serving flavor ++m_size; // add 1 to serving count

28
Q

// Old Pitcher.cpp file Pitcher::Pitcher(Pitcher& copyMe) { [WHAT GOES HERE?] }

A

m_capacity = copyMe.m_capacity; m_size = copyMe.m_size;

29
Q

// New Pitcher.cpp file Pitcher::Pitcher(Pitcher& copyMe) { [WHAT GOES HERE?] }

A

mp_serving = NULL; // always init ptrs growPitcher(copyMe.m_capacity, false); for (int j=0; j < copyMe.m_capacity; j++) { mp_serving[j] = copyMe.mp_serving[j]; } m_size = copyMe.m_size;

30
Q

// Old Pitcher.cpp file Pitcher& Pitcher::operator= (const Pitcher& rhs) { [WHAT GOES HERE?] }

A

m_capacity = rhs.m_capacity;m_size = rhs.m_size;

31
Q

// New Pitcher.cpp file Pitcher& Pitcher::operator= (const Pitcher& rhs) { [WHAT GOES HERE?] m_size = rhs.m_size; }

A

if (m_capacity < rhs.m_capacity){ growPitcher(rhs.m_capacity, false);}for (int j=0; j < rhs.m_capacity; j++){ mp_serving[j] = rhs.mp_serving[j];}

32
Q

// Old Pitcher.cpp file bool operator== (const Pitcher& lhs, const Pitcher& rhs) { [WHAT GOES HERE?] }

A

bool retVal = false; if (lhs.m_size == rhs.m_size) retVal = true; return retVal;

33
Q

// New Pitcher.cpp file bool operator== (const Pitcher& lhs, const Pitcher& rhs) { [WHAT GOES HERE?] } return retVal; }

A

retVal = true;int j = 0;while ( (j < lhs.m_size) && (true == retVal)){ if ( lhs.mp_serving[j] != rhs.mp_serving[j]) { retVal = false; }++j;}

34
Q

“class Pitcher operator &laquo_space; ostream& operator&laquo_space;(ostream& ostr, const Pitcher& pi) { if ( pi.m_size <= 0) { ostr &laquo_space;"”pitcher is empty””; } for(int i=0; i < pi.m_size; i++) { switch (pi.mp_serving[i]) { case raspberry: ostr &laquo_space;"”raspberry””; break; case peach: ostr &laquo_space;"”peach””; break; : // lines skipped here [WHAT GOES HERE?] } // end switch ostr &laquo_space;”” “”; } // end for return ostr; }”

A

“default: ostr &laquo_space;"”oops spilled one””; break;”