exam 2 class definitions and code questions Flashcards

(58 cards)

1
Q

what are the private members of the Darray class?

A

T *ptr;
int cap;

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

make the default constructor for the Darray class.

A

template <typename>
Darray<T>::Darray(){
ptr = 0;
cap = 0;
}</T></typename>

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

make a constructor for the Darray class that takes an int as a parameter.

A

template <typename>
Darray<T>::Darray(int capacity){
cap = capacity;
ptr = new T[cap];
}</T></typename>

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

make the copy constructor for the Darray class.

A

template <typename>
Darray<T>::Darray(const Darray<T>& actual){
cap = actual.cap;
ptr = new T[cap];
for(int i = 0; i < cap; ++i){
ptr[i] = actual.ptr[i];
}
}</T></T></typename>

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

make a swap function for the Darray class.

A

template <typename>
void Darray<T>::swap(Darray<T> & rhs){
T *temp = ptr;
ptr = rhs.ptr;
rhs.ptr = temp;
int tmp = cap;
cap = rhs.cap;
rhs.cap = tmp;
}</T></T></typename>

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

make a method for the Darray class called resize that takes an int parameter and is a void function.

A

template <typename>
void Darray<T>::resize(int newCap){
int smaller = cap;
if(newCap < smaller){
smaller = newCap;
}
T *tmp = new T[newCap];
for(int i = 0; i < smaller; ++i){
tmp[i] = ptr[i];
}
delete[] ptr;
cap = newCap;
ptr = tmp;
}</T></typename>

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

make a destructor for the Darray class.

A

template <typename>
Darray<T>::~Darray(){
delete[] ptr;
}</T></typename>

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

overload operator[] for the Darray class so that it takes an int as a parameter and is non const.

A

template <typename>
T& Darray<T>::operator[](int i){
return ptr[i];
}</T></typename>

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

overload operator[] for the Darray class so that it takes an int as a parameter and is const.

A

template <typename>
T Darray<T>::operator[](int i) const{
return ptr[i];
}</T></typename>

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

make a method for the Darray class called capacity that returns the capacity of ptr.

A

int Darray<T>::capacity() const{
return cap;
}</T>

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

what are the private variables for the Stack class?

A

T s[size];
int tos; //top of stack

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

make a method for the Stack class called isEmpty that takes no parameters and returns whether the Stack is empty.

A

bool Stack::isEmpty() const{
return (tos == -1);
}

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

make an in-line default constructor for the Stack class.

A

Stack() : tos(-1){}:

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

make a method for the Stack class called isFull that takes no parameters and returns whether the Stack is full.

A

template <typename>
bool Stack<T>::isFull() const{
return (tos >= size);
}</T></typename>

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

make a method for the Stack class called push that pushes an item onto the Stack.

A

template <typename>
void Stack<T>::push(const T& item){
assert(!isFull());
s[++tos] = item //increment then return
}</T></typename>

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

make a method for the Stack class called pop that returns the newest item in the Stack and ‘pops’ it.

A

template <typename>
T Stack<T>::pop(){
assert(!isEmpty());
return s[tos--]; //return then decrement
}</T></typename>

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

make a method for the Stack class called pop that returns the newest item in the Stack.

A

template <typename>
T Stack<T>::top(){
assert(!isEmpty());
return s[tos];
}</T></typename>

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

write a method for the String class called infix_to_postfix that uses a Stack variable and returns the postfix version of the equation entered.

A

String infix_to_postfix(const char expr[]){
Stack<String> s;
char token;
String lhs, rhs, op;
int i = 0;
while(expr[i] != 0){
token = expr[i];
\++i;
if(token == ')'){
rhs = s.pop();
op = s.pop();
lhs = s.pop();
s.push(lhs + rhs + op);
}
else if(token !+ '('){
String temp = " ";
temp[0] = token;
s.push(temp);
}
}
return s.pop();
}</String>

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

write a method for the String class called infix_to_prefix that uses a Stack variable and returns the prefix version of the equation entered.

A

