CSCI 159: Spring 2023 Final Exam
Sections S23N01-S23N02 (Dr. Wessels)


Question 1: Input and error checking [10]

Write a complete and correct C++ function that meets all of the following requirements:


Question 2: Processing sorted arrays [10]

Write a complete and correct C++ function that takes a sorted array of integers and its size as parameters and returns true if the array contains any duplicate values, false otherwise. (You can assume the array contents are already sorted, you do not need to check or sort.)
Hint: there were any duplicate values in a sorted array they would have to be beside each other.


Question 3: Nested loops [10]

Write a complete and correct C++ program that takes two integer parameters, M and N, and displays M rows of output, where each row consists of the character '*' N times.


Question 4: Recursion [10]

A recursive C++ function is given below. Show the exact output the function would produce if it was called as follows: recurse(7,11);

void recurse(int x, int y)
{
   cout << "beginning:" << x << "," << y << endl;
   if (x < y) {
      recurse(x+1, y-1);
   }
   cout << "ending: " << x << "," << y << endl;
}


Question 5: Arrays and loops [10]

Write a complete and correct C++ function that meets all the requirements below:

For example, if an array of of size 7 had values 3,19,27,4,8,-1,12, then after running slide once the array contents would become 0,3,19,27,4,8,-1. If we called slide on the array again then its contents would become 0,0,3,19,27,4,8, etc.


Question 6: Null terminated character arrays, cstring [10]

Suppose the following segment of C++ code is run:

   char str1[80];
   char str2[60];
   char str3[70];
   strcpy(str1, "goodbye!");
   strcpy(str2, "hello world!");
   strcpy(str3, "almost done with 159...");
   str1[7] = '\0';
   strcpy(str3, str1);
Assuming the code segment had just finished, answer the following questions:
  1. What would be returned by strlen(str1)?
  2. What is displayed by cout << str2;
  3. If we were to run strlen on each of str1, str2, and str3, which would have the greatest length (or are all equal length)?
  4. What character is in str3[7]?
  5. What character is in str3[8]?


Question 7: structs [10]

Given the definition of the DATA struct below, write a complete and correct C++ function that meets all the following requirements:

struct DATA {
   int first;
   float second;
   char third;
};


Question 8: Arrays of structs [10]

Given the Fraction definition below, provide a complete and correct C++ function that meets all the following requirements:

struct Fraction {
   int numerator;
   int denominator;
};


Question 9: Pointers, dynamic allocation [10]

(i) Explain the apparent purpose of function f below:

double *f(int X)
{
   double *result;
   result = new double[X];
   for (int i = 0; i < X; i++) {
       result[i] = 0.0;
   }
   return result;
}
(ii) Add error checking to the function to deal with cases where the new operation failed because of insufficient memory.


That's all, good luck with the rest of your exams and have a good summer!