changing an array class to dynamic array class

everything i have read says this should be easy and that you just add
1
2
3
  typedef double* DoublePtr;
  DoublePtr p;
  p = new double [10]


but where do i add this code?


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
85
86
87
88
89
90
91
92
93
94
95
96
#include<iostream>
using namespace std;
const int MAX_SIZE = 50;

class ListDynamic
{
	
    public:
        
        ListDynamic();
        bool full();
        int getSize();
        void addValue(double value);
        double getValue(int index);
        double getLast();
        void deleteLast();
        friend ostream& operator <<(ostream& out, const ListDynamic& thisList);  
    
    private:
        double listValues[MAX_SIZE];
        int size;
};
int main()
{
    double value;
    ListDynamic l;
    cout << "size of List " << l.getSize() << endl;
     
     
     
    cout << "New size of List " << l.getSize() << endl;
    cout << "First Value: " << l.getValue(0) << endl;
    cout << "Last Value: " << l.getLast() << endl;
    cout << "deleting last value from list" << endl;
    l.deleteLast();
    cout << "new list size "  << l.getSize() << endl;
    cout << "the list now contains: " << endl << l << endl;
    system("pause");
    return 0;
}

ListDynamic::ListDynamic()
{
    size = 0;
}

bool ListDynamic::full()
{
    return (size == MAX_SIZE);
}

int ListDynamic::getSize()
{
    return size;
}

void ListDynamic::addValue(double value)
{
    if (size < MAX_SIZE)
    {
        listValues[size] = value;
        size++;
    }
    else
        cout << "\n\n*** Error in ListDynamic Class: Attempting to add value past max limit.";
}

double ListDynamic::getValue(int index)
{
    if (index < size)
        return listValues[index];
    else
        cout << "\n\n*** Error in ListDynamic Class: Attempting to retrieve value past current size.";
}
double ListDynamic::getLast()
{
    if (size > 0)
        return getValue(size - 1);
    else
        cout << "\n\n*** Error in ListDynamic Class: Call to getLast in Empty List.";
}

void ListDynamic::deleteLast()
{
    if (size > 0)
        size--;
    else
        cout << "\n\n*** Error in ListDynamic Class: Call to deleteLast in Empty List.";
}
ostream& operator <<(ostream& out, const ListDynamic& thisList)
{
    for (int i = 0; i < thisList.size; i++)
        out << thisList.listValues[i] << endl;
    return out;
}  
anyone? I could really use some help with this?
Does it have to be a dynamic array? Could you use std::vector instead?
Thank you for responding, Sadly it has to be a dynamic array, I am a little closer (I think) I have the dynamic array declared in my program but I cant use it. I tried manipulating the dynamic array with .notation but it didn't work. Is something wrong with my pointer, or is my syntax just wrong?




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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include<iostream>
using namespace std;
const int MAX_SIZE = 50;
typedef double* DoublePtr;
  
class ListDynamic
{
	
    public:
        
        ListDynamic();
        bool full();
        int getSize();
        void addValue(double value);
        double getValue(int index);
        double getLast();
        void deleteLast();
        friend ostream& operator <<(ostream& out, const ListDynamic& thisList);  
    
    private:
        double* listValues;
        int size;
};
int main()
{
    double value;
    DoublePtr p;
    p = new double [50];
    ListDynamic l;
    cout << "size of List " << l.getSize() << endl;
    cout << "enter value to add to list: ";
    cin >> value;
    l.addValue(value);
    cout << "enter value to add to list: ";
    cin >> value;
    l.addValue(value);
    cout << "enter value to add to list: ";
    cin >> value;
    l.addValue(value);
    cout << "enter value to add to list: ";
    cin >> value;
    l.addValue(value);
    cout << "enter value to add to list: ";
    cin >> value;
    l.addValue(value);
    cout << "New size of List " << l.getSize() << endl;
    cout << "First Value: " << l.getValue(0) << endl;
    cout << "Last Value: " << l.getLast() << endl;
    cout << "deleting last value from list" << endl;
    l.deleteLast();
    cout << "new list size "  << l.getSize() << endl;
    cout << "the list now contains: " << endl << l << endl;
    delete [] p;
    system("pause");
    return 0;
}

ListDynamic::ListDynamic()
{
    size = 0;
}

bool ListDynamic::full()
{
    return (size == MAX_SIZE);
}

int ListDynamic::getSize()
{
    return size;
}

void ListDynamic::addValue(double value)
{
    if (full())
    {
        double* temp = new double[size];
        std::copy(listValues, listValues + size, temp);

        delete[] listValues;
        listValues = new double[size + 1];

        std::copy(temp, temp + size, listValues);
        listValues[size] = value;
        delete[] temp;
    } else
    {
        listValues[size++] = value;
    }
}
double ListDynamic::getValue(int index)
{
    if (index < size)
        return listValues[index];
    else
        cout << "\n\n*** Error in List Class: Attempting to retrieve value past current size.";
}
double ListDynamic::getLast()
{
    if (size > 0)
        return getValue(size - 1);
    else
        cout << "\n\n*** Error in List Class: Call to getLast in Empty List.";
}

void ListDynamic::deleteLast()
{
    if (size > 0)
        size--;
    else
        cout << "\n\n*** Error in List Class: Call to deleteLast in Empty List.";
}
ostream& operator <<(ostream& out, const ListDynamic& thisList)
{
    for (int i = 0; i < thisList.size; i++)
        out << thisList.listValues[i] << endl;
    return out;
}  


Last edited on
It would be helpful if you copied and pasted the exact compilation errors you are getting.
Im not getting any errors from this code it compiles and runs just fine. my problem is how do i use my pointer variable p which is supposed to point to my array object l. Right now the pointer is not being used and the class object is doing everything.

Let me get this straight: you want to go from a modular design to a brittle design? Why?
Don't you get a segfault?

Hint: listValues needs to point to something.
that was the assignment take the list class and make it a dynamic list class that uses a dynamic array instead an array of max size 50
You misunderstand the assignment.
We already did Project 11 in Homework 9. For this assignment, re-write that project using dynamic allocation. Call the updated class ListDynamic. This new class can always accept more values by dynamically allocating a new internal array when the current internal array is full.

What do you think its asking then?
Last edited on
Topic archived. No new replies allowed.