| | |
Sorting and Searching to get rid of Duplicate Numbers
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Dec 2004
Posts: 22
Reputation:
Solved Threads: 0
I am having a problem sorting my numbers after my binary search. Can you tell me what is wrong with my code?
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <cstdlib> using namespace std; void pick_three(int p3arr[]); void pick_four(int p4arr[]); void lotto(int lottoarr[]); void selection_sort(int a[], int size); int lottobsearch(int a[], int size); int main() { int choice; int p3arr[3]; int p4arr[4]; int lottoarr[6]; cout << "Welcome to the Maryland Lottery!\n"; do { cout << endl << "Choose 1 for Pick 3.\n" << "Choose 2 for Pick 4.\n" << "Choose 3 for Lotto.\n" << "Choose 4 for Mega Millions.\n" << "Choose 5 to Exit.\n" << "Enter choice and press Enter: "; cin >> choice; switch(choice) { case 1: pick_three(p3arr); break; case 2: pick_four(p4arr); break; case 3: lotto(lottoarr); break; case 5: cout << "End of Maryland Lottery.\n"; break; default: cout << "Not a valid choice.\n" << "Choose again.\n"; } } while(choice != 5); return 0; } void pick_three(int p3arr[]) { const int arrsize = 3; p3arr[arrsize]; int rand_int = 0; int tickets; cout << "How many tickets?:\n"; cin >> tickets; cout << endl; if(tickets >=1 && tickets <=5) { while(tickets != 0) { for(int i=0; i<3; i++) { rand_int = rand()%10; p3arr[i] = rand_int; selection_sort(p3arr, arrsize); cout << p3arr[i] << endl; } cout << endl; tickets = tickets - 1; } } else { cout << "Ticket number too large.\n"; } return; } void pick_four(int p4arr[]) { const int arrsize = 4; p4arr[arrsize]; int rand_int = 0; int tickets; cout << "How many tickets?:\n"; cin >> tickets; cout << endl; if(tickets >=1 && tickets <=5) { while(tickets != 0) { for(int i=0; i<4; i++) { rand_int = rand()%10; p4arr[i] = rand_int; selection_sort(p4arr, arrsize); cout << p4arr[i] << endl; } cout << endl; tickets = tickets - 1; } } else { cout << "Ticket number too large.\n"; } return; } void lotto(int lottoarr[]) { const int arrsize = 6; lottoarr[arrsize]; int rand_int = 0; int tickets; cout << "How many tickets?:\n"; cin >> tickets; cout << endl; if(tickets >=1 && tickets <=5) { while(tickets != 0) { for(int i=0; i<6; i++) { rand_int = rand()%49+1; lottoarr[i] = rand_int; selection_sort(lottoarr, arrsize); lottobsearch(lottoarr, arrsize); cout << lottoarr[i] << endl; } cout << endl; tickets = tickets - 1; } } else { cout << "Ticket number too large.\n"; } return; } void selection_sort(int a[], int size) { int temp, i, j; for(i=0; i<size; i++) for(j=i+1; j<size; j++) { if (a[i] > a[j]) { temp = a[i]; a[i] = a[j]; a[j] = temp; } } return; } int lottobsearch(int a[], int size) { for ( int i = 0; i < size; ++i) { a[i] = rand()%49+1; for (int j = 0; j < i; ++j) { if (a[i] == a[j]) { a[j] = a[i]; } } } return 0; }
•
•
Join Date: Dec 2004
Posts: 22
Reputation:
Solved Threads: 0
•
•
•
•
Originally Posted by prog-bman
What do you mean by you are having trouble sorting your numbers after a binary search, the basis of a binary search is that the numbers are already sorted.
•
•
Join Date: Nov 2004
Posts: 108
Reputation:
Solved Threads: 3
Well first off in all you lotto statements you have a lottoarr[size]; which is doing absoulty nothing. I recommend that you turn on all your compiler's warnings that will help out with things like that probably complier options -> commands to compiler add the switch -Wall.
As for your problem
Your assigning a new value to your array during your binary search which is giving you unsorted numbers. Also another thing I noticed is that you get negative numbers sometimes also
As for your problem
C++ Syntax (Toggle Plain Text)
int lottobsearch(int a[], int size) { for ( int i = 0; i < size; ++i) { //a[i] = rand()%49+1; here is your problem for (int j = 0; j < i; ++j) { if (a[i] == a[j]) { a[j] = a[i]; } } } return 0; }
•
•
Join Date: Dec 2004
Posts: 22
Reputation:
Solved Threads: 0
•
•
•
•
Originally Posted by prog-bman
Well first off in all you lotto statements you have a lottoarr[size]; which is doing absoulty nothing. I recommend that you turn on all your compiler's warnings that will help out with things like that probably complier options -> commands to compiler add the switch -Wall.
As for your problem
Your assigning a new value to your array during your binary search which is giving you unsorted numbers. Also another thing I noticed is that you get negative numbers sometimes alsoC++ Syntax (Toggle Plain Text)
int lottobsearch(int a[], int size) { for ( int i = 0; i < size; ++i) { //a[i] = rand()%49+1; here is your problem for (int j = 0; j < i; ++j) { if (a[i] == a[j]) { a[j] = a[i]; } } } return 0; }
I never received a negative number, they might be my logic. What is the new value? I am trying to see what you are saying, but I don't see it at all.
•
•
Join Date: Nov 2004
Posts: 108
Reputation:
Solved Threads: 3
Heres a little program I whipped together for you check it out hope it will help you.
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <cstdlib> #include <ctime> using namespace std; void Sort(int numbers[], int size) { int i, j; int min, temp; //selection sort for (i = 0; i < size-1; i++) { min = i; for (j = i+1; j < size; j++) { if (numbers[j] < numbers[min]) { min = j; } } temp = numbers[i]; numbers[i] = numbers[min]; numbers[min] = temp; } } void checkForDuplicate(int numbers[], int size) { int i, j; //probably someway to make this more efficent but you get the idea for(i = 0; i < size; i++) { for(j = i + 1; j < size; j++) { if(numbers[i] == numbers[j]) { numbers[j] = rand() % 10; } } } } int main(void) { int i; const int size = 3; int array[size]; srand((unsigned int)time(NULL));//makes the numbers based on time for(i = 0; i < size; i++) { array[i] = rand() % 10; } checkForDuplicate(array,size);//call check for duplicate before you sort them //so you don't have to sort twice Sort(array,size); for(i = 0; i < size; i++) { cout<<array[i]<<endl; } cin.get(); return 0; }
![]() |
Other Threads in the C++ Forum
- Previous Thread: IF problem
- Next Thread: An Example of ugly code
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll download dynamiccharacterarray email encryption error file forms fstream function functions game generator getline givemetehcodez graph homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings struct temperature template text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





