$24
Assignment 2 includes a programming portion and a written portion. The programming portion must compile and should consist of a single le (hw02.cpp). The written portion should consist of a single le (hw02written) in a .pdf format. Be sure to include your name at the beginning of each le! You must hand in both les via NYU Classes. No late assignments will be accepted.
Programming Part:
For the class called IntArray implement a deep copy constructor, a move constructor, a destructor, and a move assignment operators. Use the std::swap function if appropriate, and below where you used the std::swap function, write as a comment the code to perform the same steps without using the std::swap function.
Implement the methods outside the class interface.
You may implement the put and get methods to test your code. You will not be graded on these methods.
Nicolai, our TA, has created a unit test for this question.1 Your code must pass this unit test. The unit test is in a le called main.cc.
class IntArray{
public:
IntArray(int n = 0): size(n), array(new int[n]){} // add your methods here
void put (int i, int value);
int get( int i);
private:
int * array;
int size;
};
A bonus of %5 percent will be given if you turn in this homework assignment by Friday September 23th at 11:00 p.m.
1We will introduce unit testing next week.
1
Write a functor to determine if an integer is even or odd.
Write a functor that compares two strings, str1 and str2. If str1’s length is less than str2’s length, your functor returns true. Otherwise it returns false.
Written Part
What is printed if you ran the following code. Would this be what you wanted to happen? Is there anything that goes wrong?
class IntArray{ public:
IntArray(int n = 0): size(n), array(new int[n]){} void put (int i, int value); int get( int i);
private:
int * array;
int size;
};
void someFun( )
{
IntArray myA1( 1 );
IntArray myA2( myA1 );
IntArray myA3( 1 );
myA1.put( 0, 12 );
cout << myA1.get( 0 ) << endl;
myA2.put( 0, 10 );
cout << myA1.get( 0 ) << endl;
myA3.put( 0, 5 );
myA1 = myA3;
cout << myA1.get( 0 ) << endl;
}
int main(int argc, const char * argv[])
{
someFun( );
}
For each of the following code fragments, determine the worst case running time using Big-Oh notation as a function of n.
int sum = 0;
for (int k = n; k=1; k = (int) k/3) sum += k;
cout << sum;
2
for (int j = 1; j <= n; j++)
{
for (int i = j; i 1; i/=9)
cout << "(" << i << ", " << j << ") " ; cout << endl;
}
template <class Comparable
int min(const vector<Comparable & items)
{
if (items.size() == 0) return -1;
int min_index = 0;
for (int i = 0; i < items.size(); ++i)
if (items[i] < items[min_index])
min_index = i;
return min_index;
}
for( i = 0; i < n; ++i) for (j = 0; j < n; ++j)
a[i][j] = b[i][j] + c[i][j];
void in_order( vector<int & a )
{
int j = 0;
for( int p = 1; p < a.size( ); p++ )
{
print(a); // This function takes O(n) time.
Comparable tmp = a[ p ];
for( j = p; j 0 && tmp < a[ j - 1 ]; j-- )
a[ j ] = a[ j - 1 ];
a[ j ] = tmp;
}
}
3
void f( vector<int v )
{
if ( v.size( ) 0 ) v[0] = 1;
}
int main( )
{
some code to create a vector of size n that you do not include in your run time
f( v );
}
What is the di erence between delete [] and delete?
For programming problem 1, Using big-Oh notation if size = n, give the worst case running time for the:
copy constructor
move constructor
Using big-Oh notation what is the running time of the following code snippets where v is a vector of n 0 integers.
void f( vector<int b )
{
cout << b[0];
}
int main( )
{
code to create the vector v which will not be included in your run time f( v );
}
void f( vector<int & b )
{
cout << b[0];
}
int main( )
{
code to create the vector v which will not be included in your run time f( v );
}
4
6. If your code contained the following functions2
// Function 1
void f( const string & s )
{
cout << "Function 1 was called." << endl;
}
// Function 2
void f( string & s )
{
cout << "Function 2 was called." << endl;
}
// Function 3
void f( string && s )
{
cout << "Function 3 was called." << endl;
}
State which function was called for each of the following.
f(string("hello"));
string s1 = "hello"; f(s1);
string s2 = "hello"; f( std::move(s2) );
const string s3 = "hello"; f(s3);
Determine if the speci ed expression is an rvalue or an lvalue:
int x = 3; // what is x
char * ptr = new char[3]; // what is new char[3]
int f( ) return 1; // What is the return value of the function f
string && sRef = string("What?"); // what is sRef
Please run these functions to check your answer. Do not turn in your code.
5
Anything wrong with this code? If there is, please describe what the problem is, otherwise state there is no problem.
string & getName( )
{
string name;
cout << "Please enter your name: "; cin name;
cout << "Hi " << name << ’!’ << endl; return name;
}
int main( )
{
cout << getName( );
}
6