Note that I am working on a project for class and I would just like some ideas (NOT CODE) that I can use for deleting duplicates from a dynamic array containing pseudorandom numbers.
What I'm doing is allowing users to choose the range of numbers (1 through whatever they want) and how many pseudorandom numbers within that range they want to see. The array is created and filled based on this information.
Then, using sort(), the array is sorted from the least to the greatest. Which is all fine and exactly what I want.
However, here is the problem. I have no idea how to remove numbers that are duplicates or triplicates. I have developed a few lines of code to make sure that users can not force the program into a situation of returning a duplicate, but once a valid array is made, I don't know what to do.
Again, I ask that no code be given to me, but I would like some ideas as to what methods I can apply to my dynamic array. Thanks.
Code:#include <iostream> //opens iostream as a standard C++ included file #include <conio.h> //opens conio.h for use of getch() and system() commands #include <ctime> //opens ctime for the seeding of rand() using time #include <cstdlib> //opens cstdlib for rand() function #include <algorithm> //opens algorithm for sort() function using namespace std; //uses the standard C++ namespace int main() //opens the main (and only) function { int maxnum = 0; //sets the variable "maxnum" as an integer and sets it to 0 as a reset int numofnums = 0; //sets the variable "numofnums" as an integer and sets it to 0 as a reset int watcher = 0; //sets the variable "watcher" as in integer and sets it to 0 as a reset int endopt = 0; //sets the variable "endopt" as in integer and sets it to 0 as a reset int i; srand(time(0)); //seeds rand() by using the system time; exact outcome is based on OS cout << "Welcome to the Random Number Generator." << endl; cout << "Press any key to continue." << endl; getch(); //pauses the program until the user hits any key system("cls"); //a system call to clear the screen do //initial do...while loop allowing for the repetition of the entire program { do { cout << "Enter the maximum number to be returned." << endl; cout << "This number must be bigger than the number of times you want to return a random number." << endl; cout << "This number is the highest possible random number." << endl; cin >> maxnum; //this stores a user input as the highest number to be returned while (maxnum <= 0) //executes lines 32 through 36 if maxnum is 0 or a negative number { system("cls"); //a system call to clear the screen cout << "You entered an invalid maximum number." << endl; cout << "The maximum number must be greater than 0." << endl; cout << "Please try again. Enter the maximum number to be returned." << endl; cin >> maxnum; } system("cls"); //a system call to clear the screen cout << "Enter the number of random numbers you want to be returned." << endl; cout << "This number must be smaller than the highest number in the range of random numbers." << endl; cout << "This number is the total number of numbers that you will see." << endl; cin >> numofnums; while (numofnums <= 0) //executes lines 46 through 50 if numofnums is 0 or a negative number { system("cls"); //a system call to clear the screen cout << "You entered an invalid total number of numbers to display." << endl; cout << "At least 1 number must be displayed." << endl; cout << "Please try again. Enter the number of times to return a random number." << endl; cin >> numofnums; system("cls"); //a system call to clear the screen } system("cls"); //a system call to clear the screen } while (numofnums <= maxnum); int* array = new int[numofnums]; //allocates memory for the array for (i = 0; i < numofnums; i++) //sets the parameters for the array array[i] = ((rand() % maxnum) + 1); //loads the array sort(array, array+numofnums); //sorts the array int* array2 = new int[numofnums]; //allocates memory for a second array array[i] = array2[i]; //sers the original array equal to another array named "array2" for (i = 0; i < numofnums; i++) cout << "\n" << array[i]; //outputs the array cout << "\n"; //a new line cout << "Enter 1 to start again or 2 to quit." << endl; cin >> endopt; system("cls"); //a system call to clear the screen while (endopt != 1 && endopt != 2) { cout << "\n"; //a new line cout << "You did not enter 1 or 2." << endl; cout << "Enter 1 to start again or 2 to quit." << endl; cin >> endopt; } } while (endopt == 1); //this ends the main do...while loop that wraps the entire program return(0); //closes main() and ends the program }