Skip to content

Instantly share code, notes, and snippets.

@johnhmj
Created January 13, 2010 02:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnhmj/275863 to your computer and use it in GitHub Desktop.
Save johnhmj/275863 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <cstdlib>
#include <cstring>
// Integrated Development Environment
// Visual C++
using namespace std;
//
class CArr
{
public:
CArr(size_t size);
~CArr(void);
void setValue(size_t pos, int val);
void Sort(void);
void Print(void);
private:
int* m_pointer;
size_t m_size;
};
void main(int argc, char** argv)
{
size_t len = 0;
cout<<"Input size of Array: ", cin>>len;
CArr arr(len);
for (size_t i = 0; i < len; i ++)
{
int a = 0;
cout<<"Input value: ", cin>>a;
arr.setValue( i, a);
}
arr.Print();
arr.Sort();
arr.Print();
system("PAUSE");
}
CArr::CArr(size_t size)
{
this->m_size = size;
if ( this->m_size == 0 )
{
this->m_pointer = NULL;
return;
}
this->m_pointer = new int[this->m_size];
memset( (void*)this->m_pointer, 0, this->m_size);
}
CArr::~CArr(void)
{
if ( this->m_pointer != NULL )
{
delete this->m_pointer;
}
}
void CArr::setValue(size_t pos, int val)
{
if ( pos < this->m_size )
{
this->m_pointer[pos] = val;
}
}
void CArr::Sort(void)
{
if ( this->m_pointer == NULL )
{
return;
}
for (size_t i = 0; i < this->m_size; i ++)
{
for (size_t j = 0; j < this->m_size; j ++)
{
if ( this->m_pointer[i] < this->m_pointer[j] )
{
int a = this->m_pointer[i];
this->m_pointer[i] = this->m_pointer[j];
this->m_pointer[j] = a;
}
}
}
}
void CArr::Print(void)
{
for (size_t i = 0; i < this->m_size; i ++)
{
cout<<" "<<this->m_pointer[i];
}
cout<<endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment