943,662 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 992
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Sep 17th, 2008
0

sorting dilemma

Expand Post »
I'm doing a lab where I must use three sorting methods, then merge sort the three arrays. While separating the larger array, I'm getting a
"run time check error #2 stack around the variable x was corrupted:

It does this for y and z as well. I think it is happening where I break up the bigarray into smaller ones, but not sure, and cannot figure out why it's happening. The code is below:

C++ Syntax (Toggle Plain Text)
  1.  
  2. int main()
  3. {
  4. int bigarray[151];
  5. int x[50];
  6. int y[50];
  7. int z[50];
  8.  
  9. srand ((unsigned) time(NULL));
  10.  
  11. for (int i=0; i<151 ;i++)
  12. {
  13. bigarray[i]=rand()%1000;
  14. cout<< " "<<bigarray[i];
  15. }
  16.  
  17.  
  18. cout <<endl<<endl<<endl;
  19. cout<<" 1 A...THIS IS THE FIRST UNSORTED 50 ELEMENTS OF THE ARRY "<<endl;
  20.  
  21. for(int i=0; i < 50; i++)// this is
  22. {
  23. x[i] = bigarray[i];
  24. cout <<" "<<x[i];
  25. }
  26.  
  27. cout<<endl<<endl<<endl<<endl<<"2 A...THIS IS THE SECOND 50 ELEMENTS OF THE ARRAY "<<endl<<endl;
  28.  
  29. for(int i=50; i < 101; i++)
  30. {
  31. y[i] =bigarray[i];
  32. cout <<" "<<y[i];
  33. }
  34.  
  35. cout<<endl<<endl<<endl<<endl<<" 3 A ... THIS IS THE THIRD 50 ELEMENTS OF THE ARRAY "<<endl;
  36.  
  37. for(int i=100; i < 151; i++)
  38. {
  39. z[i] =bigarray[i] ;
  40. cout <<" "<<z[i];
  41. }
  42.  
  43. insertion_sort(x);
  44. selection_sort(y);
  45. bubble_sort(z);
  46. merge_sort(x, y, z);
  47.  
  48. return 0;
  49. }

the cpp definitions for the header:

C++ Syntax (Toggle Plain Text)
  1.  
  2. void insertion_sort(int x[])
  3. {
  4. int key,i;
  5. for(int j=0;j<50;j++)
  6. {
  7. key=x[j];
  8. i=j-1;
  9. while(x[i]>key && i>=0)
  10. {
  11. x[i+1]=x[i];
  12. i--;
  13. }
  14. x[i+1]=key;
  15. }
  16. }
  17.  
  18.  
  19. void selection_sort(int y[])
  20. {
  21. int tmp;
  22. int min;
  23. for(int i=0;i<50;i++)
  24. {
  25. min = i;
  26. for(int x=i; x<50; x++)
  27. {
  28. if(y[x] < y[min])
  29. {
  30. min = x;
  31. }
  32. }
  33. tmp = y[i];
  34. y[i] = y[min];
  35. y[min] = tmp;
  36. }
  37. }
  38.  
  39.  
  40.  
  41. void bubble_sort(int z[])
  42. {
  43. int i,j;
  44. for(i=0;i<50;i++)
  45. {
  46. for(j=0;j<i;j++)
  47. {
  48. if(z[i]>z[j])
  49. {
  50. int temp=z[i];
  51. z[i]=z[j];
  52. z[j]=temp;
  53. }
  54.  
  55. }
  56.  
  57. }
  58.  
  59. }
  60.  
  61.  
  62. void merge_sort(int x[], int y[], int z[])
  63. {
  64. int indexx = 0; // initialize the Array Indices
  65. int indexy = 0;
  66. int indexz = 0;
  67.  
  68. while((indexx < 50) && (indexy < 50))
  69. {
  70. if (x[indexx] < y[indexy])
  71. {
  72. z[indexz] = x[indexx];
  73. indexx++; //increase the index
  74. }
  75. else
  76. {
  77. z[indexz] = y[indexy];
  78. indexy++; //increase the index
  79. }
  80. indexz++; //move to the next position in the new array
  81. }
  82. // Push remaining elements to end of new array when 1 feeder array is empty
  83. while (indexx < 50)
  84. {
  85. z[indexz] = x[indexx];
  86. indexx++;
  87. indexz++;
  88. }
  89. while (indexy< 50)
  90. {
  91. z[indexz] = y[indexy];
  92. indexy++;
  93. indexz++;
  94. }
  95. return;
  96. }

