Appending a dynamic array

Create a dynamic array of int with a initial space of 4. Write a function ‘append’ that appends a given value to this array. At any stage, if this function finds the array full it automatically doubles the size of array to accommodate this new value. Also write a function to display all the elements of this array. Write a main to test all these functions.

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
 
#include<iostream>
using namespace std;


int main()
{
int p, n = 4, val;
int *array = new int [n];
for (int i=0; i<4; i++)
cin>>array[i];
while(1)
{
cout<<"Enter 0 to end and 1 to continue";
cin>>p;
while(1)
{
cin>>val;
append(array,val,n);
}

}
return 0;
}



I do not know how to proceed further. Please give me a hint or tell me how shall I do this?
Last edited on
Well you need to make a class out of it.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class rabble {
    int *array;
    size_t maxsize;
    size_t usedsize;
    void expand() {
    } 
public:
    rabble() {
        array = new int[4];
        maxsize = 4;
        usedsize = 0;
    }
    void append(int v) {
    }
    void print() {
    }
};
"Class" has not been covered in the college classes. I need to do it without using "Class". How to do it without using the concept of "Class"
You don't really need a class. Just use globals for array, maxsize, and usedsize.

Remember that array may be reallocated, so you need to pass its address to the function, either as a pointer or by reference:

void append( int* *array, int *maxsize, int *usedsize, int value );

void append( int* &array, int &maxsize, int &usedsize, int value );

[edit]
or just refer to the global values in the function directly:

void append( int value )

I don't know if that'll lose you points, though. It would in my class.
Last edited on
1
2
3
4
5
6
7
8
9
def append(value, array, size, capacity):
	if size == capacity: //is full
		capacity *= 2
		aux = new Type[capacity] //allocate a new region, duplicate the capacity
		copy_n(array, size, aux) //copy the vales
		delete [] array //deallocates old region
		array = aux //point to new location
	
	//add the value 


> Just use globals for array, maxsize, and usedsize.
> I don't know if that'll lose you points, though. It would in my class.
¿so why are you suggesting them?
I suggested not referring to them directly.

As far as using globals, for this learning purpose, it appears the professor expects the students to learn to do stuff, and will later package it properly. One concept at a time.

Compare:

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


int* data;
int  size;
int  capacity;

void resize( int new_size )
{
  if (new_size + 1 > capacity)
  {
    int* new_data = data
      ? realloc( data, new_size + 1 )
      : malloc( new_size + 1 );
    if (new_data)
    {
      capacity = new_size + 1;
      size     = new_size;
      data     = new_data;
    }
  }
  else
  {
    size = new_size;
  }
}
struct vector
{
  int* data;
  int  size;
  int  capacity;

  void resize( int new_size )
  {
    if (new_size + 1 > capacity)
    {
      int* new_data = data
        ? realloc( data, new_size + 1 )
        : malloc( new_size + 1 );
      if (new_data)
      {
        capacity = new_size + 1;
        size     = new_size;
        data     = new_data;
      }
    }
    else
    {
      size = new_size;
    }
  }
};

and then you need to work with two vectors and the global code crumbles

> I suggested not referring to them directly.
¿so you mean to pass them to this function? void append( int* &array, int &maxsize, int &usedsize, int value );
¿what's the point on making them global then?


by the way, `malloc()' and `realloc()' allocates n bytes, so malloc( new_size + 1 ); will may not give you enough for another integer.
Topic archived. No new replies allowed.