Question 1: Basic calculation and I/O[10]
Write a complete and correct C++ program that does the following (in order):
Question 2: Iteration (loops) [10]
(i) Provide an implementation for the function f prototyped below. Your implementation must use a loop (not recursion) to achieve the repetition. (Tip: x % k is 0 if k evenly divides x.)
// displays all even numbers from m to n (inclusive) that are also divisible by either 3 or 5 // e.g. f(1,20) would display 6, 10, 12, 18 void f(int m, int n);
(ii) Show, as precisely as possible, the output from the following code segment:
int array[5] = { 1, 3, 5, 7, 9 };
int data[6] = { 2, 4, 6, 8, 10, 12 };
for (int i = 0; i < 5; i++) {
cout << array[i] << ":";
for (int j = 0; j < 6; j++) {
if (data[j] < arr[i]) {
cout << " " << data[j];
}
}
cout << endl;
}
Question 3: Recursion [10] (i) Show, as precisely as possible, the output produced by the following program.
#include <iostream>
using namespace std;
void f(int arr[], int size, int pos);
int main()
{
int data[9] = { 2, 4, 6, 8, 10, 12, 14, 16, 18 };
f(arr, 9, 5);
}
void f(int arr[], int size, int pos)
{
if (pos < size) {
f(arr, size, pos+1);
cout << pos << ": " << arr[pos] << endl;
}
}
(ii) Provide a fixed recursive version of the following (broken) function:
// gets the user to provide a character in the range specified by parameters A and B
// (repeating as needed until a valid character is provided)
// then returns the valid response
char getCharBetween(char A, char B)
{
cout << "Enter a character in the range " << A << " to " << B << " (inclusive): ";
char userCh;
cin >> userCh;
if (userCh << A) && (userCh > B) {
cerr << "Error: " << userCh " is not between " << A << " and " << B;
cerr << ", please try again" << endl;
getCharBetween(A, B);
}
return userCh;
}
Question 4: null terminated character arrays [10]
Suppose str1 and str2 are both null terminated character arrays passed as parameters to a function. (The array sizes are not passed.)
(i) Without using anything from the string or cstring libraries (i.e. without using strcpy, strcmp, strlen, etc), write a loop that copies the contents of str1 into str2 up to a maximum of 100 chars or the null terminator, whichever comes first.
(ii) Explain which of the common cstring functions (strlen, strcmp, strncmp, strcpy, strncpy, strcat, strncat) your loop most closely resembles and why.
Question 5: Scope and parameters [10](i) implement the adjustBoth function described in the prototype below.
// adjustBoth alters the values of x and y by adding z to each of them void adjustBoth(int &x, int &y, int z);
(ii) Assuming a correct implementation of adjustBoth is given, show (as precisely as possible) the output that would be produced by the following program:
#include <iostream>
using namespace std;
// adjustBoth alters the values of x and y by adding z to each of them
void adjustBoth(int &x, int &y, int z);
void f(int &a, int &b, int &c);
int main()
{
int x = 2;
int y = 5;
int z = 9;
f(x, y, z);
cout << "x is " << x << ", y is " << y << ", z is " << z << endl;
}
void f(int &a, int &b, int &c)
{
if (a > b) {
int c = 100;
cout << "c is " << c << endl;
} else {
int c = -7;
cout << "c is " << c << endl;
}
adjustBoth(b, c, a);
cout << "a is " << a << ", b is " << b << ", c is " << c << endl;
}
Question 6: Sorting and searching [10]
(i) Write the linear search function prototyped below.
// searches from the last position in the array to the beginning (size-1 down to 0), // returning the position where it first encounters the target value // (returns -1 if the target value is never found) int linearsearch(double arr[], int size, double target);
(ii) In your own words, explain how a basic bubblesort works and illustrate the sequence of changes that would take place if we bubblesort an array whose initial contents are 3, 18, -1, 12, 7 (assume we want the values sorted in increasing order).
Question 7: Structs and arrays [10]Study the definition of the AssignMarks struct below then implement the findBest function described in the prototype.
const int MaxAssigns = 50; // the maximum number of assignments allowed in courses
struct AssignMarks {
int numAssigns; // the actual number of assignments actually used in a course
float possibleMark[MaxAssigns]; // the maximum possible mark in each assignment
float classAve[MaxAssigns]; // the class average mark for each assignment
};
// findBest goes through the assignments done for a course,
// finds the assignment for which the class had the best percentage on average
// (i.e. the best class average/possible mark)
// and returns the array position of that assignment
// e.g. if there were 5 assignments with possible marks { 10, 20, 20, 50, 10 }
// and averages { 5, 15, 14, 30, 6 } then the class did the best on the
// second one (15/20), so the function returns that array position (1)
int findBest(AssignMarks a);
Question 8: Pointers and memory allocation [10]
(i) Assuming the fill and print functions have been correctly implemented and that the user enters a valid size when prompted, finish the main routine below so that it correctly: allocates the floatArr, then calls fill and print on the array, then deallocates the array.
// fills the first N positions of the array with user-chosen values
void fill(float* arr, int N);
// displays the contents of the first N positions of the array
void print(float arr[], int N);
int main()
{
float *floatArr;
int size;
cout << "Please enter the desired array size: ";
cin >> size;
// YOUR SEGMENT OF MAIN ROUTINE CODE WOULD GO HERE
}
(ii) Study the struct definition then provide an implementation for the display function prototyped below.
struct SomeData {
int iVal;
float fVal;
};
// displays the iVal and fVal fields of the SomeData items that ptr1 and ptr2 refer to (point at)
void display(SomeData* ptr1, SomeData* ptr2);
That's all, good luck with the rest of your exams and have a good winter break!