The header:

C++ Syntax (Toggle Plain Text)
  1. #pragma once
  2.  
  3.  
  4. void insertion_sort(int x[]);
  5.  
  6. void selection_sort(int y[]);
  7.  
  8. void bubble_sort(int z[]);
  9.  
  10. void merge_sort(int x[], int y[], int z[]);
Similar Threads
Reputation Points: 10
Solved Threads: 2
Posting Whiz in Training
henpecked1 is offline Offline
244 posts
since Dec 2007
Sep 18th, 2008
0

Re: sorting dilemma

OK, this is a sneaky one.

First of all, you've made a big error when you're loading the y and z arrays - you start their loops at a value other than 0. Look at your code below:
C++ Syntax (Toggle Plain Text)
  1. for(int i=0; i < 50; i++)// this is
  2. {
  3. x[i] = bigarray[i];
  4. cout <<" "<<x[i];
  5. }
  6.  
  7.  
  8. for(int i=50; i < 101; i++)
  9. {
  10. y[i] =bigarray[i];
  11. cout <<" "<<y[i];
  12. }
You've filled the x array, element indexes 0-49 - fine! Then you start y array at index 50, and go to <101, which if you count on your fingers you'll find is 51 elements - OOPS. Double OOPS, array y should have started at index 0. The way memory usually gets laid out (with no optimization going on, anyway), the x array is at higher memory address than y array, which is higher than z array. All your array writing has occurred in the x array (three times) and by going to indexes limited by <101 and <151, you've written to the element one past the end of x array. That's what fires the warning.

Solution to this - use a separate index for reading from bigarray, restart each of x, y, z at the same 0, like:
C++ Syntax (Toggle Plain Text)
  1. int i, j = 0;
  2. for( i=0; i < 50; i++, j++)// this is
  3. {
  4. x[i] = bigarray[j];
  5. cout <<" "<<x[i];
  6. }
  7.  
  8. //j starts at 50 for this loop
  9. for(i=0; i < 50; i++,j++)
  10. {
  11. y[i] =bigarray[j];
  12. cout <<" "<<y[i];
  13. }
  14. //and again for the z array
Reputation Points: 1268
Solved Threads: 228
Posting Virtuoso
vmanes is offline Offline
1,895 posts
since Aug 2007
Sep 18th, 2008
0

Re: sorting dilemma

I think I see what you mean with the incrementing i++ for x j++ for y and say k++ for z. I thought it might have something to do with the counts, but I couldn't see where I was off. I didn't think you had to restart from 0 every time, I thought you just started from a different index.

hmm, still giving me that error on y and z after changing the code to this:
C++ Syntax (Toggle Plain Text)
  1.  
  2. int main()
  3. {
  4. int bigarray[151];
  5. int x[50];
  6. int y[50];
  7. int z[50];
  8.  
  9. srand ((unsigned) time(NULL));
  10.  
  11. int i=0;
  12. int j=0;
  13. int k=0;
  14.  
  15. for (int i=0; i<151 ;i++)
  16. {
  17. bigarray[i]=rand()%1000;
  18. cout<< " "<<bigarray[i];
  19. }
  20.  
  21.  
  22. cout <<endl<<endl<<endl;
  23. cout<<" 1 A...THIS IS THE FIRST UNSORTED 50 ELEMENTS OF THE ARRY "<<endl;
  24.  
  25. for(int i=0; i < 50; i++, j++, k++)// this is
  26. {
  27. x[i] = bigarray[i];
  28. cout <<" "<<x[i];
  29. }
  30.  
  31. cout<<endl<<endl<<endl<<endl<<"2 A...THIS IS THE SECOND 50 ELEMENTS OF THE ARRAY "<<endl<<endl;
  32.  
  33. for(int i=0; i < 50; i++, j++, k++)
  34. {
  35. y[i] =bigarray[j];
  36. cout <<" "<<y[i];
  37. }
  38.  
  39. cout<<endl<<endl<<endl<<endl<<" 3 A ... THIS IS THE THIRD 50 ELEMENTS OF THE ARRAY "<<endl;
  40.  
  41. for(int i=0; i < 50; i++, j++, k++)
  42. {
  43. z[i] =bigarray[k] ;
  44. cout <<" "<<z[i];
  45. }
  46.  
  47. insertion_sort(x);
  48. selection_sort(y);
  49. bubble_sort(z);
  50. merge_sort(x, y, z);
  51.  
  52. return 0;
  53. }
