MTH4300 Home

MTH 4300: Midterm 2 Practice 1

Problem 1.

Create a function sumLargestAndSmallest that calculates the sum of the largest and the smallest term of the sequence x. The arguments of the function should be the address of the first term of the sequence and the length of the sequence.

For example if the sequence x has length 5 and if the terms of the sequence x are \(20\), \(7\), \(80\), \(93\), and \(50\), then after the command

long result=sumLargestAndSmallest(x,5);
is executed, the content of the varialbe result should be \(100\).

Problem 2. The user input consists of a positive integer \(n\), followed by a sequence \(x\) of \(n\) positive integers and two positive integers \(a\) and \(b\). Create a program that prints the sum of the terms of \(x\) that are divisible by either \(a\) or \(b\) but not by both \(a\) and \(b\).

Problem 3.

Assume that you have a supercomputer with sufficiently large memory and sufficiently fast processor. Assume that the class Space3D is already implemented and for each three real numbers \(a\), \(b\), and \(c\) it stores the color of the point in space whose coordinates are \((a,b,c)\). The color is stored as an integer of type long. The class has a method getColor that accepts three integers a, b, and c and returns the color of the point \((a,b,c)\). It also has a method setColor whose input consists of three real numbers \(a\), \(b\), and \(c\), and an integer \(x\). The method assigns the color \(x\) to the point \((a,b,c)\). What is the output of the function tiger() if the implementations of the functions elephant, giraffe, and tiger are given below.

long elephant(Space3D& s){
   return 3*s.getColor(6,1,1)-5*s.getColor(8,4,3);
long giraffe(Space3D s){
   return 4*s.getColor(6,1,1)-3*s.getColor(8,4,3);
long tiger(){
   Space3D monkey;
   long result1=elephant(monkey);
   long result2=giraffe(monkey);
   long result3=elephant(monkey);
   return 5*result1+4*result2+3*result3;

Problem 4.

Important notice: In this problem you will be asked to declare a class and implement only few of its methods. Do not implement all of the methods. You would spend a lot of time doing so and you will not earn extra credit for that!

You must use the keyword const whenever possible.

The class BigLibrary should have the private attribute aListSections which is the pointer to the head of a list. Each list node should contain the attribute nameOfSection of type std::string and a pointer to another list that contains books in the section of the library.

The public methods should include: addSection that adds the section to the library; addBookToSection that adds the book to the section (the names of book and section should be supplied as arguments); printAllBooksFromTheSection that prints all books in the specified section (the name of the section should be an argument of the method); printAll that prints all sections and all books.

In addtion, provide the declarations for the default constructor, copy constructor, move constructor, copy assignment, move assignment and the destructor.

Create implementations for the copy constructor, move assignment, and destructor.

Problem 5.

Warning: Do not implement the code from this problem on a standard computer. A serious damage can occur.

What does the following program print if it is executed on a computer with sufficiently fast CPU and sufficiently large memory? Provide a rigorous justification for your answer.

void generateSequence(long*& aX, long len){
   for(long i=0; i < len; ++i){
void funPointer(long* aX){ 
   for(long i=0;i<1000000;++i){ 
      aX=new long[8000000+i];
void funPointerReference(long*& aX){ 
   for(long i=0;i<1000000;++i){ 
      aX=new long[8000000+i];
int main(){
   long* x;
   x=new long[8000000]; 
   long res1=x[57];
   long res2=x[57];
   return 0;

Problem 6. Create a program that reads a positive integer \(n\) from the standard input and on standard output prints all possible sequences of length \(n\) whose terms are \(0\) and \(1\) in such a way that all of the following three requirements are met:
  • Requirement 1. Each sequence is printed in a separate line.
  • Requirement 2. Each of the \(2^n\) possible sequences is printed exactly once.
  • Requirement 3. Every two adjacent lines contain sequences that differ in exactly one term.
Input: 3