MTH4300 Home
# MTH 4300: Midterm 2 Practice 2

**Problem** 1. What is a copy assignment operator? Provide an explanation and an example of a program in which a copy assignment operator is implemented and used.
**Problem** 2.
The user input consists of positive integers that are followed by one negative integer \(x\). Create the program that performs the following three tasks:
**Problem** 3.
The class
**Problem** 4. Determine the size of the memory leak created by the function
**Problem** 5. Determine the size of the memory leak created by the function
**Problem** 6.

**Task 1.**Create a stack consisting of positive integers provided by the user.**Task 2.**Identify all members from the stack that are divisible by \(x^2\) and print them on standard output.**Task 3.**Delete all elements of the stack to prevent the memory leak.

`Animal`

has a private member `victims`

of type `long*`

and a public method `eatHuman`

whose implementation is
long Animal::eatHuman(long n){ *victims=40; *(victims+n+1) = n*10+40; return *(victims+n); }It is known that

`mosquito`

is an object of the class `Animal`

. It is known that `mosquito.victims`

is a pointer to the beginning of a block of memory that contains \(1000000000\) integers of type `long`

. What is the content of the variable `victories`

after the following code?
long victories=0; long x; long i=0; while(i<100000000){ x=mosquito.eatHuman(i); if((800-x)*(x-340)>0){ ++victories; } ++i; }WARNING: Do not implement the codes from the formulation of the problem. They could de-stabilize, crash, and/or damage your computer.

`memLeak()`

listed below. The "size" of the memory leak is defined as the number of locations of type `long`

that were created but not deleted.
#include<iostream> void slow(long*& w){ long*& s=w; s=new long[45]; *s=8; } void fast(long* w){ long*& s=w; s=new long[75]; *s=8; } int memLeak(){ long* x; x=new long[10]; x=new long[20]; *x=3; slow(x); if(*x==3){delete[] x;} x=new long[35]; x=new long[40]; *x=3; fast(x); if(*x==3){delete[] x;} return 0; }WARNING: Do not implement the codes from this problem. The memory leak may crash or damage your computer.

`memoryLeak()`

listed below. The "size" of the memory leak is defined as the number of locations of type `long`

that were created but not deleted. Assume that the program is compiled with the flag `-fno-elide-constructors`

.
#include<iostream> class Line{ private: long* a; public: Line(); Line(const Line&); Line(Line&&); void operator=(const Line&); void operator=(Line&&); ~Line(); }; Line::Line(){ a=new long; a=new long; } Line::Line(const Line& x){ a=new long; a=new long; a=new long; } Line::Line(Line&& x){ a=new long; } void Line::operator=(const Line& x){ a=new long;a=new long;a=new long; } void Line::operator=(Line&& x){ a=new long; } Line::~Line(){ a=new long; a=new long; } Line myFun(Line x){ Line y=x; return y; } int memoryLeak(){ Line x,z; z=x; for(int i=0;i<10;++i){ z=myFun(x); } return 0; }WARNING: Do not implement the codes from this problem. The memory leak may crash or damage your computer.

For two sequences \(z=\left(z_0, z_1, \dots, z_{M-1}\right)\) and \(w=\left(w_0,w_1,\dots, w_{M-1}\right)\) of integers we say that \(z\) is *smaller than* \(w\) if there exists an index \(p\in\{0,1,\dots, M-1\}\) such that \(z_p < w_p\) and \(z_i=w_i\) for all \(i\in [0,p-1]\). The sequences \(z\) and \(w\) must have the same length in order to be compared.

The user input consists of the following \(3\) components: 1) a positive integer \(k\) greater than or equal to \(2\); 2) a positive integer \(M\); 3) a sequence \(z=(z_0\), \(z_1\), \(\dots\), \(z_{M-1})\) of positive integers from the set \(\{0\), \(1\), \(\dots\), \(k-1\}\).

Make a program that based on the user's input generates the smallest sequence \(w=(w_0\), \(w_1\), \(\dots\), \(w_{M-1})\) of integers from \(\{0\), \(1\), \(\dots\), \(k-1\}\) that is larger than \(z\). In other words, \(w\) must satisfy the following three conditions:

**Condition 1.** All elements of \(w\) are from \(\{0\), \(1\), \(\dots\), \(k-1\}\).

**Condition 2.** \(z\) is smaller than \(w\).

**Condition 3.** There is no sequence \(q\) that is between \(z\) and \(w\). (This is an alternative way to state this last condition 3: There does not exist a sequence \(q\) of length \(M\) such that \(z\) is smaller than \(q\) and \(q\) is smaller than \(w\).)

If the sequence \(w\) does not exist, then print the message that says so.

Example 1: k=3; M=5; z=(0,2,0,2,2) Output: w=(0,2,1,0,0) Example 2: k and M are the same as in Example 1; z=(2,2,2,2,2) Output: The sequence w does not exist.