Last edited by henpecked1; Sep 18th, 2008 at 12:27 am.
Reputation Points: 10
Solved Threads: 2
Posting Whiz in Training
henpecked1 is offline Offline
244 posts
since Dec 2007
Sep 18th, 2008
0

Re: sorting dilemma

altered to match what you posted (for z as well, misunderstood)
Still gives me that error for y and z?
Last edited by henpecked1; Sep 18th, 2008 at 12:35 am.
Reputation Points: 10
Solved Threads: 2
Posting Whiz in Training
henpecked1 is offline Offline
244 posts
since Dec 2007
Sep 18th, 2008
0

Re: sorting dilemma

I don't see any reason for warnings or errors around the y and z arrays. Please post the exact message, and line that it points to, if any.

Also, you don't need variable k, just set j to 0 at the beginning, let it be the index of bigarray in all three loops.
Reputation Points: 1268
Solved Threads: 228
Posting Virtuoso
vmanes is offline Offline
1,895 posts
since Aug 2007
Sep 18th, 2008
0

Re: sorting dilemma

Okay, removed k completely, set j to be the index of big array in all three loops.

I no longer get the error for x, only for y and z. The error comes up in a windows dialog box and says

"run time check error #2 stack around the variable y was corrupted" and asks me if I want to abort, retry, or ignore. If I hit ignore, it says "run time check error #2 stack around the variable z was corrupted", and if I hit ignore for that, the program ends
Reputation Points: 10
Solved Threads: 2
Posting Whiz in Training
henpecked1 is offline Offline
244 posts
since Dec 2007
Sep 18th, 2008
0

Re: sorting dilemma

please post the current version of the code. I still don't see anything in what you posted above that should be problematic.

Whoa, look at your mergesort. You pass it the x, y, z arrays, each sized 50. Your function is merging the first two into the third - thus you're stuffing 100 items in the 50 element z array!!!!

Shouldn't the merge function be given all four of your arrays, merging x, y, z back into bigarray?

Do the error messages come up with y first, then z? I'd think it would be the other way.
Reputation Points: 1268
Solved Threads: 228
Posting Virtuoso
vmanes is offline Offline
1,895 posts
since Aug 2007
Sep 18th, 2008
0

Re: sorting dilemma

yes, it comes up with y first, then z. It's definitely something I did in merge sort, when I comment it out I get no errors.

here's the merge sort that I messed up
C++ Syntax (Toggle Plain Text)
  1. void merge_sort(int x[], int y[], int z[])
  2. {
  3. int indexx = 0; // initialize the Array Indices
  4. int indexy = 0;
  5. int indexz = 0;
  6.  
  7. while((indexx < 50) && (indexy < 50))
  8. {
  9. if (x[indexx] < y[indexy])
  10. {
  11. z[indexz] = x[indexx];
  12. indexx++; //increase the index
  13. }
  14. else
  15. {
  16. z[indexz] = y[indexy];
  17. indexy++; //increase the index
  18. }
  19. indexz++; //move to the next position in the new array
  20. }
  21. // Push remaining elements to end of new array when 1 feeder array is empty
  22. while (indexx < 50)
  23. {
  24. z[indexz] = x[indexx];
  25. indexx++;
  26. indexz++;
  27. }
  28. while (indexy< 50)
  29. {
  30. z[indexz] = y[indexy];
  31. indexy++;
  32. indexz++;
  33. }
  34. return;
  35. }
Reputation Points: 10
Solved Threads: 2
Posting Whiz in Training
henpecked1 is offline Offline
244 posts
since Dec 2007
Sep 18th, 2008
0

Re: sorting dilemma

in your main( ), change the function call for mergesort to:
C++ Syntax (Toggle Plain Text)
  1. merge_sort(x, y, bigarray);
Which is really what you want. Of course, this is not getting the z array data in, but you'll work on that later.
Reputation Points: 1268
Solved Threads: 228
Posting Virtuoso
vmanes is offline Offline
1,895 posts
since Aug 2007
Sep 18th, 2008
0

Re: sorting dilemma

did that already, errors gone. Now how is array z brought into the mix?...other than adding it to the argument list.

Is it compared to x and y as well, or is it somehow compared to bigarray? I'm not sure how thats done
Last edited by henpecked1; Sep 18th, 2008 at 1:16 am.
Reputation Points: 10
Solved Threads: 2
Posting Whiz in Training
henpecked1 is offline Offline
244 posts
since Dec 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: please solve my problem
Next Thread in C++ Forum Timeline: Vector & reference Questions





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC