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
|
// This is a program that will promt the user for input of any given amount of values.
// The program will then list the values in numeric order.
#include <cstdlib>
#include <iostream>
#ifndef VECTORDOUBLE_H
#define VECTORDOUBLE_H
using namespace std;
class vectorDouble
{
public:
void fillArray (int a[], int size);
void sort (int a[], int size);
void swapValues (int &v1, int&v2);
int indexOfSmallest (const int a[], int startIndex, int size);
};
#endif
int main()
{
int arraySize;
vectorDouble value;
using namespace std;
cout << "This program will sort a list of numbers from lowest to highest"<< endl;
cout << "How many numbers do you want to sort? " << endl;
cin >> arraySize;
typedef int* IntArrayPtr;
IntArrayPtr a;
a = new int[arraySize];
value.fillArray (a, arraySize);
value.sort (a, arraySize);
cout << "In sorted order the numbers are: " << endl;
for (int index = 0; index < arraySize; index++)
cout << a[index] << " ";
new int[arraySize];
cout << endl << *a;
cout << endl;
delete [] a;
system("PAUSE");
return EXIT_SUCCESS;
}
void vectorDouble::fillArray (int a[], int size)
{
using namespace std;
cout << "Enter " << size << " integers." << endl;
for (int index = 0; index < size; index++)
{
cin >> a[index];
}
}
void vectorDouble::sort(int a[], int size)
{
int nextSmallest;
for (int index = 0; index < size - 1; index++)
{
nextSmallest = indexOfSmallest(a, index, size);
swapValues(a[index], a[nextSmallest]);
}
}
void vectorDouble::swapValues(int& v1, int& v2)
{
int temp;
temp = v1;
v1 = v2;
v2 = temp;
}
int vectorDouble::indexOfSmallest( const int a[], int startIndex, int size)
{
int min = a[startIndex],
indexOfMin = startIndex;
for(int index = startIndex + 1; index < size; index++)
if (a[index] < min)
{
min = a[index];
indexOfMin = index;
}
return indexOfMin;
}
|