The problem was to take 20 unique numbers and display in reverse order. I changed to 5 numbers for testing. I have got the sort(insertion sort), but the not allowing duplicates is the problem. At the moment it will only catch the first duplicate and the allows the following dupes. You can feel free to ignore the sorting and displaying. Here's my code. I moved the calling of the seach function to different places and still only catches the first dupe. I don't need a search function but I've tried it in the loop and it still only caught the first dupe. Also I would like to move the prompt out of the loop as to only display it once, but this causes the array to fill up with the first number entered.
#include <iostream> #include <iomanip> using namespace std; int search( const int [], int, int); int main() { const int arraySize = 5; int data[ arraySize ]= {0,0,0,0,0}; int insert; int input; int count = 0; while (count < 5) { cout << "Enter 5 unique numbers: "; cin >> input; int element = linearSearch(data,input,arraySize); if(element == -1 && input >= 10 && input <=100) { data[count] = input; count++; } else if(element != -1) { cout << "Duplicate value." << endl; } else { cout << "Invalid input" << endl; } } //Display Unsorted Array cout << "Unsorted array:" << endl; for( int i = 0; i < arraySize; i++) { cout << setw(4) <<data[ i ]; } //insertion sort for( int next = 1; next < arraySize; next++) { insert = data[ next ]; int moveItem = next; while ((moveItem > 0) && (data[ moveItem - 1] < insert )) { data[moveItem ] = data[ moveItem -1]; moveItem--; } data[ moveItem ] = insert; } cout << endl << "Sorted Array: " << endl; for(int i = 0; i < arraySize; i++) cout << setw(4) << data[ i ]; cout << endl; system("pause"); } //I don't think I need a function for this but am unable to figure it out either way. int search(const int array[], int inputKey, int sizeOfArray) { for ( int j = 0; j < sizeOfArray; j++) { if(array[ j ] == inputKey) { return j; } else { return -1; } } }