help with sorting program

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jul 2006
Posts: 13
Reputation: djkross is an unknown quantity at this point 
Solved Threads: 0
djkross djkross is offline Offline
Newbie Poster

help with sorting program

 
0
  #1
Aug 2nd, 2006
So I have this sorting program due and I did it all but there's something crazy happening when I run the program. If I enter a value that's greater than 6400 for the size of my array, the program crashes when it tries to sort it out. Also, when i try to print out to file, anything greater than 200 elements can't be printed and the program once again crashes. It's weird. I know. Here's the code:

  1. #include <iostream>
  2. #include <algorithm>
  3. #include <ctime>
  4. #include <fstream>
  5. #include <cstdlib>
  6.  
  7. using namespace std;
  8. //functions used for sorts
  9. void generateSortedArray(int arraySize, int*& sortedArray);
  10. void generateReversedArray(int arraySize, int*& reversedArray);
  11. void generateRandomArray(int arraySize, int*& randomArray);
  12. int insertionSort(int arraySize, int array[]);
  13. void sort(int arraySize, int*& sortedArray, int*& reversedArray, int*& randomArray, int algoChoice, int sortChoice);
  14. void quickSort(int *array, int left, int right);
  15. int partition(int *array, int left, int right);
  16. void printArray(int arraySize, int *array);
  17. int main()
  18. {
  19. //declaration of variables to be used throughout program
  20. char answer;
  21. int arraySize = 0,algoChoice = 0, sortChoice = 0;
  22. //initialization of pointer arrays
  23. int *sortedArray = NULL;
  24. int *reversedArray = NULL;
  25. int *randomArray = NULL;
  26. randomArray = new int [arraySize];
  27. sortedArray=new int[arraySize];
  28. reversedArray=new int[arraySize];
  29.  
  30.  
  31. cout << "Enter the number of elements in the array: ";
  32. cin >> arraySize;
  33. cout << endl;
  34.  
  35. //used 6500 since 6400 is the highest my computer will calculate
  36. while(arraySize >= 0 && arraySize <=6500)
  37. {
  38. cout << "What algorithm would you like to use (1 = insertion 2 = quicksort): ";
  39. cin >> algoChoice;
  40. cout << endl;
  41.  
  42.  
  43. //breaks into the switches
  44. sort(arraySize, sortedArray, reversedArray, randomArray, algoChoice, sortChoice);
  45.  
  46.  
  47.  
  48. cin >> answer;
  49. //if the user wants to calculate another sort, they just enter y
  50. if(answer =='y')
  51. {
  52. cout << "Enter the number of elements in the array : ";
  53. cin >> arraySize;
  54. cout << endl;
  55. }
  56. else
  57. return -1;
  58. }
  59. //deallocation of memory
  60. delete [] randomArray;
  61. delete [] reversedArray;
  62. delete [] sortedArray;
  63. return 0;
  64. }
  65. void generateSortedArray(int arraySize, int*& sortedArray)
  66. {
  67. //sets range for elements
  68. for(int i=0; i<arraySize; i++)
  69. {
  70.  
  71. sortedArray[i] = (rand() % arraySize)+1;
  72.  
  73. }
  74.  
  75. //sorts in ascending order
  76. int temp;
  77. int counter, index;
  78. for (counter = 0; counter < arraySize - 1; counter++)
  79. {
  80. for (index = 0; index < arraySize - 1 - counter; index++)
  81. if (sortedArray[index] > sortedArray[index+1])
  82. {
  83. temp = sortedArray[index];
  84. sortedArray[index] = sortedArray[index+1];
  85. sortedArray[index+1] = temp;
  86. }
  87. }
  88.  
  89.  
  90. }
  91. void generateReversedArray(int arraySize, int*& reversedArray)
  92. {
  93. for(int i=0; i<arraySize; i++)
  94. {
  95.  
  96. reversedArray[i] = (rand() % arraySize)+1;
  97.  
  98. }
  99. //code to reverse the elements
  100. int temp;
  101. int counter, index, flag = 1;
  102. for (counter = 1; (counter <= arraySize) && flag; counter++)
  103. {
  104. flag = 0;
  105. for (index = 0; index < (arraySize - 1); index++)
  106. if (reversedArray[index+1] > reversedArray[index])
  107. {
  108. temp = reversedArray[index];
  109. reversedArray[index] = reversedArray[index+1];
  110. reversedArray[index+1] = temp;
  111. flag =1;
  112. }
  113. }
  114.  
  115. }
  116. void generateRandomArray(int arraySize, int*& randomArray)
  117. {
  118. for(int i=0; i<arraySize; i++)
  119. {
  120.  
  121. randomArray[i] = (rand() % arraySize)+1;
  122.  
  123. }
  124. //shuffles elements in array
  125. random_shuffle(randomArray , (randomArray + arraySize));
  126.  
  127. }
  128.  
  129. //The insertion sort splits an array into two sub-arrays.
  130. //The first sub-array is always sorted and gets larger as the sort continues.
  131. //The second sub-array is unsorted and contains all the elements not yet inserted into the first sub-array.
  132. // The second sub-array gets smaller as the sort progresses
  133. int insertionSort(int arraySize, int *array)
  134. {
  135. int i,j,key;
  136. for(j=1; j<arraySize; j++)
  137. {
  138. key=array[j];
  139. i=j-1;
  140. while(array[i]>key && i>=0)
  141. {
  142. array[i+1]=array[i];
  143. i--;
  144. }
  145. array[i+1]=key;
  146. }
  147. return 0;
  148. }
  149. //This sort starts by dividing the original array into two sections (partitions) based upon the value of the first item in the array.
  150. //the first section will contain all the elements with values less than the first item.
  151. //The second section will contain elements with values greater than (or equal to) the first element
  152. void quickSort(int *array, int left, int right)
  153. {
  154. int middle;
  155. if(left<right)
  156. {
  157. middle=partition(array, left, right);
  158. quickSort(array,left,middle-1);
  159. quickSort(array,middle+1,right);
  160. }
  161.  
  162.  
  163. } //end quicksort(...)
  164. int partition(int *array, int left, int right)
  165. {
  166. //the variable "middle" is the pivot of the process
  167. int middle = array[left];
  168. int i = left;
  169. int j = right+1;
  170. do
  171. {
  172. do
  173. {
  174. ++i; //increment i while the array element is less than the pivot and decrement j while it's greater.
  175. }while(array[i]<middle);
  176. do --j; while(array[j]>middle);
  177. if(i<j)//if i is less than j swap the elements
  178. swap(array[i], array[j]);
  179. }
  180. while(i<j);
  181. swap(array[j], array[left]);
  182. return j;//returns middle index
  183. }
  184. //main code
  185. void sort(int arraySize, int*& sortedArray, int*& reversedArray, int*& randomArray, int algoChoice, int sortChoice)
  186. {
  187. //the clock variables, start and end, are used to time the execution for every sort
  188. clock_t start, end;
  189.  
  190.  
  191. //algorithm choice = insertion sort
  192. if (algoChoice == 1)
  193. {
  194.  
  195. cout << "What sorting would you like to use: (1=sorted, 2=reversed, 3=random): ";
  196. cin >> sortChoice;
  197.  
  198. switch(sortChoice)
  199. {
  200. //ascending array
  201. case 1:
  202.  
  203. generateSortedArray(arraySize, sortedArray);
  204. start = clock();
  205. insertionSort(arraySize, sortedArray);
  206. end = clock();
  207. cout << endl;
  208. cout << "Duration: " << (double)(end-start)/CLOCKS_PER_SEC << endl;
  209. cout << "Writing contents of sorted array to sorted2^15.out" << endl;
  210. printArray(arraySize, sortedArray);
  211. break;
  212. case 2:
  213. //user selects 2: elements in descending order
  214. generateReversedArray(arraySize, reversedArray);
  215. start = clock();
  216. insertionSort(arraySize, reversedArray);
  217. end = clock();
  218. cout << endl;
  219. cout << "Duration: " << (double)(end-start)/CLOCKS_PER_SEC << endl;
  220. cout << "Writing contents of sorted array to sorted2^15.out" << endl;
  221. printArray(arraySize, reversedArray);
  222. break;
  223. case 3:
  224. //user selects 3: elements are randomized
  225. generateRandomArray(arraySize, randomArray);
  226. start = clock();
  227. insertionSort(arraySize, randomArray);
  228. end = clock();
  229. cout << endl;
  230. cout << "Duration: " << (double)(end-start)/CLOCKS_PER_SEC << endl;
  231. cout << "Writing contents of sorted array to sorted2^15.out" << endl;
  232. printArray(arraySize, randomArray);
  233. break;
  234. default:
  235. //user selects anything other than 1-3, this error prints
  236. cout << "Invalid choice of sort" << endl;
  237. break;
  238. }
  239. cout << endl;
  240. cout << "Do you want to test again? ";
  241. //end switch(sortChoice)
  242. } //end if (algoChoice == 1)
  243.  
  244. //choice 2 = quicksort
  245. else if (algoChoice == 2)
  246. {
  247. cout << "What sorting would you like to use: (1=sorted, 2=reversed, 3=random): ";
  248. cin >> sortChoice;
  249. switch(sortChoice)
  250. {
  251. //the left cell chosen will obviously be 0 since it's the first element of the array. the right
  252. //element is the array size minus 1 since arrays start at 0.
  253. case 1:
  254. generateSortedArray(arraySize, sortedArray);
  255. partition(sortedArray, 0, arraySize-1);
  256. start = clock();
  257. quickSort(sortedArray, 0, arraySize-1);
  258. end = clock();
  259. cout << endl;
  260. cout << "Duration: " << (double)(end-start)/CLOCKS_PER_SEC << endl;
  261. cout << "Writing contents of sorted array to sorted2^15.out" << endl;
  262. printArray(arraySize, sortedArray);
  263. break;
  264. case 2:
  265. generateReversedArray(arraySize, reversedArray);
  266. partition(reversedArray, 0, arraySize-1);
  267. start = clock();
  268. quickSort(reversedArray, 0, arraySize-1);
  269. end = clock();
  270. cout << endl;
  271. cout << "Duration: " << (double)(end-start)/CLOCKS_PER_SEC << endl;
  272. cout << "Writing contents of sorted array to sorted2^15.out" << endl;
  273. printArray(arraySize, reversedArray);
  274. break;
  275. case 3:
  276. generateRandomArray(arraySize, randomArray);
  277. partition(randomArray, 0, arraySize-1);
  278. start = clock();
  279. quickSort(randomArray, 0, arraySize-1);
  280. end = clock();
  281. cout << endl;
  282. cout << "Duration: " << (double)(end-start)/CLOCKS_PER_SEC << endl;
  283. cout << "Writing contents of sorted array to sorted2^15.out" << endl;
  284. printArray(arraySize, randomArray);
  285. break;
  286. default:
  287. cout << "Invalid choice of sort" << endl;
  288. break;
  289. }
  290. cout << endl;
  291. cout << "Do you want to test again? ";
  292. //end switch(sortChoice)
  293. } //end else if(algoChoice == 2)
  294. else
  295. cout << "Invalid algorithm choice" << endl;
  296. }
  297. //had trouble sending out to file. it would only send within 100-200 elements.
  298. //couldn't find out why
  299. void printArray(int arraySize, int *array)
  300. {
  301. ofstream outFile;
  302. outFile.open("C:/Documents and Settings/user/My Documents/sorted2^15.out");
  303.  
  304.  
  305. for(int i=0; i< arraySize; i++)
  306. {
  307.  
  308. outFile << array[i] << " ";
  309. if (i%8==0)
  310. outFile << endl;
  311.  
  312.  
  313. }
  314.  
  315. }

The code I used to generate the random numbers was :
  1. for(int i=0; i<arraySize; i++)
  2. {
  3. array[i] = (rand()%arraySize)+1;
  4. }
With "array" representing the array pointer variable to be used.

The print out to file code is:
  1. ofstream outFile;
  2. outFile.open("C:/Documents and Settings/user/My Documents/sorted2^15.out");
  3.  
  4.  
  5. for(int i=0; i< arraySize; i++)
  6. {
  7.  
  8. outFile << array[i] << " ";
  9. if (i%8==0)
  10. outFile << endl;
  11.  
  12. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,273
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 378
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: help with sorting program

 
1
  #2
Aug 2nd, 2006
From your description, it would appear you have a memory leak or you are trying to access beyond the bounds of your array.

Try breaking your program down into smaller chunks you aid debugging.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 1,496
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Solved Threads: 104
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: help with sorting program

 
1
  #3
Aug 2nd, 2006
Please format your code properly.
  • Increase the Tab settings
  • put braces even for the places where there is only one statement inside a for loop
When debugging problems, you don't debug all of the functions at once. Check each function one by one. If there is a debugger in your development environment use that. Usually crashes like this are because of array accessing errors like out-of-bound accessing.

Anyway
change this code
    randomArray = new int [arraySize];
    sortedArray=new int[arraySize];
    reversedArray=new int[arraySize];
    cout << "Enter the number of elements in the array: "; 
    cin >> arraySize;
like this
    cout << "Enter the number of elements in the array: ";     
    cin >> arraySize;
    randomArray = new int [arraySize];
    sortedArray=new int[arraySize];
    reversedArray=new int[arraySize];
you are creating an array of 0 elements and trying to access more than 0 elements. strange that it worked even upto 6400.
Last edited by WolfPack; Aug 2nd, 2006 at 6:06 pm.
バルサミコ酢やっぱいらへんで
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 13
Reputation: djkross is an unknown quantity at this point 
Solved Threads: 0
djkross djkross is offline Offline
Newbie Poster

Re: help with sorting program

 
0
  #4
Aug 2nd, 2006
AHHHHHHHHHHHHH IT WORKED!!!!!!!!!! Thanks so much, both of you!!! *bows down*
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 1118 | Replies: 3
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC