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:
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:
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!