dynamic array

Is it possible to change this array program to a dynamic array or do I need to take a different approach?

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
#include<iostream>
using namespace std;
const int MAX_SIZE = 50;

class List
{
	
    public:
        List();
        bool full();
        int getSize();
        void addValue(double value);
        double getValue(int index);
        double getLast();
        void deleteLast();
        friend ostream& operator <<(ostream& out, const List& thisList);  
    
    private:
        double listValues[MAX_SIZE];
        int size;
};
int main()
{
    double value;
    List 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 << "the list contains: " << endl << l << endl;
     
    system("pause");
    return 0;
}

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

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

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

void List::addValue(double value)
{
    if (size < MAX_SIZE)
    {
        listValues[size] = value;
        size++;
    }
    else
        cout << "\n\n*** Error in List Class: Attempting to add value past max limit.";
}
double List::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 List::getLast()
{
    if (size > 0)
        return getValue(size - 1);
    else
        cout << "\n\n*** Error in List Class: Call to getLast in Empty List.";
}

void List::deleteLast()
{
    if (size > 0)
        size--;
    else
        cout << "\n\n*** Error in List Class: Call to deleteLast in Empty List.";
}
ostream& operator <<(ostream& out, const List& thisList)
{
    for (int i = 0; i < thisList.size; i++)
        out << thisList.listValues[i] << endl;
    return out;
}  
Last edited on
closed account (1v5E3TCk)
I have coded something like that before and here is the code. I hope it helps:

#README.txt:

This is a kind of vector. Which just works with integers. :D

How to use:

-first include classOfint.h to your project.
-then add using namespace cof; to top of your code.
-create an object: classOfint object( size ); //if you dont give a size to your vector it will make it wwith 32 elements


Methods of classOfint:

push_back( int number ); //give a number to push back.
//I cant believe it runs but it runs :D
push_front( int number ); //same with push_back. just push number to front

size(); //gives the size of the vector

iset( int n_index, int number); //first give the number of the element and than give the number to define it

get( int n_index ); //give index number to get value of it


main.cpp:
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
#include <iostream>
#include "classOfint.h"

using namespace std;
using namespace cof;

int main()
{
    int size_of_cof;

    cout<< "input the size of cof";

    cin>> size_of_cof;

    classOfint a(size_of_cof);

    for( int i = 0; i<a.size(); ++i)
    {
        int number;

        cout<<"Enter:";
        cin>> number;

        a.iset( i, number );
    }

    a.push_back(6);
    a.push_front(0);

    int element;
    cout<<"element:";
    cin>> element;

    cout<< a.get( element ) << endl;

    return 0;
}


classOfint.h:
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
#ifndef CLASSOFINT_H_INCLUDED
#define CLASSOFINT_H_INCLUDED

namespace cof
{
    class classOfint
    {

        int* arrayOfint;
        int _size_of_array;

    public:

        classOfint();
        classOfint( int n_size );
        classOfint( classOfint& right );
        int get( int n_index );
        void iset( int n_index, int number );
        void push_back( int number );
        void push_front( int number );
        void operator= ( classOfint& right );
        int size();
    };
}
#endif // CLASSOFINT_H_INCLUDED 


classOfint.cpp:
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
#include "classOfint.h"

namespace cof
{
    classOfint::classOfint()
    {
        arrayOfint = new int[32];
        _size_of_array = 32;
    }

    classOfint::classOfint( int n_size )
    {
        if( n_size < 0 )
        {
            n_size *= -1;
        }

        arrayOfint = new int[ n_size ];
        _size_of_array = n_size;
    }

    classOfint::classOfint( classOfint& right )
    {
        _size_of_array = right.size();

        arrayOfint = new int [ _size_of_array ];

        for( int i = 0; i<_size_of_array; ++i)
        {
            arrayOfint[ i ] = right.get(i);
        }
    }

    int classOfint::get( int n_index)
    {
        if( n_index < 0 )
        {
            n_index *= -1;
        }
        return arrayOfint[ n_index ];
    }

    void classOfint::iset( int n_index, int number )
    {
        arrayOfint[ n_index ] = number;
    }

    void classOfint::push_back( int number )
    {
        int temp[_size_of_array];

        for( int i = 0; i<_size_of_array; ++i )
        {
            temp[ i ] = arrayOfint[ i ];
        }

        delete[] arrayOfint;
        arrayOfint = new int[ _size_of_array ];

        for( int i = 0; i<_size_of_array; ++i)
        {
            arrayOfint[ i ] = temp[ i ];
        }

        arrayOfint[ _size_of_array ] = number;

        ++_size_of_array;

    }

    void classOfint::push_front( int number )
    {
        int temp[_size_of_array];

        for( int i = 0; i<_size_of_array; ++i )
        {
            temp[ i ] = arrayOfint[ i ];
        }

        delete[] arrayOfint;
        arrayOfint = new int[_size_of_array];

        for(int i = 1, j = 0; i<=_size_of_array; ++i, ++j)
        {
            arrayOfint[ i ] = temp[ j ];
        }

        ++_size_of_array;

        arrayOfint[ 0 ] = number;
    }

    int classOfint::size()
    {
        return _size_of_array;
    }

    void classOfint::operator=( classOfint& right )
    {
        delete[] arrayOfint;

        _size_of_array = right.size();

        arrayOfint = new int[ _size_of_array ];

        for( int i = 0; i<_size_of_array; ++i)
        {
            arrayOfint[ i ] = right.get(i);
        }

    }

}





Note: You cant change the size of it but when you push back integers it resizes itself
Topic archived. No new replies allowed.