can someone help me fix this code?

...
Last edited on
What seems to be the problem with implementing FindKth, delete and the replace function.

Your code already seems very well written and commented already. Have you tried to code it yet, even written any psuedo-code?
i am not sure how to put the replace function in there and also not sure how to count number of replacements
Where is your original code?

Oh wait! I have it right here!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// Contructor sets valriables to indicate that we have an empty list.
ArrayList::ArrayList() {

    ArraySize = 0;
    nelts = 0;
    array = NULL;
}
// Destructor used to clean up allocated resources/
ArrayList::~ArrayList() {

    delete [] array;
}
// Delete all the elements from the list.
void ArrayList::MakeEmpty( )
{
    delete [] array;
    nelts = 0;
    ArraySize = 0;
    array = NULL;
}
// Tests to see if there are no elements in the array.
bool ArrayList::isEmpty( )
{
    return nelts == 0;
}
// Return the index of the first occurrance of a specified list element.  Returns -1 if not found.
int ArrayList::Find( double y )
{
    for( int i = 0; i < nelts; i++ ) {
        if( array[i] == y ) {

            // The user is considering the first element to be at position 1.
            return i +1;
        }
    }
    return -1;
}
// Insert the specied number in the list position.
void ArrayList::Insert( double y, int position )
{
    // Abort the program if a bad position is specified.
    assert( position >= 1 && position <= nelts + 1 );

    // Note the array position.  Recall that lists are numbered starting with 1.
    int ia = position - 1;

    // Make sure there is space in the array for a new element.
    MakeSpace( );

    // If we are adding a new element to the list, open up space if necessary.
    if( ia < nelts ) {
        memmove( &array[ia + 1], &array[ia], (nelts - ia) * sizeof(double ) );
    }
    array[ia] = y;
    nelts++;
}
// Display the elements of the list.
void ArrayList::PrintList( )
{
    // Display the list.
    for( int i = 0; i < nelts; i++ ) {
        cout << i + 1<< ". " << array[i] << endl;
    }
}
void ArrayList::MakeSpace( )
{
    // If there is space for another array element, return.
    if( nelts < ArraySize ) return;

    // Create storage to make a larger array.
    double *tmp;
    tmp = new double[ArraySize + ArrayIncrement];

    // Note that exceptions now occur when new fails unless you tell the compiler otherwise.
    assert( tmp != NULL );

    // If the previous array was not empty, must copy its contents.
    if( nelts > 0 ) {
        memcpy( tmp, array, nelts * sizeof(double) );
        delete [] array;
    }
    ArraySize += ArrayIncrement;
    array = tmp;
}


It was on this site.

http://phobos.ramapo.edu/~vmiller/handouts/DataStructures/listImp/array.cpp

Funny enough it had the exact same comments as the ones you had "written".

Now what aren't you sure about.
Topic archived. No new replies allowed.