Solved–Homework 2– Solution

$30.00 $19.00

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…

You’ll get a: . zip file solution

 

 
Categorys:
Tags:

Description

5/5 – (2 votes)

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:

  1. 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

  1. Write a functor to determine if an integer is even or odd.

  1. 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

  1. 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( );

}

  1. For each of the following code fragments, determine the worst case running time using Big-Oh notation as a function of n.

    1. int sum = 0;

for (int k = n; k>=1; k = (int) k/3) sum += k;

cout << sum;

2

  1. for (int j = 1; j <= n; j++)

{

for (int i = j; i > 1; i/=9)

cout << “(” << i << “, ” << j << “) ” ; cout << endl;

}

  1. 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;

}

  1. for( i = 0; i < n; ++i) for (j = 0; j < n; ++j)

a[i][j] = b[i][j] + c[i][j];

  1. 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

  1. 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 );

}

  1. What is the di erence between delete [] and delete?

  1. For programming problem 1, Using big-Oh notation if size = n, give the worst case running time for the:

    1. copy constructor

    1. move constructor

  1. Using big-Oh notation what is the running time of the following code snippets where v is a vector of n > 0 integers.

    1. 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 );

}

  1. 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.

      1. f(string(“hello”));

      1. string s1 = “hello”; f(s1);

      1. string s2 = “hello”; f( std::move(s2) );

      1. const string s3 = “hello”; f(s3);

  1. 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

  1. 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