NYU POLY CS2134 HW 1a

download NYU POLY CS2134 HW 1a

of 5

Transcript of NYU POLY CS2134 HW 1a

  • 7/21/2019 NYU POLY CS2134 HW 1a

    1/5

    CS2134 Homework 1AFall 2014

    Due 11:59 p.m. Monday, September 8, 2014

    September 2, 2014

    Your rst assignment includes a programming portion and a written portion. The programmingportion should consist of a single le called hw01.cpp, and the written portion should consist of a

    single le called hw01written in a standard format (.txt, .doc, .htm., or .pdf). Be sure to includeyour name at the beginning of each le! You must hand in both les via MyPoly.

    Programming Part:

    1. (a) Write a function (or 3 seperate functions) to return the minimum item in a vectorcontaining:

    i. charsii. ints

    iii. strings(b) Write a function (or 3 seperate functions) to return the maximum item in a vector

    containing:i. chars

    ii. intsiii. strings

    2. Using the linked list class

    class Node{

    public: // These member variables are made public to simplify your coding.// Of course these member variables should be private!

    chNode( char ch, Node * ptr = nullptr):data(ch),next(ptr){}

    char data;Node * next;

    };

    1

  • 7/21/2019 NYU POLY CS2134 HW 1a

    2/5

    and a pointer to the class Node * nodePtr; write the code to perform the following:

    create a linked list of three nodes containing the items B, D, E . add a node containing A to the front of your linked list print out the memory locations of A, B, C D, E and nodePtr (and hand in this

    information with the written part). delete the node containing A , (nodePtr now points the the nodes containing B, C

    D, E )

    3. For an array, int * intPtr = new int[5]; , using only pointers (no array indexing) writethe code to perform the following:

    insert items 2, 3,4, 5 into the rst three positions (positions 0,1,2,3). add item 1 to the front of your array; (you need to move the other items) Print out the memory locations of 1, 2, 3, 4, 5 and intPtr (and hand in this information

    with the written part). delete the array pointed to by intPtr

    2

  • 7/21/2019 NYU POLY CS2134 HW 1a

    3/5

    Written Part:

    1. Write each of the following functions in Big-Oh notation:

    (a) T (n ) = 12 n log(n ) + 10 n 0 . 5 log3(n ) + 12

    (b) T (n ) = n log(n ) + n log2(n ) + 12 n

    (c) T (n ) = 22 log(n 12 ) + 12 log(4 n )

    (d) T (n ) = 1000 n log5(n ) + 0 .002n 3 + 8 n 2

    (e) T (n ) = (4 n 2 + 4 n ) 11n

    (f) T (n ) = n log(n 12 ) + 8 n 2

    (g) T (n ) = log( n )(12 nlog( n )+ n 2 )n

    (h) T (n ) = n2 (This means n choose 2)

    (i) T (n ) = n3 (This means n choose 3)

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

    (a) int sum = 0;for (int i = 0; i < n; i++)

    sum += i;for (int j = 0; j < n; j++)

    sum += j;for (int k = 0; k < n; k++)

    sum += k;

    (b) int sum = 0;for (int i = 0; i < n; i++){

    sum += i;

    for (int j = 0; j < n; j++)sum += j;

    }

    3

  • 7/21/2019 NYU POLY CS2134 HW 1a

    4/5

    (c) int sum = 0;for (int i = 0; i < n; i++){

    sum += i;

    for (int j = 0; j < n; j++){sum += j;for (int k = 0; k < n; k++)

    sum += k;}

    }

    (d) int sum = 0;for (int i = 0; i < n; i*=2){

    sum += i;}

    (e) int sum = 0;for (int i = 0; i < n; i*=2){

    sum += i;for (int j = 0; j < n; j++){

    sum += j;}

    }

    (f) int sum = 0;

    while (n>0){

    cout

  • 7/21/2019 NYU POLY CS2134 HW 1a

    5/5

    4. Arrange the following in increasing order: O(n 2 log(n )), O (2n ), O (n 3), O (n (log (n ))2), O (n ),O (n 0 . 5 log(n )), O(n log(n )), O(n 2), O(log2(n )). Indicate if any two have the same rate.

    5. Suppose you had a very complicated code that was difficult to analyze. To get a quick ideaof your algorithms running time you ran your program on different sized inputs. Supposethe following are the timing results for your algorithm. Using the timing results below,indicate the most likely running time in big-Oh notation; choose one from the following list.O (1) , O (n ), O (n 2), O (n 3), O (n 4).

    n time

    2^7 0.0020942^8 0.008342^9 0.0334122^10 0.133054

    2^11 0.5325012^12 2.12835

    6. Using the denition of Big-Oh , show that 3 n + 17log( n ) + 5 = O (n ).

    5