Thread: Selection Sort problem #2

  1. #1
    Code Monkey
    Join Date
    Jun 2005
    Posts
    25

    Selection Sort problem #2

    Hey guys, I've had a similar topic before - but this isn't about the fundamental workings of Selection Sort this time.

    I am using vectors in correspondence with the selection sort algorithm. (Just for fun and games)...However, I'm getting an annoying error that I cannot think of why it's happening (Because I changed the code around a bit - but this line is deciding to act up now...When it's fundamentally the same as it was before).

    Anyhoo - here's the code.

    Code:
    #include <iostream>
    #include <vector>
    
    using namespace std;
    
    int main()
    {//Main
    	vector<int> Selected_Numbers;
    	int number;
    	bool quit = false;
    	int decision;
    
    	while(!quit)
    	{//While
    		cout<<"(1)Enter a number, or (2)quit, or (3)continue"<<endl;
    		cin >>decision;
    
    		switch (decision)
    		{//Switch
    		case 1:
    
    				cout<<"What number is it?"<<endl;
    				cin >>number;
    				Selected_Numbers.push_back(number);
    				break;
    
    		case 2:
    			
    				quit = true;
    				break;
    
    		case 3:
    			
    			int LowestNumber = 0;
    			int CurrentNumber = 0;
    			
    
    			for(vector<int>::iterator iter = Selected_Numbers.begin();
    				iter != Selected_Numbers.end(); ++iter)
    			{//For1
    
    				CurrentNumber = *iter;
    
    				if(LowestNumber <= CurrentNumber)
    				{//If1
    					 LowestNumber = CurrentNumber;
    
    					 Selected_Numbers.begin() = LowestNumber; 
    /*^This is the line generating
    the problem. Compiler says 
    'error C2679: binary '=' :
    no operator found 
    which takes a right-hand 
    operand of type 'int' 
    (or there is no acceptable 
    conversion)' */
    
    				}//Close if1
    				else
    				{//else1
    				cout<<"Next Number...."<<endl<<endl;
    				}//close else1
    			}//close For1
    		
    			
    		cout<<"Alrighty then...All has been arranged from lowest - highest"<<endl<<endl;
    			
    		for(vector<int>::iterator iter = Selected_Numbers.begin();
    			iter != Selected_Numbers.end(); ++iter)
    			{//For2
    				cout<<*iter<<endl;
    			}//close For 2
    			break;
    		}//Close Switch
    
    	}//Close While
    		
    	system("pause");
    }//Close Main
    Any help will be much appreciated....I do understand what the error says - but I don't know why it's saying it! Cos before, it worked exactly the same (I had to make a change to the code to get Selection Sort to work, by adding the variable 'current number').

    - Twigstar
    Last edited by Twigstar; 07-11-2005 at 06:52 PM.

  2. #2
    Banned
    Join Date
    Jun 2005
    Posts
    594
    Try the following it work on my IDE/Compiler


    Code:
    #include <algorithm>
    and then

    Code:
    sort(Selected_Numbers.begin(), Selected_Numbers.end());

  3. #3
    Code Monkey
    Join Date
    Jun 2005
    Posts
    25
    Oh, this is just personal practice...I wouldn't want to use a predefined method....Although it's nice to know there is one!

  4. #4
    Banned
    Join Date
    Jun 2005
    Posts
    594
    Possibly this might help

    Code:
    vector<int> myIV;
    myIV[0] = 10;
    myIV.push_back(20);

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    If you are trying to assign the LowestNumber to the first entry in the vector, then you want this:

    *(Selected_Numbers.begin()) = LowestNumber;

    This will fix your compiler error because you dereference the iterator so that you aren't assigning to it, but your selection sort algorithm will still need some work.
    Last edited by Daved; 07-11-2005 at 07:04 PM.

  6. #6
    Code Monkey
    Join Date
    Jun 2005
    Posts
    25
    You're right in both cases. It works now and it doesn't.

    Hmm, I'll ponder on this one.

    Also, I wanted to use an Iterator, and not subscripting - again, for my own personal practice - thanks anyway!

  7. #7
    Banned
    Join Date
    Jun 2005
    Posts
    594
    here one i wrote to sort a regualr array
    maybe you can use it to better understand
    how to sort the vector

    Code:
    char alphabet[4];
    char check;
    for(int i = 0; i != 3; i++)
    {
    	for(int j = 0; j != 2; j++)
    	{
    		if(alphabet[j] > alphabet[j+1])
    		{
    			check = alphabet[j];
    			alphabet[j] = alphabet[j+1];
    			alphabet[j+1] = check;
    		}
    	}
    }

  8. #8
    Code Monkey
    Join Date
    Jun 2005
    Posts
    25
    Sorry to double post...Wasn't letting me edit for some bizarre reason.

    Anyhoo, I slapped myself when I realised this piece of code:

    Code:
    if(LowestNumber <= CurrentNumber)
    lol! I can be quite silly sometimes. So my new code switched them around...And then we have more problems! Here's the 'Selection Sort' part of the code.

    Code:
    for(vector<int>::iterator iter = Selected_Numbers.begin();
    				iter != Selected_Numbers.end(); ++iter)
    			{//For1
    
    				CurrentNumber = *iter;
    
    				if(CurrentNumber <= LowestNumber)
    				{//If1
    					 LowestNumber = CurrentNumber;
    
    					 *(Selected_Numbers.begin()) = LowestNumber;
    				}//Close if1
    				else
    				{//else1
    				cout<<"Next Number...."<<endl<<endl;
    				}//close else1
    It works better than before...Cos before, I typed in '3, 2, 1' - and the sort turned it into '3, 2, 1'.....But now, I put in the same - and for some bizzare reason, it returns '1, 2, 1'.....

    I can't seem to see where LowestNumber would pick up '1' again...And also omit the three....

    Any pointers?

    [Edit]...[It worked!]

    Pointer(iterator) arithmitic! Damn...So I just check the next element up in the same go, compare it, and then swap if true. And then +1 to the iter...Ok, I'll give it a bash - cheers 'IloveVectors'(What an appropiate name for this situation, lol)...I'd still like to know how that 1 got there instead of the three though, lol
    Last edited by Twigstar; 07-11-2005 at 07:32 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 01-17-2009, 10:48 PM
  2. Straight Insertion Sort function problem
    By StaticKyle in forum C++ Programming
    Replies: 6
    Last Post: 05-12-2008, 04:03 AM
  3. heap sort problem
    By Cpro in forum C++ Programming
    Replies: 2
    Last Post: 02-04-2008, 04:54 AM
  4. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  5. Replies: 0
    Last Post: 02-05-2002, 07:44 PM