Test 1 Study Guide
Tests in this course may contain any material covered in lecture up until this point, although much stronger emphasis will be placed on material for which you have received graded feedback. This page contains a collection of exercises that you may wish to complete to prepare yourself for the test. Note that you do not need to complete all of the offered exercises. More questions appear here than will appear on your test.
Programming in C++
-
Write a function
arraySumthat takes threeint*arrays and anintlength. All three arrays are assumed to be the same length. For each index in the arrays, the sum of the first and second value is stored in the third.void arraySum(int* first, int* second, int* third, int length) { for (int i=0;i<length;i++) { third[i] = first[i] + second[i]; } } -
Write a function
averagewhich computes the average of the values in an array offloat`s. You must take a `float*parameter; you may take other parameters as you see fit. Your function should return the average of the values in that array.float average(float* data, int count) { float total = 0; for (int i=0;i<count;i++) { total += data[i]; } return total/count; } -
Write a function
fibonacciwhich takes a parameterint nand returns anint*to a new array which contains the firstnFibonacci numbers (0, 1, 1, 2, 3, 5, 8, 13, …).int* fibonacci(int n) { int* result = new int[n]; for (int i=0;i<n;i++) { if (i<2) { result[i] = i; } else { result[i] = result[i-1] + result[i-2] } } return result; } -
Write a function
zerowashwhich allocates space for a millionintvalues, sets them all to0, and then deallocates the memory.void zerowash() { int* space = new int[1000000]; for (int i=0;i<1000000;i++) { space[i] = 0; } delete[] space; } -
Explain the difference between
deleteanddelete[].deleteis used to delete objects;delete[]is used to delete arrays.
Debugging C++
Each of the following code fragments contains a bug. Identify the bug and explain how to correct it.
-
Code to create an array of ten elements and set each element equal to its index.
int* array = new int[10]; for (int i=1;i<=10;i++) { array[i] = i; }Arrays in C++ start indexing at 0. The loop should read
for (int i=0;i<10;i++). -
Code to define a function which sums all of the values in an array.
int sum(int* array) { int acc = 0; for (int i=0;i<len(array);i++) { acc += array[i]; } return acc; }There is no
lenfunction in C++. The functionsummust be modified to take an additional parameter which contains the length of the array. -
Code to define a class which represents a single (X,Y) coordinate.
class Point { public: Point(int x, int y); int getX(); int getY(); private: int x; int y; }; Point::Point(int x, int y) { x = x; y = y; } int Point::getX() { return x; } int Point::getY() { return y; }The body of the constructor will not do anything. It should read
this->x = x; this->y = y;For consistency, the
getXandgetYmethods should also be usingthis→. -
Code to calculate the distance between two points.
Point* p; Point* q; float dx = p->getX() - q->getX(); if (dx<0) { dx = -dx; } float dy = p->getY() - q->getY(); if (dy<0) { dy = -dy; } float dist = sqrt(dx*dx+dy*dy); // assume sqrt exists (in the cmath library) cout << "The points are " << dist << " unit(s) apart." << endl;The pointers
pandqare never initialized; they should be assigned pointers tonewobjects.Ideally, this distance-calculating code would be a method rather than code written in e.g.
main. That’s a stylistic concern, though, while the uninitializedpandqvariables can lead the program to crash.
Object-Oriented Programming
-
What is the purpose of a class? What is the difference between a class and an object?
A class groups data and behavior (fields and methods) together. We use classes to describe concepts relevant to the program we are writing so that our code can recognize and discuss those concepts rather than focus on tiny details. An object is an instance of a class: it is an example of the sort of thing that the class describes.
-
Define polymorphism as it pertains to C++. Give an example which was not given in lecture.
Polymorphism allows an object to be treated as an instance of its superclass. For instance, a pointer to an
Employeeobject might be treated as a pointer to aPersonobject (since we would expectEmployeeto be a subclass ofPerson). -
What is the purpose of the
privatekeyword?The
privatekeyword designates fields and methods which cannot be called from outside of the definition of a class’s methods. We know that outsiders cannot change the value ofprivatefields (unless apublicmethod allows them to do so), allowing us to reason about what values they may contain. -
Draw a stack diagram of the following code when it reaches the end of
main. Be sure to be clear what is on the stack and what is on the heap.class LabAssignment { public: LabAssignment(int number, int score); int number; int score; } LabAssignment::LabAssignment(int number, int score) { this->number = number; this->score = score; } int main() { LabAssignment* a = new LabAssignment(2,84); LabAssignment* b = a; a = new LabAssignment(3,95); b->score = 92; return 0; }