String infix_to_prefix(const char expr[]){
Stack<String> s;
char token;
String lhs, rhs, op;
int i = 0;
while(expr[i] != 0){
token = expr[i];
\++i;
if(token == ')'){
rhs = s.pop();
op = s.pop();
lhs = s.pop();
s.push(op + lhs + rhs);
}
else if(token !+ '('){
String temp = " ";
temp[0] = token;
s.push(temp);
}
}
return s.pop();
}</String>

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

write a helper method for the EvalPostFix method called isoperand that returns whether a character is an operand.

A

bool isoperand(char ch){
return (ch == ‘+’ || ch == ‘-‘ || ch == ‘/’ ||
ch == ‘*’);
}

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

write a helper method for the EvalPostFix method called EVAL that evaluates an expression.

A

int EVAL(int lhs, char op, int rhs){
switch(op){
case ‘+’: return lhs + rhs;
case ‘-‘: return lhs - rhs;
case ‘/’: return lhs / rhs;
case ‘*’: return lhs * rhs;
}
return 0;
}

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

make a method for the String class called EvalPostFix that evaluates the postfix expression.

A

int EvalPostFix(const char& expr[]){
Stack<int> s;
int i = 0;
while(expr[i] != 0){
if(!isoperand()){
int lhs, rhs;
rhs = s.pop();
lhs = s.pop();
s.push(EVAL(lhs, expr[i], rhs);
}
else{
s.push((int(ch) - int('0'));
}
\++i;
}
return s.pop();
}</int>

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

write a recursive method for the Fibonacci sequence.

A

int fib(int n){
if(n <= 2){
return 1;
}
else{
return fib(n - 1) + fib(n - 2);
}
}

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

write a recursive method to find a factorial.

A

int nfact(int N){
if(n <= 1){
return 1;
}
else{
return nfact(N - 1);
}
}

25
write an enum for the pole class (towers of hanoi)
enum poleName {left, right, middle};
26
what are the private variables for the pole class (towers of hanoi)?
poleName p (enum for the poles)
27
write the in-line function definition for the default constructor for the pole class.
pole(){};
28
write the in-line function definition for a constructor for the pole class that takes a poleName as a parameter.
pole(poleName x) : p(x) {};
29
write the in-line function definition for the output operator for the pole class that takes an ostream and a pole as parameters.
friend std::ostream& operator<<(std::ostream& out, pole rhs){ switch(rhs.p){ case left: out << "left"; break; case right: out << "right"; break; case middle: out << "middle"; break; } return out; }
30
write a free function called move that is recursive and performs the towers of hanoi operations using the pole class.
void move(int n, pole src, pole aux, pole dest){ if(n <= 1){ std::cout << "move disk from" << src << "to" << dest << std::endl; else{ move(n-1, src, dest, aux); std::cout << "move disk from" << src << "to" << dest << std::endl; move(n-1, aux, src, dest); } }
31
what are the private variables for the queue class (no dynamic memory).
int front int back bool empty T q[SIZE]; const int SIZE = 100;
32
make an in-line definition for the default constructor for the queue class.
queue(): front(0), back(0), empty(true) {};
33
make an in-line definition for the method isEmpty for the queue class.
bool isEmpty() const {return empty;};
34
make an in-line definition for the method isFull for the queue class.
bool isFull() const {return back == front && !empty;};
35
make a method for the queue class called enqueue that takes a T item and adds it to the queue.
template void queue::enqueue(const T& item){ assert(!isFull()); q[back] = item; back = (back + 1) % size; empty = false; }
36
make a method for the queue class called dequeue that takes no parameters deletes and returns the first item from the queue.
template T queue::dequeue(){ assert(!isEmpty()); T result = queue[front]; front = (front + 1) % SIZE; if(front == back){ empty = true; } return result; }
37
what are the public data members of the node class?
T data; node *next;
38
what are the private data members of the stack class with dynamic memory?
node *tos; (top of stack)
39
make an in-line definition for the default constructor of the stack class with dynamic memory.
stack(): tos(0) {};
40
make an in-line definition for a method called isEmpty for the stack class with dynamic memory.
bool isEmpty() const {return tos == 0;};
41
make an in-line definition for a method called Top for the stack class with dynamic memory that returns what is at the top of the stack.
T top() const {assert(!isEmpty()); return tos -> data;};
42
make a method called push for the stack class with dynamic memory that adds an item to the top of the stack. Include requires and ensures.
//REQUIRES: tos -> x1 -> x2 -> ... -> xn -> 0 //ENSURES: tos -> item -> x1 -> x2 -> ... -> xn -> 0 template void stack::push(const T& item){ assert(!isFull()): node *temp = new node item; temp->next = tos; tos = temp; }
43
make a method called pop for the stack class with dynamic memory that removes an item from the top of the stack. Include requires and ensures.
//REQUIRES: tos -> x1 -> x2 -> ... -> xn -> 0 //ENSURES: tos -> x2 -> ... -> xn -> 0 template T stack::pop(){ assert(!isEmpty()); T result = tos -> data; node *temp = tos; tos = tos -> next; delete temp; return result; }
44
make the destructor for the stack class with dynamic memory.
template stack::~stack(){ while(tos != 0){ node *temp = tos; tos = tos -> next; delete temp; } }
45
make a copy constructor for the stack class with dynamic memory.
template stack::stack(const stack& actual) : stack(){ if(actual.tos == 0){ return; } tos = new node(actual.tos->data); node *bottom = tos; node *temp = actual.tos->next; while(temp != 0){ bottom->next = new node(temp- >data) bottom = bottom->next; temp = temp->next; } }
46
make the swap method for the stack class with dynamic memory.
template void stack::swap(const stack& rhs){ node *temp = tos; tos = rhs.tos; rhs.tos = temp; }
47
make a method call isFull for the stack class with dynamic memory that returns whether the stack is full.
template bool stack::isFull() const{ node *temp = new(std::no throw) node(); if(temp == 0){ return true; } delete temp; return false; }
48
write an in-line definition for the default constructor for the queue class with dynamic memory.
queue() : beginning(0), end(0) {};
49
what are the private variables for the queue class with dynamic memory?
node *beginning; node *ending;
50
make an in-line definition for the overloaded operator= for the queue class with dynamic memory that is canonical.
queue& operator=(queue rhs){swap(rhs); return *this;};
51
make an in-line definition for the isEmpty method for the queue class with dynamic memory.
bool isEmpty() const {return beginning == 0;};
52
write a method for the queue class with dynamic memory called enqueue that adds an item to the queue. Include requires and ensures.
//REQUIRES: beginning -> x1 -> ... -> xn <- end && !isFull() //ENSURES:: beginning -> x1 -> ... -> xn -> item <- end template void queue::enqueue(const T& item){ assert(!isFull()); if(beginning == 0){ beginning = new node(item); ending = beginning; } else{ ending -> next = new node(item); ending = ending -> next; } }
53
write a method for the queue class with dynamic memory called dequeue that removes the first item from the queue. Include requires and ensures.
//REQUIRES:: beginning -> x1 -> x2 -> ... -> xn <- end && !isEmpty() //ENSURES:: beginning -> x2 -> ... -> xn <- ending template T queue::dequeue(){ assert(!isEmpty()); node *temp = beginning; T result = beginning -> data; beginning = beginning -> next; if(beginning == 0){ end = 0; } delete temp; return result; }
54
write a method for the queue class with dynamic memory called front that looks at the front item in the queue.
template T queue::front(){ assert(!isEmpty()); return beginning -> data; }
55
write a destructor for the queue class with dynamic memory.
template queue::~queue(){ node *temp; while(beginning != 0){ temp = beginning; beginning = beginning -> next; delete temp; } }
56
make a copy constructor for the queue class with dynamic memory.
template queue::queue(const queue& actual) :queue(){ if(actual.isEmpty()){ return; } beginning = new node (actual.beginning -> data); node *temp = actual.beginning -> next; while(actual.beginning != 0){ ending = new node(temp -> data); ending = ending -> next; temp = temp -> next; } }
57
make the swap method for the queue class with dynamic memory.
void queue::swap(const queue& rhs){ node *temp = beginning; beginning = rhs.beginning; rhs.beginning = temp; temp = end; end = rhs.end; rhs.end = temp; }
58
make an isFull() method for the queue class with dynamic memory.
bool queue::isFull() const{ node *temp = new(std::no throw) node(); if(temp == 0){ return true; } delete temp; return false; }