MTH4300 Home

MTH 4300: Midterm 2 Practice 8

Problem 1. What does the following program print? Provide a rigorous justification for your answer.
 
#include<iostream>
int main(){
     int red, *green, *blue;
     red=27;
     green=&red;
     blue=new int;
     *green=28;
     int& yellow=*blue;
     *blue=29;
     yellow=30;
     std::cout<< red+(*green)+(*blue)+yellow<<std::endl;
     delete blue;
     return 0;
}

Problem 2. The user input consists of a list of positive integers. A negative integer is the sign that the input is over. We do not know in advance the total number of elements of the list. After the list is collected, the user provides a positive integer \(x\). Create a program that prints all elements from the list that are bigger than \(x\).

Problem 3. What does the following program print? Provide a rigorous justification for your answer.
// myProgram.cpp
// compile with 
// c++ myProgram.cpp -o mP -std=c++11
// execute with 
// ./mP 
#include<iostream>
class Bag{
private:
     int capacity;
public: 
     Bag(int = 0);
     void setCapacity(int = 0);
     int getCapacity();
};
Bag::Bag(int c){
     capacity=c;
}
void Bag::setCapacity(int c){
     capacity=c;
}
int Bag::getCapacity(){
     capacity += 4;
     return capacity;
}
int main(){
    Bag b;
    int totalSum=7;
    for(int i=0;i<3;++i){
         totalSum += b.getCapacity();
    }
    std::cout<<totalSum<<std::endl;
    return 0;
}

Problem 4. How many times is the letter A printed when the following code is executed? Provide a rigorous justification for your answer.
#include<iostream>
class Frog{
public:
   Frog();
   ~Frog();
};
Frog::Frog(){
   std::cout<<"AA";
}
Frog::~Frog(){
   std::cout<<"AAA";
}
int main(){
   Frog x;
   Frog* y; 
   Frog* z; 
   y=new Frog;
   for(long i = 0;i < 36;++i){
      if( ((i > 5) && (i < 8)) || ((i > 17) && (i < 21)) ){
         z = new Frog;
      }
   }
   Frog q;
   std::cout<<"\n";
   return 0;
}

Problem 5.

Important notice: In this problem you will be asked to declare a class and implement only few of its methods. Do not implement all of the methods. You would spend a lot of time doing so and you will not earn extra credit for that!

You must use the keyword const whenever possible.

The class University should have the private attribute allDepartments which is the pointer to the head of a list. Each list node should contain the attribute nameOfDepartment of type std::string and a pointer to another list that contains courses in the department.

The public methods should include: addDepartment that adds a department to the university; addCourseToDepartment that adds the course to the department (the names of course and department should be supplied as arguments); printAllCoursesFromTheDepartment that prints all courses in the specified department (the name of the department should be an argument of the method); printAll that prints all departments and all courses.

In addtion, provide the declarations for the default constructor, copy constructor, move constructor, copy assignment, move assignment and the destructor.

Create implementations for the move constructor, copy assignment, and destructor.

Problem 6. The objects of the class SN are nodes of a stack. The declaration of the class is
class SN{
public:
   double content;
   SN* aNext;
};
Create a function mergeTwoSortedStacks that has the following declaration
SN* mergeTwoSortedStacks(SN* a1, SN* a2)
The two arguments of the function a1 and a2 are the addresses of the top elements of two stacks whose terms are sorted in a non-decreasing order (hence the smallest elements in each of the stacks are on the top). The function should achieve the following outcomes:
  • The function should create a new stack that contains all elements from the two given stacks sorted in non-decreasing order;
  • The function should return the address of the head of the new stack;
  • The function should not modify or erase the terms from the stacks a1 and a2.
Remark: You do not need to implement the functions push and pop. You may assume that they are implemented in the usual way. However, if you need any other stack function, then you need to implement it.