943,807 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 581
  • C++ RSS
Oct 10th, 2008
0

Help with list

Expand Post »
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, 15 views)
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
vrga is offline Offline
6 posts
since Sep 2008
Oct 11th, 2008
0

Re: Help with list

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.

C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 673
Solved Threads: 125
Practically a Posting Shark
Sky Diploma is offline Offline
818 posts
since Mar 2008
Oct 11th, 2008
0

Re: Help with list

Something is wrong because it is not outputting anything. Thank you!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
vrga is offline Offline
6 posts
since Sep 2008
Oct 12th, 2008
1

Re: Help with list

C++ Syntax (Toggle Plain Text)
  1. while(soldiers.size()==1)
This while statement is ill
Reputation Points: 110
Solved Threads: 43
Posting Whiz in Training
Sci@phy is offline Offline
279 posts
since Sep 2008
Oct 12th, 2008
0

Re: Help with list

C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 673
Solved Threads: 125
Practically a Posting Shark
Sky Diploma is offline Offline
818 posts
since Mar 2008

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: Another problem
Next Thread in C++ Forum Timeline: Matrix Inversion help :'(





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


Follow us on Twitter


© 2011 DaniWeb® LLC