MTH4300 Home

MTH 4300: Midterm 2 Practice 7

Problem 1.

What happens when the following program is executed? Provide a rigorous justification for your answer.

 
#include<iostream>
int f(int x){
     if(((x%5==1)&&(x%2==0)) || (x%3==1) ) {
          return 10;
     }
     return 3;
}
int main(){
     int total=0;
     for(int i=0;i<30;++i){
          total+=f(i);
     }
     std::cout<<total<<std::endl;
     return 0;
 }

Problem 2.

What happens when the following program is executed? Provide a rigorous justification for your answer.

 
 #include<iostream>
 class Glass{
 public:
     int w;
     Glass();
     Glass(const Glass & );
 };
 Glass::Glass(){
     w=7;
 }
 Glass::Glass(const Glass & c){
     w=c.w+5;
 }
 int f1(Glass & x){
     return x.w;
 }
 int f2(Glass x){
     return x.w;
 }
 int main(){
     Glass tallGlass; 
     std::cout<<3*f1(tallGlass) + 4*f2(tallGlass)<<"\n";
     return 0;
 }

Problem 3.

What happens when the following program is executed? Provide a rigorous justification for your answer.

 
 #include<iostream>
 int g(int* s){
     int* t;
     t = s+9;
     (*t) += 8;
     return *t;
 }
 int main(){
     int* s;
     s=new int[40];
     for(int i=0;i<40;++i){
          s[i] = 10 * i + 1;
     }
     s[0]=g(s);
     std::cout<<s[0]+s[8]+s[9]<<std::endl;
     delete[] s;
     return 0;
 }

Problem 4.

Each object of the class MovieTheater should have two private attributes. The first theaterAddress should be of type std::string. The second private attribute should contain a stack of real numbers.

Your task is to create the appropriate class and the methods that are described below. You must use the keyword const whenever possible in the declaration and implementation of the class.

  • (a) getAddress should return the value of the attribute theaterAddress.
  • (b) setAddress with one argument of type std::string. The method should assign the provided argument to the attribute theaterAddress;
  • (c) getLastTicketPrice that returns the last (top) element of the stack;
  • (d) eraseLastTicketPrice that erases the top element of the stack;
  • (e) addTicketPrice with one argument that is a real number. The method should add this real number to the top of the stack;
  • (f) isEmpty that returns 1 if the stack is empty and 0 otherwise;
  • (g) Destructor that frees the memory.
  • (h) Default constructor (that creates an empty stack and sets theaterAddress to "nothing," copy constructor, copy assignment, move constructor, and move assignment. The move constructor and move assignment should not allocate additional memory for the stack, but instead should use the resources of the supplied argument.

Problem 5.

The user input consists of a sequence of distinct positive real numbers that ends in a negative number. The length of the sequence is not known in advance. Find the two numbers in the sequence that are closest to each other. Print these two numbers and their distance. The complexity of your program should be at most \(O(N\log N)\), where \(N\) is the number of terms in the sequence.

Example:

Input:
 9.2 5.1 11.7 5 12 -1
 Output:
 The closest numbers are 5.1 and 5. Their distance is 0.1.