you can do it without sorting, but you have to tell the method where to insert the element...here's a dirty example (didn't really test it and there's no error checking):
Code:
#include <iostream>
using std::cout;
using std::endl;
class IntArray {
private:
int m_array[100];
int m_size;
public:
IntArray (const int*, const int);
void insert (int, int);
void print ();
};
IntArray::IntArray (const int* array, const int size)
{
for (int i = 0; i < size; ++i)
m_array[i] = array[i];
m_size = size;
}
void IntArray::insert (int position, int number)
{
for (int i = m_size; i > position; --i)
m_array[i] = m_array[i-1];
m_array[position] = number;
m_size++;
}
void IntArray::print ()
{
for (int i = 0; i < m_size; ++i)
cout << m_array[i] << ' ' ;
cout << endl;
}
int main ()
{
int array[] = { 1, 2,3, 5, 6, 7, 8, 9, 10 } ;
int size = sizeof(array)/sizeof(int);
IntArray ArrayObject (array, size);
ArrayObject.print();
ArrayObject.insert (3, 4); // insert number 4 at position 3
ArrayObject.print ();
return 0;
}
Sorry there's no comments, but that's what you get for free.. This should help you figure out deletion too...
also notice that my class holds a large "statically" allocated array. it would be a more useful class if it contained a dynamically allocated array, which could grow and shrink accordingly (only when necessary)...i'll let you handle that....