logo CodeStepByStep logo

longestSortedSubsequence

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

Write a member function named longestSortedSequence that could be added to the ArrayIntList class. Your function should return the length of the longest sorted sequence within the list of integers. For example, if a variable named list stores the following sequence of values:

{1, 3, 5, 2, 9, 7, -3, 0, 42, 308, 17}

Then the call of list.longestSortedSequence() would return 4 because it is the length of the longest sorted sequence within this list (the sequence -3, 0, 42, 308). If the list is empty, your method should return 0. Notice that for a non-empty list the method will always return a value of at least 1 because any individual element is a sorted sequence.

Constraints: You may call other member functions of the ArrayIntList if you like, but your function should not modify the state of the list. Its header should be declared in such a way as to indicate to the client that this function does not modify the list. Do not use any auxiliary data structures to solve this problem (no array, vector, stack, queue, string, etc).

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 {
private:
    int* elements;   // array storing element data
    int mysize;      // number of elements in the array
    int capacity;    // array's length
    
public: 
    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.