MTH4300 Home
# MTH 4300: Midterm 1 Practice 3

**Problem** 1. The user input consists of positive integers. A negative integer or \(0\) is a sign that the input is over. Create the program that counts the number of the integers \(x\) from the user input that satisfy exactly one of the following three conditions:
**Problem** 2. Create a program that performs the following tasks:
**Problem** 3.
The node of the linked list is declared in the following way
**address** of the node that contains the number \(42\)?
\[\;\]
Note: Some of the choices offer the code that is unsafe: The code may work and produce the desired result in certain "lucky" situations but may crash the computer in others. You are asked to identify the code that would work always. **Do not** implement the codes from this problem. Some of them could access illegal locations and damage your computer.
**Problem** 4. Determine the size of the memory leak that the command
**Problem** 5. The code below contains the declaration of the node of the link list, and the implementations of the functions
**Problem** 6. Create a function that reverses the order of elements of a part of a linked list. The contents of the linked list are positive integers. The input of the function consists of three components: The first component is the pointer to the first element of the linked list; the second component is an integer

**Condition 1:**The number \(x\) is not divisible by \(7\);**Condition 2:**The number \(x\) is divisible by \(5\) but not by \(11\);**Condition 3:**The number \(x\) does not belong to the open interval \((100,200)\);

**Task 1.**The program reads an integer \(n\) from the user input. If the number \(n\) is negative or \(0\), then the number \(n\) should be replaced by \(5\);**Task 2.**The program allocates the memory in heap for a sequence`s`

of \(n\) real numbers;**Task 3.**The program reads \(n\) real numbers and stores them in the sequence created in Task 2.**Task 4.**The program reads two integers \(i\) and \(j\). If the user provides integers \(i\) and \(j\) that fail the condition \(0\leq i < j \leq n\), then the program should replace \(i\) and \(j\) with \(0\) and \(n\).**Task 5.**The program prints the numbers`s[i]`

,`s[i]+s[i+1]`

, \(\dots\),`s[i]+s[i+1]+...+s[j-1]`

.**Task 6.**The program returns to the operating system all the memory it allocated on the heap.

class ListNode{ public: int c; ListNode* next; };It is known that the linked list contains the terms \(19\), \(29\), \(38\), \(42\), \(47\). Assume that

`runner`

is the pointer to the node that contains the number \(29\). Which among the following codes is the safest way to print the **(A)**`ListNode t=*runner; t = (*t).next; t = (*t).next; std::cout<<(long)((*t).next);`

**(B)**`ListNode* t; t=runner+2; std::cout<<(long)(t);`

**(C)**`ListNode t; t = *(runner+2); std::cout<<(long)(t);`

**(D)**`ListNode t; t = *(runner)+2; std::cout<<(long)(&t);`

**(E)**`ListNode* t=runner; t = (*t).next; t = (*t).next; std::cout<<(long)(t);`

**(F)**`ListNode* t=runner; t = (*t).next; t = (*t).next; std::cout<<(long)((*t).next);`

**(G)**`ListNode* t=runner; t = (*t).next; std::cout<<(long)(t);`

**(H)**`ListNode* t=runner; t = (*t).next; std::cout<<(long)(&t);`

**(I)**`ListNode* t=runner; t = (*t).next; t = (*t).next; std::cout<<(long)(&t);`

**(J)**`ListNode t=*runner; t = (*t).next; std::cout<<(long)(&t);`

`wastefulFunction();`

creates. The "size" of the memory leak is defined as the number of memory locations of type `int`

that were created but not deleted. WARNING: Do not implement the codes from this problem. The memory leak is so serious that it may crash or damage your computer.
void wastefulFunction(){ int* pointer; long i=0; long limit=10; while(i < limit){ if(limit < 1000000){ ++limit; } pointer = new int; pointer = new int; delete pointer; pointer = new int[5500]; pointer = new int[3500]; delete[] pointer; pointer = new int[1500]; pointer = new int[1000]; ++i; } delete[] pointer; }

`deleteList`

and `wolf`

.
class LN{ public: long content; LN* aNext; }; void deleteList(LN* aHead){ if(aHead!=nullptr){ deleteList(aHead->aNext); delete aHead; } } LN* wolf(LN* first){ if(first==nullptr){ return nullptr; } LN* alpha=wolf(first->aNext); for(long i=0;i < (first->content);++i){ LN** beta; beta = new LN*[10001]; *beta = alpha; for(long j=1;j < 1001;++j){ beta[j]=new LN; beta[j]->content = 15; beta[j]->aNext=beta[j-1]; } alpha=beta[first->content]; delete[] beta; } first->aNext=alpha; return first; }The variable

`addressHead`

is of type `LN*`

and contains the address of the first node of the linked list with \(5000\) terms. The list is periodic with period \(5\). The first \(5\) numbers are \(7, { }8,9,10,11\). Then next \(5\) numbers are \(7, { }8,9,10,11\) again, etc. Determine the size of the memory leak that the command `deleteList(wolf(addressHead));`

creates. The "size" of the memory leak is defined as the number of list nodes that were created but not deleted. WARNING: Do not implement the codes from this problem. The memory leak is so serious that it may crash or damage your computer.
`start`

and the third component is the integer `end`

. The function should reverse the order of the elements from `start`

to `end`

and return the pointer to the new list. You are allowed to assume that all the elements of the list are different integers and that the numbers `start`

and `end`

are different.
\[\;\]
Examples: Assume that the integers in the linked list are \(2\), \(3\), \(4\), \(5\), \(6\), \(7\), \(8\), \(9\).
\[\;\]
If the numbers `start`

and `end`

satisfy: `start=4`

, `end=7`

the resulting list should be \(2\), \(3\), \(7\), \(6\), \(5\), \(4\), \(8\), \(9\).
\[\;\]
If the numbers `start`

and `end`

satisfy: `start=2`

, `end=7`

the resulting list should be \(7\), \(6\), \(5\), \(4\), \(3\), \(2\), \(8\), \(9\).