logo CodeStepByStep logo


Language/Type: C++ array lists arrays
Author: Marty Stepp (on 2016/06/16)

Write a member function named stretch that could be added to the ArrayIntList class. Your function accepts an integer k as a parameter and that replaces each integer in the original list with k copies of that integer. For example, if an ArrayIntList variable named list stores this sequence of values:

{18, 7, 4, 4, 24, 11}

And the client makes the following call of list.stretch(3); , the list should be modified to store the following values. Notice that there are three copies of each value from the original list because 3 was passed as the parameter value.

{18, 18, 18, 7, 7, 7, 4, 4, 4, 4, 4, 4, 24, 24, 24, 11, 11, 11}

If the value of k is less than or equal to 0, the list should be empty after the call.

Remember that an array list has an internal "unfilled" array whose capacity might be larger than its size. Note that this member function might require more capacity than your list's array currently has. If so, you must handle this by resizing to a larger array if necessary. You should not create any auxiliary arrays unless it is absolutely necessary to do so to solve the problem. If the list's existing internal array already has enough capacity, you should perform the modification in place without using any auxiliary data structures.

Constraints: Do not call any member functions of the ArrayList. For example, do not call add, insert, remove, or size. You may, of course, refer to the private member variables inside the list. Do not make assumptions about how many elements are in the list or about the list's capacity. Do not use any auxiliary data structures to solve this problem (no vector, stack, queue, string, etc). Do not leak memory; if you cease using any dynamically allocated (via new) memory, free it. Your code must run in no worse than O(N) time, where N is the length of the list.

Write the member function as it would appear in ArrayIntList.cpp. You do not need to declare the function header that would appear in ArrayIntList.h. Assume that you are adding this method to the ArrayIntList class as defined below:

class ArrayIntList {
    int* elements;   // array storing element data
    int mysize;      // number of elements in the array
    int capacity;    // array's length
    your code goes here;
Type your solution here:

This is a member function problem. Submit a member function that will become part of an existing C++ class. You do not need to write the complete class, just the member function described in the problem.

You must log in before you can solve this problem.

Log In

If you do not understand how to solve a problem or why your solution doesn't work, please contact your TA or instructor.
If something seems wrong with the site (errors, slow performance, incorrect problems/tests, etc.), please

Is there a problem? Contact a site administrator.

© Marty Stepp, all rights reserved.