Merge Sort Code Not Working Properly In Vc++.code Is Given

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

Join Date: Mar 2007
Posts: 6
Reputation: Navrex is an unknown quantity at this point 
Solved Threads: 0
Navrex Navrex is offline Offline
Newbie Poster

Merge Sort Code Not Working Properly In Vc++.code Is Given

 
0
  #1
Apr 6th, 2007
Hey .........Would any 1 like to tel me that if the mege sort works at runtime or not.?
im giving my code below....Tell me y it is not running in VC++ correctly.....the code given below doesnt have any error in code but when any1 runs it it gives error.....CHECK IT OUT AND PLZZZZZZZZZ TELL ME WAT IS WRONG WITH THE CODE..
:-|
  1. #include <iostream.h>
  2. #include <conio.h>
  3. #include <process.h>
  4. #include<fstream.h>
  5. //template <class item>
  6. class Merge
  7. {
  8. private:
  9. int *arr;
  10. int *arr1;
  11. int *arr2;
  12.  
  13. int top1;
  14. int top2;
  15. int S1_size;
  16. int S2_size;
  17. public:
  18. Merge():top1(-1),top2(-1), S1_size(0), S2_size(0), arr(NULL),arr1(NULL), arr2(NULL)
  19. {}
  20. void set()
  21. {
  22. cout<<"enter the size of stack 1 : "; cin>>S1_size;
  23. cout<<"enter the size of stack 2 : "; cin>>S2_size;
  24.  
  25. //top1=-1;
  26.  
  27. //top2=-1;
  28. arr=new int[S1_size+S2_size];
  29. }
  30. bool Isfull_S1()
  31. {
  32. if(top1==S1_size)
  33. return true;
  34. return false;
  35. }
  36. bool Isfull_S2()
  37. {
  38. if(top2==S2_size)
  39. return true;
  40. return false;
  41. }
  42. bool Isempty_S1()
  43. {
  44. if(top1==-1)
  45. return true;
  46. return false;
  47. }
  48. bool Isempty_S2()
  49. {
  50. if(top2==-1)
  51. return true;
  52. return false;
  53. }
  54.  
  55. void Push(int v)
  56. {
  57. char choice;
  58. cout<<"WANT TO PUT IN STACK 1 OR STACK 2 (S/E) ";cin>>choice;
  59.  
  60. switch(choice)
  61. {
  62. case 'S':{
  63. if(Isfull_S1()==true)
  64. cout<<"Stack1 is full"<<endl;
  65. else
  66. arr1[++top1]=v;
  67. break;
  68. }
  69. case'E' : {
  70. if(Isfull_S2()==true)
  71. cout<<"Stack2 is full"<<endl;
  72. else
  73. arr2[++top2]=v;
  74. break;
  75. }
  76. default : cout<<"invalid input"<<endl;
  77. break;
  78. }
  79. }
  80.  
  81. int pop()
  82. {
  83. char choice;
  84. cout<<"WANT TO POP FROM STACK 1 OR STACK 2 (S/E) ";cin>>choice;
  85.  
  86. switch(choice)
  87. {
  88. case 'S':{
  89. if(Isempty_S1()==true)
  90. cout<<"Stack1 is empty"<<endl;
  91. else
  92. return arr1[top1--]; //problem occurs over here....
  93. break;
  94. }
  95. case'E' : {
  96. if(Isempty_S2()==true)
  97. cout<<"Stack2 is empty"<<endl;
  98. else
  99. return arr2[top2--]; //and over here also probelm occurs.:lol: .
  100. break;
  101. }
  102. default : cout<<"invalid input"<<endl;
  103. break;
  104. }
  105. return 1;
  106. }
  107. void Show()
  108. {
  109. cout<<"Stack1 Elements"<<endl;
  110. for(int i=0;i<=top1;i++)
  111. cout<<arr[i]<<endl;
  112. cout<<endl;
  113. //cout<<"Stack2 Elements"<<endl;
  114. //for(int j=S1_size;j<=top2;j++)
  115. // cout<<arr[j]<<endl;
  116. }
  117. void merge()
  118. {
  119.  
  120. int apoint, bpoint, cpoint;
  121. //int alimit, blimit, climit;
  122.  
  123. //alimit=n-1;
  124. //blimit=n2-1;
  125. //climit=n3-1;
  126. int n3 = S1_size+S2_size;
  127. if(S1_size+S2_size!=n3)
  128. {
  129. cout<<"array bounds are incompatible"<<endl;
  130. exit(1);
  131. }
  132. apoint=0;
  133. bpoint=0;
  134. for (cpoint = 0; apoint<=S1_size && bpoint<=S2_size; cpoint++)
  135. if (arr1[apoint]<arr2[bpoint])
  136. arr[cpoint]=arr1[apoint++];
  137. else
  138. arr[cpoint]=arr2[bpoint++];
  139. while(apoint<=S1_size)
  140. arr[cpoint++]=arr1[apoint++];
  141. while(bpoint<=S2_size)
  142. arr[cpoint++]=arr2[bpoint++];
  143.  
  144. }
  145.  
  146. };
  147. ////////////////////////////////////////
  148. void main()
  149. {
  150. Merge m1;
  151. m1.set();
  152. m1.Push(12);
  153. m1.Push(21);
  154. m1.Push(56);
  155. m1.Push(3);
  156. m1.Push(90);
  157. m1.Push(41);
  158. m1.Push(0);
  159. m1.Push(11);
  160. m1.Push(87);
  161. cout<<endl;
  162. m1.Show();
  163. m1.merge();
  164. cout<<endl;
  165. m1.Show();
  166. getch();
  167.  
  168. }
Last edited by ~s.o.s~; Apr 7th, 2007 at 12:34 am. Reason: Added code tags, learn to use them.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Merge Sort Code Not Working Properly In Vc++.code Is Given

 
0
  #2
Apr 7th, 2007
Two things spring to mind.

1. Your script kiddie speak is hard for me to read, and I'm a native English speaker. One wonders what others think of it who are not native speakers.
http://www.catb.org/~esr/faqs/smart-...html#writewell

2. Your code is so badly indented that neither you nor anyone else can figure out what is going on.
Last edited by Salem; Apr 7th, 2007 at 5:20 am.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 212
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Merge Sort Code Not Working Properly In Vc++.code Is Given

 
0
  #3
Apr 7th, 2007
another thing comes to mind: it's almost certainly not VC++ that's the problem but the code itself
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Reply

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




Views: 1836 | Replies: 2
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC