944,039 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2089
  • C++ RSS
Apr 6th, 2007
0

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

Expand Post »
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..
:-|
C++ Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Navrex is offline Offline
6 posts
since Mar 2007
Apr 7th, 2007
0

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

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.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Apr 7th, 2007
0

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

another thing comes to mind: it's almost certainly not VC++ that's the problem but the code itself
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004

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: An OpenSource Database
Next Thread in C++ Forum Timeline: Partitioning





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


Follow us on Twitter


© 2011 DaniWeb® LLC