help!shell sort, deck...

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

Join Date: Apr 2008
Posts: 29
Reputation: didi00 is an unknown quantity at this point 
Solved Threads: 0
didi00 didi00 is offline Offline
Light Poster

help!shell sort, deck...

 
0
  #1
Dec 5th, 2008
hello,can someone help me?
I must make a program which contains:
Deck (the data in it must be integer(numbers)),(if possible,in header file).
Sort the elements by the method of Shell
Find the elements that can be divided by 5, and smaller then the largest number that can be divided by 7.
The results(if possible in header file).
Function main() with menu of the functions, and checking the status of the data.
I understand this well,but I can't do it. I've tried so long and I just can't.
You are my last hope.
Thanks

[code]
  1. #include<iostream.h>
  2. #include<conio.h>
  3. void push_l(int n);
  4. void push_r(int n);
  5. void shellsort(int a[];int left;int right);
  6. struct deck1{
  7. int key;
  8. deck1*next;
  9. }
  10. *left=NULL;
  11. *right=NULL;
  12. void main(){
  13. int num;
  14. do{
  15. int numb;
  16. cout<<"Menu:\n";
  17. cout<<"1-Add elements(left)\n";
  18. cout<<"2-Add elemens(right)\n";
  19. cout<<"3-Find elements\n";
  20. cout<<"4-Sort\n";
  21. cout<<"5-Results\n";
  22. cout<<"6-End\n";
  23. cout<<"Select:";
  24. cin<<num;
  25. switch(num){
  26. case(1):
  27. case(2):
  28. out<<"Add elements:";
  29. cin>>numb;
  30. if(num 1)
  31. push_l(numb);
  32. else
  33. push_r(numb);
  34. break;
  35. case(3):{
  36. if(numb%5&&numb<(i don't know how to do this)
  37. cout<<numb<<"\n";
  38. getche();
  39. }
  40. else{
  41. cout<<"There's no such number!\n";
  42. getch();
  43. }
  44. break;
  45. case(4):
  46. cout<<numb.shelsort()<<"\n";
  47. break;
  48. case(5):
  49. cout<<numb<<\n;
  50. while(num!6);
  51. break;
  52. }
  53. void push_l(int n){
  54. deck1 *p;
  55. p=left;
  56. left=new deck1;
  57. left->key=n;
  58. left->next*p;
  59. if(right==NULL){
  60. right=left;}
  61. }
  62. void push_r(int n){
  63. deck1*p;
  64. p=right;
  65. right=new elem;
  66. right->key=n;
  67. right->next=NULL;
  68. if(left==NULL)
  69. left=right;
  70. else
  71. p->next=right;
  72. }
  73. void shellsort(int a[];int left;int right){
  74. int h;
  75. for(h=1;h<=(right-left)/9;
  76. h=3*h+1;
  77. for(;h>0;h/=3)
  78. for(int i=left+h;i<=right;i++){
  79. int j=i;
  80. int v=a[i];
  81. while(j)=left+h&&v<a[j-h]){
  82. a[j]=a[j-h];
  83. j-=h;}
  84. a[j]=v;
  85. }
  86. }}}
  87.  
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: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: help!shell sort, deck...

 
0
  #2
Dec 5th, 2008
Any particular reason why we would want to look at that unindented mess?
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 960
Reputation: MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice 
Solved Threads: 92
MosaicFuneral's Avatar
MosaicFuneral MosaicFuneral is offline Offline
Posting Shark

Re: help!shell sort, deck...

 
0
  #3
Dec 5th, 2008
What is line 10, and 11?
main() must be able to return zero, and there really is no file called <iostream.h>, look it up.
Is getche() a typo, or do you really need it?
Biggest question: is your tab, or space bar broken? You'll never be able to fix something no-one can read, I just stop after several lines, and scrolled through the mess.
"Jedenfalls bin ich überzeugt, daß der Alte nicht würfelt."
"I became very sensitive to what will happen to all this and all of us." -Two geniuses named Albert
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 29
Reputation: didi00 is an unknown quantity at this point 
Solved Threads: 0
didi00 didi00 is offline Offline
Light Poster

Re: help!shell sort, deck...

 
0
  #4
Dec 5th, 2008
I''m so sorry about the mess!
I hope this is better:
  1. #include<stdafx.h>
  2. #include<iostream.h>
  3. #include<conio.h>
  4. void push_l(int n);
  5. void push_r(int n);
  6. void shellsort(int a[];int left;int right);
  7. struct elem{
  8. int key;
  9. elem *next;
  10. }
  11. *left=NULL;
  12. *right=NULL;
  13. int main(){
  14. int num;
  15. do{
  16. int numb;
  17. cout<<"Menu:\n";
  18. cout<<"1-Add elements(left)\n";
  19. cout<<"2-Add elemens(right)\n";
  20. cout<<"3-Find elements\n";
  21. cout<<"4-Sort\n";
  22. cout<<"5-Results\n";
  23. cout<<"6-End\n";
  24. cout<<"Select:";
  25. cin<<num;
  26. switch(num){
  27. case(1):
  28. case(2):
  29. cout<<"Add elements:";
  30. cin>>numb;
  31. if(num 1)
  32. push_l(numb);
  33. else
  34. push_r(numb);
  35. break;
  36. case(3):{
  37. if(numb%5&&numb<(i don't know how to do smaller from divided by 7)
  38. cout<<numb<<"\n";
  39. }
  40. else{
  41. cout<<"There's no such number!\n";
  42. }
  43. break;
  44. case(4):
  45. cout<<numb.shelsort()<<"\n";
  46. break;
  47. case(5):
  48. cout<<numb<<\n;
  49. while(num!6);
  50. break;
  51. }
  52. void push_l(int n){
  53. elem *p;
  54. p=left;
  55. left=new elem;
  56. left->key=n;
  57. left->next*p;
  58. if(right==NULL){
  59. right=left;}
  60. }
  61. void push_r(int n){
  62. elem *p;
  63. p=right;
  64. right=new elem;
  65. right->key=n;
  66. right->next=NULL;
  67. if(left==NULL;
  68. left=right;
  69. else
  70. p->next=right;
  71. }
  72. void shellsort(int a[];int left;int right){
  73. int h;
  74. for(h=1;h<=(right-left)/9;
  75. h=3*h+1;
  76. for(;h>0;h/=3)
  77. for(int i=left+h;i<=right;i++){
  78. int j=i;
  79. int v=a[i];
  80. while(j)=left+h&&v<a[j-h]){
  81. a[j]=a[j-h];
  82. j-=h;
  83. }
  84. a[j]=v;
  85. }
  86. }
  87. }
  88. }
  89.  
line 10 and 11 are the sides of the deck (left and right ). I made them NULL so I can add elements later.
Last edited by didi00; Dec 5th, 2008 at 7:15 pm.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 960
Reputation: MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice 
Solved Threads: 92
MosaicFuneral's Avatar
MosaicFuneral MosaicFuneral is offline Offline
Posting Shark

Re: help!shell sort, deck...

 
0
  #5
Dec 5th, 2008
Have you you tried compiling it, and reading the error reports? There's invalid operations, spelling mistakes, missing types, definitions where there shouldn't be, et al.
I think you need to start anew, slowly adding piece-by-piece, compiling, fixing, learning, and so on.

You also might want an IDE to help do the indenting for you, as you go along.
Last edited by MosaicFuneral; Dec 5th, 2008 at 8:38 pm.
"Jedenfalls bin ich überzeugt, daß der Alte nicht würfelt."
"I became very sensitive to what will happen to all this and all of us." -Two geniuses named Albert
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 29
Reputation: didi00 is an unknown quantity at this point 
Solved Threads: 0
didi00 didi00 is offline Offline
Light Poster

Re: help!shell sort, deck...

 
0
  #6
Dec 6th, 2008
Actually, I find out why it was giving me no errors, I've worked over it and now I have some questions:
The shell sort will work only if I "place" the deck into massive. (I don't know how), and I don't know will be good to find the max number then to divide it with 7 and than to add something like:
num%&&num<max%7 (I'm talking about case 3), or there is some easy way to do it?
I'm having progress so far
Thanx
Last edited by didi00; Dec 6th, 2008 at 4:43 pm.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 29
Reputation: didi00 is an unknown quantity at this point 
Solved Threads: 0
didi00 didi00 is offline Offline
Light Poster

Re: help!shell sort, deck...

 
0
  #7
Dec 6th, 2008
Originally Posted by didi00 View Post
Actually, I find out why it was giving me no errors, I've worked over it and now I have some questions:
The shell sort will work only if I "place" the deck into massive.
Sorry... I must place the deck (deque) into array so that the shell sort will work.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 29
Reputation: didi00 is an unknown quantity at this point 
Solved Threads: 0
didi00 didi00 is offline Offline
Light Poster

Re: help!shell sort, deck...

 
0
  #8
Dec 10th, 2008
Ok, I was able to correct most of the code, but I still have 10 errors.
Can someone help me now?
  1. #include<fstream.h>
  2. #include<iostream.h>
  3. #include<conio.h>
  4. void push_l(int n);
  5. void push_r(int n);
  6. int pop_l(int n);
  7. int pop_r(int n);
  8. void shell_sort(int a[],int size);
  9. void results();
  10. struct elem{
  11. int key;
  12. elem*next;
  13. };
  14. elem *left=NULL;
  15. elem *right=NULL;
  16. int b[100];
  17. int sort;
  18. void main(){
  19. int num;
  20. do{
  21. int numb;
  22. cout<<"Menu:n";
  23. cout<<"1-Add elements(left)\n";
  24. cout<<"2-Add elemens(right)\n";
  25. cout<<"3-Adding into array\n";
  26. cout<<"4-Find elements\n";
  27. cout<<"5-Sort\n";
  28. cout<<"6-Results\n";
  29. cout<<"7-End\n";
  30. cout<<"Select:\n";
  31. cin>>num;
  32. switch(num){
  33. case(1):
  34. cout<<"Add elements to left:";
  35. while(cin>>numb){
  36. push_l(numb);
  37. }
  38. break;
  39. case(2):
  40. cout<<"Add elements to right:";
  41. while(cin>>numb){
  42. push_r(numb);
  43. }
  44. break;
  45. case(3):
  46. int k,j,i=0;
  47. int a[100];
  48. a[0]=1;
  49. while(left){
  50. pop_l(k);
  51. a[i]=k;
  52. i++;
  53. }
  54. for(j=0;j<=i;j++){
  55. pop_r(a[j]);
  56. }
  57. break;
  58. case(4):
  59. int broj;
  60. int counter=-1;
  61. for (i=0; i<100; i++)
  62. if (a[i]%7==0)
  63. broj=a[i];
  64. for (i=0; i<numb; i++)
  65. if (a[i]<=broj)
  66. if (a[i]%5==0)
  67. {
  68. counter=counter++;
  69. b[counter]=a[i];
  70. }
  71. else
  72. cout<<"There's no such number!\n";
  73. }
  74. break;
  75. case(5):
  76. cout<<sort=shell_sort(numb);
  77. break;
  78. case(6):
  79. ifstream f("results.txt");
  80. while(pop_l(numb)&&pop_r(numb)){
  81. cout<<numb<<"";
  82. }
  83. while(num!=7);
  84. break;
  85. }
  86. void push_l(int n){
  87. elem *p=left;
  88. left=new elem;
  89. left->key=n;
  90. left->next=p;
  91. if(p==right)
  92. right=left;
  93. }
  94. void push_r(int n){
  95. elem *p=right;
  96. right=new elem;
  97. right->key=n;
  98. right->next=NULL;
  99. if(p)
  100. p->next=right;
  101. if(left==p)
  102. left=right;
  103. }
  104. int pop_l(int &n){
  105. elem *p;
  106. if(left){
  107. n=left->key;
  108. p=left;
  109. left=left->next;
  110. if(left==NULL)
  111. right=NULL;
  112. delete p;
  113. return 1;
  114. }
  115. else
  116. return 0;
  117. }
  118. int pop_r(int &n){
  119. elem *p;
  120. if(right){
  121. n=right->key;
  122. if(left==right){
  123. delete right;
  124. left=right=NULL;
  125. }
  126. else{
  127. p=left;
  128. while(p->next!=right)
  129. p++;
  130. n=right->key;
  131. p->next=NULL;
  132. delete right;
  133. right p;
  134. return 1;
  135. }
  136. else return 0;
  137. }
  138. void shell_sort(int a[],int size){
  139. int i,j,increment,temp;
  140. increment=size/2;
  141. while (increment>0)
  142. {
  143. for(i=increment;i<size;i++){
  144. j=i;
  145. temp=a[i];
  146. while((j>=increment)&&(a[j-increment]>temp)){
  147. a[j]=a[j-increment];
  148. j=j-increment;
  149. }
  150. a[j]=temp;
  151. }
  152. if(increment==2)
  153. increment=1;
  154. else
  155. increment=(int)(increment/2.2);
  156. }
  157. }
  158. }
  159. }
Thanks
Last edited by didi00; Dec 10th, 2008 at 4:59 pm.
Reply With Quote Quick reply to this message  
Reply

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


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC