i wrote a program that randomly generates numbers (from 1 to 15) for an integer array of size 17. After filling up the array with these numbers, it sorts the array in a descending order.
Now i want to eliminate the duplicate numbers from the array, and resize the array to the new number of elements. But I don't really know how to do that. Any help would be greatly appreciated. Thank You.
Here's my code:
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
const int arraySize = 17;
int RandomNumbers[arraySize];
int insert; // temporary variable to hold element to insert
srand(time(0));
cout<<"Before Sorting :"<<endl;
for (int i=0; i<17; i++)
{
RandomNumbers[i]= 1+rand()%15;
cout<<RandomNumbers[i]<<" , ";
}
cout<<endl;
cout<<endl;
for (int next = 1; next < arraySize; next++)
{
insert = RandomNumbers[next]; // store the value in the current element
int moveItem = next; //initialize location to place element
//search for the location in which to put the current element
while ((moveItem >0) &&(RandomNumbers[moveItem -1]< insert) )
{
//shift element one slot to the right
RandomNumbers[moveItem] = RandomNumbers[moveItem -1];
moveItem--;
} //end while
RandomNumbers[moveItem]= insert; //place inserted element into the array
} //end for
cout << "After Sorting :"<<endl;
// output sorted array
for (int j =0; j < arraySize; j++)
{cout<<RandomNumbers[j]<<" , ";
}
cout<<endl;
return 0;
}
Here's my output:
Before Sorting :
13 , 14 , 5 , 12 , 7 , 3 , 9 , 15 , 12 , 3 , 3 , 13 , 13 , 3 , 3 , 10 , 4 ,
After Sorting :
15 , 14 , 13 , 13 , 13 , 12 , 12 , 10 , 9 , 7 , 5 , 4 , 3 , 3 , 3 , 3 , 3 ,
Press any key to continue . . .