Thread: bubblesort problem

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    12

    bubblesort problem

    I have been writing a bubble sort function to sort an array of numbers and an array of names. I wrote the code the way I thought it was supposed to be but every time I run it, the last input from the array gets changed into a 0 and gets sorted to the front, and I can't figure out the problem. Here is the code:

    Code:
    int main()
    {
    	char x='z';
    	string names[100]={};
    	int numbers[100]={};
    	int counter=0;
    	message();
    	while(x!='q')
    	{
    		x=getcommand();
    		switch(x)
    		{
    			case'a':
    			case'A':
    				add(names, numbers, counter);
    				break;
    			case'i':
    			case'I':
    				displayi(names, numbers, counter);
    				break;
    			case'n':
    			case'N':
    				displayn(names, numbers, counter);
    				break;
    			case'q':
    			case'Q':
    				x='q';	
    				break;
    			default:
    				cout << "Invalid Input" << endl;
    		}
    	}
    	return 0;
    }
    void add(string names[], int numbers[], int& counter)
    {
    	counter++;
    	cout << "Enter the name: ";
    	cin >> names[counter];
    	cout << "Enter the CWID: ";
    	cin >> numbers[counter];
    }
    
    void displayi(string names[], int numbers[], int& counter)
    {
    	int i, j;
    	for (i=0; i < counter; i++)
    	{
    		for (j=0; j<i; j++)
    		{
    			if(numbers[i]<numbers[j])
    			{
    				int temp= numbers[i];
    				numbers[i]=numbers[j];
    				numbers[j]=temp;
    			}
    		}
    	}
    	for(int f=0;f<counter;f++)
    		cout<<numbers[f]<<endl;
    }
    Any suggestions would be great, thanks.
    Also, once I fix this problem I have to write the other bubble sort for the names and I was confused about how, if the user selects to sort by number, to have the names sorted as well so they display next to the correct number. Thanks.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The problem with the indices comes from the fact that you increment counter before adding names and IDs to the arrays, rather than after. Array index 0 stays "blank".

    And why can't you swap names at the same time you swap numbers?

    Edit to add: And while you're at it, you should look up the bubblesort algorithm again.
    Last edited by tabstop; 02-08-2008 at 07:33 PM.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    As tabstop says, the problem is with your 'add' function.
    The 'displayi' function is okay. It's not using BubbleSort, but it will sort the numbers array. It's what some call 'SimpleSort'.

    You aren't touching the names array. I presume this is because the program is unfinished?

    You should look into using to_upper instead of having to code in case statements for both upper and lower case.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem: Bubblesort - "strlen" in the function...
    By Petike in forum C Programming
    Replies: 15
    Last Post: 01-24-2008, 09:20 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM