- What is the difference between an object and a class?
An object is a data value. A class is a type, and cannot be used
as a data value.
- What is the responsibility of a constructor for a class?
A contructor typically initializes the variables of an object.
- A circle in the plane can be described by its center point and
its radius. Class Circle describes objects that are circles in the
plane, and its definition is partially given below.
Some operations on circles are provided. For example, function
area returns the area of a circle. Write the definition
of function area, as it would occur in file circle.cc. Use
approximation 3.1416 for pi.
class Circle
{
public:
Circle(Point center, double radius);
double area();
...
private:
Point myCenter;
double myRadius;
};
double Circle::area()
{
return 3.1416*myRadius*myRadius;
}
- Write a C++ declaration for an array called Yarn that
holds 25 integers.
int Yarn[25];
- Suppose that array A contains the following.
i A[i]
0 3
1 7
2 8
3 5
Function mystery is shown below. What is in array A after
running mystery(A,4)?
int mystery(int B[], int n)
{
int k;
for(k = 0; k < n; k++) {
B[k] = B[k] + k;
}
}
i A[i]
0 3
1 8
2 10
3 8
- Write a function that computes the sum of all of the numbers
in an array of integers. There should be two parameters, the array A
and the size n of the array. A function heading is provided.
int sum(int A[], int n)
{
int k,s;
s = 0;
for(k = 0; k < n; k++) {
s = s + A[k];
}
return s;
}
- You would like to set variable s to
the sum of all of the integers
in array Fish, which has 12 members.
What statement would you write
to use function sum to do the computation?
s = sum(Fish,12);
- Write a function that has a single null-terminated string
parameter. It should return 1 if the string contains an 'a', and 0
if it has no occurrences of the letter 'a'. Call it anyAs. For
example, anyAs("cat") = 1, but anyAs("dog") = 0.
bool anyAs(char s[])
{
int k;
for(k = 0; s[k] != '\0'; k++) {
if(s[k] == 'a') return 1;
}
return 0;
}
- To within a constant factor (that is, in terms of
proportionality) how much time does it take to sort an array
of n integers using insertion sort, in the worst case?
n2. (You can also say n2/2. I only asked for
the answer to within a constant factor.)
- To within a constant factor, how much time does it take
to sort an array of n integers using quicksort, in the average case?
n log2(n)
- To within a constant factor, how long does it take to do
a linear search of an array of n integers, in the worst case?
n
- To within a constant factor, how long does it take to do
a binary search of a sorted array of n integers, in the worst case?
log2(n)
- With an error of no more than 1, what is log base 2 of 2000?
11 (Just start at 2000 and successively cut your number in half. At
a fractional value, round up to the next integer. Keep going until you
get 1. Count the number of halvings that you did. It should be 11.)
- When passing an array as a parameter to a function, you usually
also pass the logical size of an array.
That is not necessary when the array
is a null-terminated string? Why not?
Because you can determine the length of the string using function strlen.
Comparable functions for other kinds of arrays do not exist.
- What is the difference between the logical and physical
size of an array?
The physical size of an array is the amount of space that is
available to be used. The logical size is the amount of space
that is actually in use at the moment.