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 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
|
#include <iostream>
using namespace std;
int getNumber()
{
int number (-2);
while( number < -1 || number > 100 )
{
cout << "Enter a number greater than 0 and less or equal to 100 ";
cin >> number;
if ( number < -1 || number > 100 )
{
cout << endl;
cout << "You did not enter a number between 0..100 inclusive." << endl;
cout << "Let's try again." << endl;
cout << endl;
}
}
return number;
}
void swap( int table[], int i , int j )
{
int t = table[i];
table[i] = table[j];
table[j] = t;
}
int posofSmallest( int table[], int length, int first )
{
int cmPos(first);
int i(first + 1);
while( i < length )
{
if( table[i] < table[cmPos] )
cmPos = i ;
i = i + 1 ;
}
return cmPos;
}
void selectionSort( int table[], int length)
{
int i(0);
int pos;
while ( i < length)
{
pos = posofSmallest( table, length, i );
swap( table[i], table[pos]);
i = i + 1;
}
}
void removeEntryAt( int table[], int & length, int cmPos )
{
int i(cmPos);
while ( 1 < length - 1 )
{
table[i] = table[ i + 1 ];
i = i + 1;
}
length = length - 1;
}
int getDupnumber( int table[], int length, int& cmPos)
{
int i(0);
int Dupnum(0);
int pos = cmPos + 1;
while ((table[cmPos] == table[pos]) && (pos < length))
{
pos++;
/**
pos = posofSmallest( table, length, i);
if ( table[i] == table[pos] && (i != pos))
{
Dupnum = pos;
break;
}
i++;
*/
}
return (cmPos = (pos - 1)); //incase of no duplicate, cmpos will go unchanged.
}
void compress(int table[], int initial, int start, int end)
{
while(start != end)
{
table[initial++] = table[start++];
}
return;
}
void removeDup(int table[], int& length, int cmPos)
{
int i(0);
while ( i < length)
{
if (( table[i] == table[getDupnumber(table, length, cmPos)]) && (cmPos != i))
{
//removeEntryAt( table, length, cmPos);
//this means, values from i to cmpos are same, so leaving index i, move all the values backwards
compress(table, i+1, cmPos + 1, length);
length -= (cmPos - i); //length reduced by the number of elements moved
}
cmPos = ++i; //new position to start.
}
}
void outputTable( int table[], int length)
{
cout << endl;
int i (0);
while( i < length )
{
cout << "Table[" <<i<<"] : " ;
cout << table[i] << endl;
i = i + 1;
}
cout << endl;
}
void readTable(int table[], int size, int &length)
{
int number(0);
int index(0);
number = getNumber();
while ( index < size && number > 0)
{
table[index] = number;
index = index + 1;
if (index <= size - 1)
number = getNumber();
}
length = index - 1;
}
int main()
{
int length(12);
int table[20] = {9,3,2,9,8,7,6,5,4,3,2,1};
int cmPos(0);
//readTable( table, 20, length);
outputTable( table, length);
selectionSort(table, length);
outputTable( table, length);
removeDup( table, length, cmPos);
outputTable( table, length);
system("Pause");
return 0;
}
|