MTH4300 Home

MTH 4300: Midterm 1 Practice 4

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 two of the following three conditions:
  • Condition 1: The number \(x(x+5)\) is not divisible by \(7\);
  • Condition 2: The number \(x\) is divisible by \(5\) but not by \(10\);
  • Condition 3: The number \(x\) does not belong to the union of closed intervals \([100,200] \cup [300,400]\);

Problem 2. The functions alpha, beta, gamma, and delta are implemented in the following way:
int alpha(int* a){
   (*a)=(*a)+25;
   return *a;
}
int beta(int* a){
   return (*a)+25;
}
int gamma(int a){
   a=a+25;
   return a;
}
int delta(int a){
   return a+25;
}
It is known that x is a pointer to a memory location that contains the integer \(5\). Determine the content of the variable z after the following command
int z=gamma(*x)+alpha(x)+beta(x)+delta(*x);

Problem 3.

The node of the linked list is declared in the following way

class ListNode{
public:
   int c;
   ListNode* next;
};

It is known that the linked list contains the terms \[18, 29, 34, 35, 73, \quad\mbox{ in this order.}\]

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 address of the node that contains the number \(35\)?

  • (A) ListNode* t=runner; t = (*t).next; 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).next);
  • (D) ListNode* t; t=runner+2; std::cout<<(long)(t);
  • (E) ListNode t; t = *(runner+2); std::cout<<(long)(t);
  • (F) ListNode t; t = *(runner)+2; std::cout<<(long)(&t);
  • (G) ListNode* t=runner; t = (*t).next; t = (*t).next; std::cout<<(long)(t);
  • (H) ListNode* t=runner; t = (*t).next; t = (*t).next; std::cout<<(long)((*t).next);
  • (I) ListNode* t=runner; t = (*t).next; std::cout<<(long)(t);
  • (J) ListNode* t=runner; t = (*t).next; std::cout<<(long)(&t);

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. The implementation of the function seqModification is:
int seqModification(int * aQ){ 
   if( (*aQ) == 55 ){
      *aQ=(*aQ)*5; 
   }
   else{
      *aQ=(*aQ)*10; 
   }
   int z= *(aQ+50);
   return z+(*aQ+50);
} 
Assume that aP contains the address of the first term of the sequence of integers. The elements of the sequence are \(5\), \(15\), \(25\), \(35\), \(\dots\). Every two consecutive terms differ by \(10\) and there are \(1000\) terms in the sequence. What is the content of the variable result after the following block of code?
int result;
result=seqModification(aP+3)+seqModification(aP+5);
result+=aP[3]+aP[5];

Problem 5. It is known that aX is a pointer the first element of a sequence of \(5000\) integers of type long. The first five terms of the sequence are \(50, 60, 70, 80, 90\). Determine the size of the memory leak produced by the code below:
long i=5;
while(i<5000){
   *(aX+i)=*(aX+i-5);
   ++i;
}
i=0;
while(i<5000){
   long* m;
   m=new long[3];
   if (*(aX+i) > 52){
      m=new long[8];
      m=new long[7];
   }
   delete[] m;
   ++i;
}
delete[] aX;
The "size" of the memory leak is defined as the number of memory locations of type long 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.

Problem 6.

Create a function that re-organizes the terms of the linked list in such a way that the following requirements are satisfied:

  • Requirement 1. The even terms must be in the beginning of the list and the odd terms must be in the end of the re-organized list.
  • Requirement 2. The order of the odd and the even terms must be reversed from their original order.

The function should have one argument: the pointer to the head of the original list. After the terms are re-organized the function should return the pointer to the head of the new list.

Note: The function should re-organize the terms without wasting memory: you are not allowed to assume that the computer will have sufficient memory to make a copy of the list and keep both the original and the copy in the memory at the same time.

Examples:
Input: \({ }\)5 7 9 8 6 12 7 19 10 2
Output: \({ }\)2 10 12 6 8 19 7 9 7 5