MTH4300 Home

MTH 4300: Final Practice 5

Problem 1

The user input consists of positive integers \( a \), \( b \), and \( p \). Create the program that prints all integers \( x \) for which \( a\leq x\leq b \) and for which the number \( 2 x^3+3x^2+3 \) is divisible by \( p \).

If there are no such numbers \( x \), then print the message:

There are no integers that satisfy the required relation.

Example:

Input:

5 30 11

Output:

7 18 29 

Problem 2

What happens when the following program is executed? 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 Rabbit{
 private:
     long skill;
 public: 
     Rabbit(long = 0);
     void setSkill(long = 0);
     long getSkill();
 };
 Rabbit::Rabbit(long x){
     skill=x;
 }
 void Rabbit::setSkill(long x){
     skill=x;
 }
 long Rabbit::getSkill(){
     skill += 6;
     return skill;
 }


 int main(){
    Rabbit r;
    long totalSum=5;
    for(int i=0;i<4;++i){
         totalSum += r.getSkill();
    }
    std::cout<<totalSum<<std::endl;
    return 0;
 }

Problem 3

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

// myProgram.cpp
 // compile with 
 // c++ myProgram.cpp -o mP -std=c++11
 // execute with 
 // ./mP 
 #include<iostream>
 

 int main(){
    int value1=0;
    int value2=0;
    int* p;
    p=new int[100];
    *p=14;
    *(p+1)=3;
    for(int i=2;i<100;++i){
         p[i]=p[i-2];
    }
       for(int i=0;i<100;++i){
            if( i+p[i] == 18){
                 value1+=p[i];
                for(int j=0;j<100;++j){
                   value2+=p[j];
              }
         }
    }
    std::cout<<value1<<" "<<value2<<std::endl;
    return 0;
 }

Problem 4

What is printed on standard output when the following program is executed? Provide a rigorous justification for your answer.

// mysteryPrinting.cpp
 // compile with
 // c++ mysteryPrinting.cpp -o mPrinting -std=c++11 -fno-elide-constructors
 // execute with
 // ./mPrinting
 #include<iostream>
 class Wolf{
 private:
      std::string name;
 public:
     Wolf(const std::string & = "");
     
     Wolf(const Wolf &);    
     Wolf(Wolf &&);
     void operator=(const Wolf &);
     void operator=(Wolf &&);
     virtual ~Wolf();
 };
 Wolf::Wolf(const std::string & s){
     name=s;
 }
 Wolf::Wolf(const Wolf& x){
     name="A"+x.name;
 }
 Wolf::Wolf(Wolf&& x){
     name="B"+x.name;
     x.name+="C";
}
 void Wolf::operator=(const Wolf & x){
     name+="D"+x.name;
 }
 void Wolf::operator=(Wolf && x){
     name+="E"+x.name;
     x.name+="F";
 }
 Wolf::~Wolf(){
     std::cout<<name<<std::endl;
 }
 Wolf whisper(Wolf z){
     Wolf w("G");
     Wolf r=z;
     return w;
 }
 int main(){
     Wolf a("R");
     Wolf b("W");
     b=whisper(a);
     return 0;
 }

Problem 5

The user provides a positive integer \( n \) followed by \( n \) lines. Each line starts with the name of a company which is of type std::string. Then the user gives an integer \( k \). The remainder of the line consists of \( k \) pairs of strings and numbers. Each string is a job position that the company has and the number after the string is the number of employees holding this position in one of the locations. Your program should allow for several lines of the input to correspond to the same company. Also, the program should allow for each company to have any of its job positions appear multiple times in the input - in this case the number should be the sum of all of the numbers collected for this particular job.

After these \( n \) lines of input the user provides two more lines. The first of them contains a single integer \( T \), which is the total number of job positions. In the last line the user provides for each job the number that represents its salary.

Create the program that for each company outputs the total salary of all employees. The program should print companies in alphabetical order.

Example:

Input:

4
 United 4  driver 700 mechanic 300 pilot 500 mechanic 700
 Fedex 2 driver 500 pilot 300 
 UPS 3 pilot 700 mechanic 700 engineer 500
 Fedex 2 programmer 700 pilot 700
 5 
 mechanic 30 driver 40 pilot 50 engineer 50 programmer 40

Output:

Fedex has total operating cost 98000
 United has total operating cost 83000
 UPS has total operating cost 81000

Problem 6

The user input consists of a positive integer \(n\geq5\) and a sequence \(x_1\), \(x_2\), \(\dots\), \(x_{n-1}\) of \(n-1\) positive real numbers. There are \(n\) airports arranged on a straight line. Their names are \(A_0\), \(A_1\), \(\dots\), \(A_{n-1}\). Every two adjacent airports are \(100\) miles apart. A small airplane is located at the airport \(A_0\) and needs to fly to the airport \(A_{n-1}\). Once it takes off, the range of the airplane is \(300\) miles. The landing fee for the airport \(A_1\) is \(x_1\), the landing fee for \(A_2\) is \(x_2\), \(\dots\), the landing fee for the airport \(A_{n-1}\) is \(x_{n-1}\). Create the program that calculates the smallest amount of money that the airplane needs to be able to complete the journey from \(A_0\) to \(A_{n-1}\).

The airplane does not have to pay the fee for the airport \(A_0\) since it came there on a boat or by some other transportation method that avoided landing.

Example:

Input:

12
 1 2 9 1 9 9 1 2 9 9 5

Output:

 10

Explanation: The cheapest route involves landing at the airports \(1\), \(4\), \(7\), \(8\), and \(11\). The total fee is \[x[1]+x[4]+x[7]+x[8]+x[11]=1+1+1+2+5=10.\]