sorting dilemma

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Dec 2007
Posts: 226
Reputation: henpecked1 is an unknown quantity at this point 
Solved Threads: 1
henpecked1 henpecked1 is offline Offline
Posting Whiz in Training

sorting dilemma

 
0
  #1
Sep 17th, 2008
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:

  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:

  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:

  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[]);
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,679
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: sorting dilemma

 
0
  #2
Sep 18th, 2008
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:
  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:
  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
Everyone's gotta believe in something. I believe I'll have another drink.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 226
Reputation: henpecked1 is an unknown quantity at this point 
Solved Threads: 1
henpecked1 henpecked1 is offline Offline
Posting Whiz in Training

Re: sorting dilemma

 
0
  #3
Sep 18th, 2008
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:
  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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 226
Reputation: henpecked1 is an unknown quantity at this point 
Solved Threads: 1
henpecked1 henpecked1 is offline Offline
Posting Whiz in Training

Re: sorting dilemma

 
0
  #4
Sep 18th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,679
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: sorting dilemma

 
0
  #5
Sep 18th, 2008
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.
Everyone's gotta believe in something. I believe I'll have another drink.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 226
Reputation: henpecked1 is an unknown quantity at this point 
Solved Threads: 1
henpecked1 henpecked1 is offline Offline
Posting Whiz in Training

Re: sorting dilemma

 
0
  #6
Sep 18th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,679
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: sorting dilemma

 
0
  #7
Sep 18th, 2008
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.
Everyone's gotta believe in something. I believe I'll have another drink.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 226
Reputation: henpecked1 is an unknown quantity at this point 
Solved Threads: 1
henpecked1 henpecked1 is offline Offline
Posting Whiz in Training

Re: sorting dilemma

 
0
  #8
Sep 18th, 2008
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
  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. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,679
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: sorting dilemma

 
0
  #9
Sep 18th, 2008
in your main( ), change the function call for mergesort to:
  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.
Everyone's gotta believe in something. I believe I'll have another drink.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 226
Reputation: henpecked1 is an unknown quantity at this point 
Solved Threads: 1
henpecked1 henpecked1 is offline Offline
Posting Whiz in Training

Re: sorting dilemma

 
0
  #10
Sep 18th, 2008
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.
Reply With Quote Quick reply to this message  
Reply

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



Similar Threads
Other Threads in the C++ Forum


Views: 776 | Replies: 12
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC