Sorting and Searching to get rid of Duplicate Numbers

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Dec 2004
Posts: 22
Reputation: blackbabydoll is an unknown quantity at this point 
Solved Threads: 0
blackbabydoll blackbabydoll is offline Offline
Newbie Poster

Sorting and Searching to get rid of Duplicate Numbers

 
0
  #1
Dec 15th, 2004
I am having a problem sorting my numbers after my binary search. Can you tell me what is wrong with my code?

  1. #include <iostream>
  2. #include <cstdlib>
  3. using namespace std;
  4.  
  5. void pick_three(int p3arr[]);
  6. void pick_four(int p4arr[]);
  7. void lotto(int lottoarr[]);
  8. void selection_sort(int a[], int size);
  9. int lottobsearch(int a[], int size);
  10.  
  11. int main()
  12. {
  13. int choice;
  14. int p3arr[3];
  15. int p4arr[4];
  16. int lottoarr[6];
  17.  
  18. cout << "Welcome to the Maryland Lottery!\n";
  19. do
  20. {
  21. cout << endl
  22. << "Choose 1 for Pick 3.\n"
  23. << "Choose 2 for Pick 4.\n"
  24. << "Choose 3 for Lotto.\n"
  25. << "Choose 4 for Mega Millions.\n"
  26. << "Choose 5 to Exit.\n"
  27. << "Enter choice and press Enter: ";
  28. cin >> choice;
  29.  
  30. switch(choice)
  31. {
  32. case 1:
  33. pick_three(p3arr);
  34. break;
  35. case 2:
  36. pick_four(p4arr);
  37. break;
  38. case 3:
  39. lotto(lottoarr);
  40. break;
  41. case 5:
  42. cout << "End of Maryland Lottery.\n";
  43. break;
  44. default:
  45. cout << "Not a valid choice.\n"
  46. << "Choose again.\n";
  47. }
  48. }
  49. while(choice != 5);
  50. return 0;
  51. }
  52.  
  53. void pick_three(int p3arr[])
  54. {
  55. const int arrsize = 3;
  56. p3arr[arrsize];
  57. int rand_int = 0;
  58. int tickets;
  59.  
  60. cout << "How many tickets?:\n";
  61. cin >> tickets;
  62. cout << endl;
  63.  
  64. if(tickets >=1 && tickets <=5)
  65. {
  66. while(tickets != 0)
  67. {
  68. for(int i=0; i<3; i++)
  69. {
  70. rand_int = rand()%10;
  71. p3arr[i] = rand_int;
  72. selection_sort(p3arr, arrsize);
  73. cout << p3arr[i] << endl;
  74. }
  75. cout << endl;
  76. tickets = tickets - 1;
  77. }
  78. }
  79. else
  80. {
  81. cout << "Ticket number too large.\n";
  82. }
  83.  
  84. return;
  85. }
  86.  
  87. void pick_four(int p4arr[])
  88. {
  89. const int arrsize = 4;
  90. p4arr[arrsize];
  91. int rand_int = 0;
  92. int tickets;
  93.  
  94. cout << "How many tickets?:\n";
  95. cin >> tickets;
  96. cout << endl;
  97.  
  98. if(tickets >=1 && tickets <=5)
  99. {
  100. while(tickets != 0)
  101. {
  102. for(int i=0; i<4; i++)
  103. {
  104. rand_int = rand()%10;
  105. p4arr[i] = rand_int;
  106. selection_sort(p4arr, arrsize);
  107. cout << p4arr[i] << endl;
  108. }
  109. cout << endl;
  110. tickets = tickets - 1;
  111. }
  112. }
  113. else
  114. {
  115. cout << "Ticket number too large.\n";
  116. }
  117.  
  118. return;
  119. }
  120. void lotto(int lottoarr[])
  121. {
  122. const int arrsize = 6;
  123. lottoarr[arrsize];
  124. int rand_int = 0;
  125. int tickets;
  126.  
  127. cout << "How many tickets?:\n";
  128. cin >> tickets;
  129. cout << endl;
  130.  
  131. if(tickets >=1 && tickets <=5)
  132. {
  133. while(tickets != 0)
  134. {
  135. for(int i=0; i<6; i++)
  136. {
  137. rand_int = rand()%49+1;
  138. lottoarr[i] = rand_int;
  139. selection_sort(lottoarr, arrsize);
  140. lottobsearch(lottoarr, arrsize);
  141. cout << lottoarr[i] << endl;
  142. }
  143. cout << endl;
  144. tickets = tickets - 1;
  145. }
  146. }
  147. else
  148. {
  149. cout << "Ticket number too large.\n";
  150. }
  151.  
  152. return;
  153. }
  154. void selection_sort(int a[], int size)
  155. {
  156. int temp, i, j;
  157.  
  158. for(i=0; i<size; i++)
  159. for(j=i+1; j<size; j++)
  160. {
  161. if (a[i] > a[j])
  162. {
  163. temp = a[i];
  164. a[i] = a[j];
  165. a[j] = temp;
  166. }
  167. }
  168. return;
  169. }
  170. int lottobsearch(int a[], int size)
  171. {
  172. for ( int i = 0; i < size; ++i)
  173. {
  174. a[i] = rand()%49+1;
  175. for (int j = 0; j < i; ++j)
  176. {
  177. if (a[i] == a[j])
  178. {
  179. a[j] = a[i];
  180. }
  181. }
  182. }
  183. return 0;
  184. }
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 108
Reputation: prog-bman is an unknown quantity at this point 
Solved Threads: 3
prog-bman prog-bman is offline Offline
Junior Poster

Re: Sorting and Searching to get rid of Duplicate Numbers

 
0
  #2
Dec 15th, 2004
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 22
Reputation: blackbabydoll is an unknown quantity at this point 
Solved Threads: 0
blackbabydoll blackbabydoll is offline Offline
Newbie Poster

Re: Sorting and Searching to get rid of Duplicate Numbers

 
0
  #3
Dec 15th, 2004
thanks. i didnt' know.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 108
Reputation: prog-bman is an unknown quantity at this point 
Solved Threads: 3
prog-bman prog-bman is offline Offline
Junior Poster

Re: Sorting and Searching to get rid of Duplicate Numbers

 
0
  #4
Dec 15th, 2004
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 22
Reputation: blackbabydoll is an unknown quantity at this point 
Solved Threads: 0
blackbabydoll blackbabydoll is offline Offline
Newbie Poster

Re: Sorting and Searching to get rid of Duplicate Numbers

 
0
  #5
Dec 15th, 2004
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.
They are already suppose to be sorted then i got in for the binary search but when my output comes up they are not sorted.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 108
Reputation: prog-bman is an unknown quantity at this point 
Solved Threads: 3
prog-bman prog-bman is offline Offline
Junior Poster

Re: Sorting and Searching to get rid of Duplicate Numbers

 
0
  #6
Dec 15th, 2004
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
  1. int lottobsearch(int a[], int size)
  2. {
  3. for ( int i = 0; i < size; ++i)
  4. {
  5. //a[i] = rand()%49+1; here is your problem
  6. for (int j = 0; j < i; ++j)
  7. {
  8. if (a[i] == a[j])
  9. {
  10. a[j] = a[i];
  11. }
  12. }
  13. }
  14. return 0;
  15. }
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
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 22
Reputation: blackbabydoll is an unknown quantity at this point 
Solved Threads: 0
blackbabydoll blackbabydoll is offline Offline
Newbie Poster

Re: Sorting and Searching to get rid of Duplicate Numbers

 
0
  #7
Dec 15th, 2004
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
  1. int lottobsearch(int a[], int size)
  2. {
  3. for ( int i = 0; i < size; ++i)
  4. {
  5. //a[i] = rand()%49+1; here is your problem
  6. for (int j = 0; j < i; ++j)
  7. {
  8. if (a[i] == a[j])
  9. {
  10. a[j] = a[i];
  11. }
  12. }
  13. }
  14. return 0;
  15. }
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

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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 108
Reputation: prog-bman is an unknown quantity at this point 
Solved Threads: 3
prog-bman prog-bman is offline Offline
Junior Poster

Re: Sorting and Searching to get rid of Duplicate Numbers

 
0
  #8
Dec 15th, 2004
Ok first you are sorting a list of numbers than when you call your search(which is really flawed btw) everytime you loop through your setting a new value to a[i] which is really lottoarr[i].
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 108
Reputation: prog-bman is an unknown quantity at this point 
Solved Threads: 3
prog-bman prog-bman is offline Offline
Junior Poster

Re: Sorting and Searching to get rid of Duplicate Numbers

 
0
  #9
Dec 15th, 2004
Heres a little program I whipped together for you check it out hope it will help you.
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <ctime>
  4. using namespace std;
  5.  
  6.  
  7. void Sort(int numbers[], int size)
  8. {
  9. int i, j;
  10. int min, temp;
  11. //selection sort
  12. for (i = 0; i < size-1; i++)
  13. {
  14.  
  15. min = i;
  16.  
  17. for (j = i+1; j < size; j++)
  18. {
  19.  
  20. if (numbers[j] < numbers[min])
  21. {
  22.  
  23. min = j;
  24.  
  25. }
  26.  
  27. }
  28.  
  29. temp = numbers[i];
  30. numbers[i] = numbers[min];
  31. numbers[min] = temp;
  32.  
  33. }
  34.  
  35. }
  36.  
  37. void checkForDuplicate(int numbers[], int size)
  38. {
  39.  
  40. int i, j;
  41. //probably someway to make this more efficent but you get the idea
  42. for(i = 0; i < size; i++)
  43. {
  44. for(j = i + 1; j < size; j++)
  45. {
  46.  
  47. if(numbers[i] == numbers[j])
  48. {
  49.  
  50. numbers[j] = rand() % 10;
  51.  
  52. }
  53.  
  54. }
  55.  
  56. }
  57.  
  58.  
  59.  
  60. }
  61.  
  62. int main(void)
  63. {
  64. int i;
  65. const int size = 3;
  66. int array[size];
  67. srand((unsigned int)time(NULL));//makes the numbers based on time
  68.  
  69. for(i = 0; i < size; i++)
  70. {
  71.  
  72. array[i] = rand() % 10;
  73.  
  74. }
  75.  
  76. checkForDuplicate(array,size);//call check for duplicate before you sort them
  77. //so you don't have to sort twice
  78. Sort(array,size);
  79.  
  80. for(i = 0; i < size; i++)
  81. {
  82.  
  83. cout<<array[i]<<endl;
  84.  
  85. }
  86.  
  87. cin.get();
  88.  
  89. return 0;
  90.  
  91. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 22
Reputation: blackbabydoll is an unknown quantity at this point 
Solved Threads: 0
blackbabydoll blackbabydoll is offline Offline
Newbie Poster

Re: Sorting and Searching to get rid of Duplicate Numbers

 
0
  #10
Dec 15th, 2004
thanks! that helped me very much with my sort. I really appreciate it.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC