Skip to content

Instantly share code, notes, and snippets.

@johnhmj
Created December 1, 2010 12:26
Show Gist options
  • Save johnhmj/723414 to your computer and use it in GitHub Desktop.
Save johnhmj/723414 to your computer and use it in GitHub Desktop.
// Integrated Development Environment
// Visual C++
#include <iostream>
#include <cstdlib>
#include "header.h"
using namespace std;
//
int main(int argc, char** argv)
{
unsigned int length = 0;
cout << "Input array size: ", cin >> length;
CMyArr arr(length);
for (unsigned int i = 0; i < arr.m_size; i ++)
{
int val;
cout << "Input [" << i << "]: ", cin >> val;
arr.SetVal(i, val);
}
arr.Display();
arr.SortArr();
arr.Display();
cout << "Max = " << arr.m_max << endl;
cout << "Min = " << arr.m_min << endl;
system("PAUSE");
return (0);
}
#ifndef _HEADER_H_
#define _HEADER_H_
#include <iostream>
using namespace std;
//
class CMyArr
{
public:
int* m_ptr;
unsigned int m_size;
int m_max;
int m_min;
CMyArr(unsigned int length);
~CMyArr(void);
void SetVal(unsigned int pos, int val);
void SortArr(void);
void Display(void);
};
//
#endif
#include "header.h"
using namespace std;
//
CMyArr::CMyArr(unsigned int length)
{
this->m_size = length;
this->m_ptr = new int[this->m_size];
}
CMyArr::~CMyArr(void)
{
if ( this->m_ptr != NULL )
{
delete [] this->m_ptr;
}
this->m_size = 0;
}
void CMyArr::SetVal(unsigned int pos, int val)
{
if ( pos >= (this->m_size) )
{
return;
}
this->m_ptr[pos] = val;
}
void CMyArr::SortArr(void)
{
int temp;
for (unsigned int i = 0; i < (this->m_size); i ++)
{
for (unsigned int j = 0; j < (this->m_size); j ++)
{
if ( this->m_ptr[i] > this->m_ptr[j] )
{
temp = this->m_ptr[i];
this->m_ptr[i] = this->m_ptr[j];
this->m_ptr[j] = temp;
}
}
}
this->m_max = this->m_ptr[0];
this->m_min = this->m_ptr[this->m_size - 1];
}
void CMyArr::Display(void)
{
cout << "Array = [";
for (unsigned int i = 0; i < (this->m_size); i ++)
{
cout << " " << this->m_ptr[i];
}
cout << "]" << endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment