MTH4300 Home

MTH 4300: Midterm 1 Practice 5

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 at least one of the following three conditions:
  • Condition 1: The number \(x\) is not divisible by \(7\);
  • Condition 2: The number \(x\) is divisible by \(5\) or by \(11\) but not by both \(5\) and \(11\);
  • Condition 3: The number \(x\) belongs to the union of open intervals \((100,200) \cup (300,400)\);

Problem 2. What is the content of the variable c after the following block of code?
  int a; a=15;
  int* b; b=&a; *b=25;
  int c; c=50;
  b=new int; *b=55; 
  c = c+(*b);
  b=&c;
  c = a + (*b) + c;

Problem 3. The node of the linked list is declared in the following way
struct ListNode{
public:
   int c;
   ListNode* next;
};
It is known that the linked list contains the terms \[17, 21, 32, 43, 60, \quad\mbox{ in this order.}\] Assume that runner is the pointer to the node that contains the number \(21\). Which among the following codes is the safest way to print the address of the node that contains the number \(43\)? \[\;\] 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.
  • (A) ListNode* t=runner; t = (*t).next; std::cout<<(long)(t);
  • (B) ListNode* t=runner; t = (*t).next; std::cout<<(long)(&t);
  • (C) ListNode* t=runner; t = (*t).next; t = (*t).next; std::cout<<(long)(&t);
  • (D) ListNode t=*runner; t = (*t).next; std::cout<<(long)(&t);
  • (E) ListNode t=*runner; t = (*t).next; t = (*t).next; std::cout<<(long)((*t).next);
  • (F) ListNode* t; t=runner+2; std::cout<<(long)(t);
  • (G) ListNode t; t = *(runner+2); std::cout<<(long)(t);
  • (H) ListNode t; t = *(runner)+2; 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; t = (*t).next; std::cout<<(long)((*t).next);

Problem 4. The implementation of the function modification is:
int modification(int * aA){ 
   if( (*aA) == 20 ){
      *aA=(*aA)*1; 
   }
   else{
      *aA=(*aA)*10; 
   }
   int z= *(aA+50);
   return z+(*aA+50);
} 
Assume that aX contains the address of the first term of the sequence of integers. The elements of the sequence are \(10\), \(12\), \(14\), \(16\), \(\dots\). Every two consecutive terms differ by \(2\) and there are \(1000\) terms in the sequence. What is the content of the variable total after the following block of code?
int total;
total=modification(aX+5)+modification(aX+15);
total+=aX[5]+aX[15];

Problem 5.

Create a function that deletes the last \(k\) elements of the linked list. The arguments of the function should be the pointer to the head of the linked list and the integer \(k\). The nodes of linked list contain the real numbers of type double. The resulting program should visit each element at most a constant number of times that does not depend on \(k\). (In particular, one unacceptable solution is to write the function that deletes the last term, and then call this function \(k\) times.)

Problem 6.

For a given positive integer \(m\), the sequence \(x_0\), \(x_1\), \(\dots\), \(x_{k-1}\) of positive integers is called \(m\)-separated, if the differences \((x_1-x_0)\), \((x_2-x_1)\), \(\dots\), \((x_{k-1}-x_{k-2})\) are all greater than or equal to \(m\).

The user input consists of two positive integers \(m\) and \(k\). Create a program that finds the largest integer \(n\) for which there exists an \(m\)-separated sequence of positive integers of length \(n\) whose sum of elements is \(k\).

Example:
Input: 
3 50
Output:
5

Explanation. The sequence \((3, 6, 10, 14, 17)\) is a \(3\)-separated sequence with \(5\) terms and \(3+6+10+14+17=50\).