Help with list

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2008
Posts: 6
Reputation: vrga is an unknown quantity at this point 
Solved Threads: 0
vrga vrga is offline Offline
Newbie Poster

Help with list

 
0
  #1
Oct 10th, 2008
I need help deleting from a vector. My program is about the josephus problem. It has to delete soldiers. It does deletes the first one but then it stops. Thank you!
Attached Files
File Type: txt list.txt (964 Bytes, 1 views)
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 675
Reputation: Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold 
Solved Threads: 100
Sky Diploma's Avatar
Sky Diploma Sky Diploma is offline Offline
Practically a Master Poster

Re: Help with list

 
0
  #2
Oct 11th, 2008
Well I see that you will need to delete all the members until you are left with one.

So you will need to do this.

  1. #include <iostream>
  2. #include <list>
  3.  
  4. using namespace std;
  5. int main() {
  6. int n;//number of soldiers
  7. int k;//number skipped between
  8.  
  9. cout << "Welcome to The Josephus Problem" << endl;
  10. cout << "Please enter the number of soldiers in the army." << endl;
  11. cin >> n;
  12. cout << "Please enter the number to be skipped between sucessive executions." << endl;
  13. cin >> k;
  14.  
  15. list < int > soldiers;
  16. for ( int i = 0; i < n; i++ )
  17. soldiers.push_back ( i + 1 );
  18.  
  19. list < int > :: iterator p;
  20.  
  21. p = soldiers.begin ();
  22. while(soldiers.size()==1)
  23. {
  24. for ( int i = 0; i < k-1; i++ ) {
  25. p++;
  26. if ( p == soldiers.end() )
  27. p = soldiers.begin ();
  28. }
  29. p = soldiers.erase ( p );
  30.  
  31.  
  32. for ( p = soldiers.begin(); p != soldiers.end(); p++ ){
  33.  
  34. cout << *p;
  35. }
  36. }
  37.  
  38.  
  39. system("pause");
  40.  
  41. return 0;
  42. }

I dint check the code. But it should work.
1. Please Mark Your Thread as Solved After Getting Your Answers.
2. Please Use CODE TAGS .
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 6
Reputation: vrga is an unknown quantity at this point 
Solved Threads: 0
vrga vrga is offline Offline
Newbie Poster

Re: Help with list

 
0
  #3
Oct 11th, 2008
Something is wrong because it is not outputting anything. Thank you!
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 273
Reputation: Sci@phy will become famous soon enough Sci@phy will become famous soon enough 
Solved Threads: 42
Sci@phy's Avatar
Sci@phy Sci@phy is offline Offline
Posting Whiz in Training

Re: Help with list

 
1
  #4
Oct 12th, 2008
  1. while(soldiers.size()==1)
This while statement is ill
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 675
Reputation: Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold 
Solved Threads: 100
Sky Diploma's Avatar
Sky Diploma Sky Diploma is offline Offline
Practically a Master Poster

Re: Help with list

 
0
  #5
Oct 12th, 2008
  1. #include <iostream>
  2. #include <list>
  3.  
  4. using namespace std;
  5. int main() {
  6. int n;//number of soldiers
  7. int k;//number skipped between
  8.  
  9. cout << "Welcome to The Josephus Problem" << endl;
  10. cout << "Please enter the number of soldiers in the army." << endl;
  11. cin >> n;
  12. cout << "Please enter the number to be skipped between sucessive executions." << endl;
  13. cin >> k;
  14.  
  15. list < int > soldiers;
  16. for ( int i = 0; i < n; i++ )
  17. soldiers.push_back ( i + 1 );
  18.  
  19. list < int > :: iterator p;
  20.  
  21. p = soldiers.begin ();
  22. while(soldiers.size()!=1)
  23. {
  24. for ( int i = 0; i < k-1; i++ ) {
  25. p++;
  26. if ( p == soldiers.end() )
  27. p = soldiers.begin ();
  28. }
  29. p = soldiers.erase ( p );
  30.  
  31.  
  32. for ( p = soldiers.begin(); p != soldiers.end(); p++ ){
  33.  
  34. cout << *p;
  35. }
  36. }
  37.  
  38.  
  39. system("pause");
  40.  
  41. return 0;
  42. }

I guess it is solved now.
1. Please Mark Your Thread as Solved After Getting Your Answers.
2. Please Use CODE TAGS .
Reply With Quote Quick reply to this message  
Reply

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



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC