2 Replies - 59935 Views - Last Post: 27 March 2008 - 08:42 PM Rate Topic: -----

#1 coolgymnast01  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 26-March 08

Sorting words in alphabetical order using BubbleSort

Posted 26 March 2008 - 04:24 PM

I am trying to prompt the user to enter 20 last names and have a program put them in alphabetical order using Bubblesort(). So far this is what i have:
#include<iostream>
using namespace std;

void getData(char names[][30], int& sizeOfArray);
void BubbleSort (char names[][30], int size);
void showArray(char names[][30], int size);
void strcpy(char x[], char y[]);

int main()
{
	const char TOTALNAMES = 20;
	char names[TOTALNAMES];
	int numberOfNames = 0;

	getData(names, numberOfNames);

	// Display the values
	cout << "The unsorted values are:\n";
	showArray(names, 20);

	// Sort the values
	BubbleSort(names, 20);

	// Display them again
	cout << "The sorted values are:\n";
	showArray(names, 20);

	return 0;
}

void getData (char names[][30], int& sizeOfArray)
{
	int pos = 0;
	char name[30];
	int value = 0;

	cout << "Please enter a last name" << endl;
	cin >> name;

	while (pos < 20)
	{
		names[pos] = value;
		pos++;

		cout << "Please enter another last name: " << endl;
		cin >> name;
	}
	sizeOfArray = pos;
}

void BubbleSort (char names[][30], int size)
{	int k = 0;
	 bool done = false;
	 int limit = 0;
	 char name[30];
	  while (!done)
	  {	  done = true;
			  for (int n=0; n<size-1-limit; n++)
		  if (strcmp(names[k], names[k+1]) > 0)
		   {	  Swap (name, n, n+1);
				  done = false;
		   }
		limit++;
	  }
}

void Swap (char names[][30], int n, int k)
{	char temp[30];
	 strcpy(temp,names[n]);
	 strcpy(names[n], names[k]);
	 strcpy(names[k], temp);
}

void strcpy(char x[], char y[])
{
	int n=0;
	while(y[n] != '\0')
	{
		x[n] = y[n];
		n++;
	}
	x[n] = '\0';
}

void showArray (char names[][30], int size)
{
	int count = 0;

	for (int count=0; count < size; count++)
		cout << names[] << " " << endl;
}


However, it's not working. If i could get any input from anyone that would be greatly appreciated!

Is This A Good Question/Topic? 0
  • +

Replies To: Sorting words in alphabetical order using BubbleSort

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4421
  • View blog
  • Posts: 12,286
  • Joined: 18-April 07

Re: Sorting words in alphabetical order using BubbleSort

Posted 26 March 2008 - 07:17 PM

I have gone through the code and shown you everywhere you made the mistakes. You had a few of them. Just keep an eye on what you are declaring versus what you are working on. A 2D array is needed if you want to pass to an array that you say is going to accept a 2D array.

#include<iostream>
using namespace std;

void getData(char names[][30], int& sizeOfArray);
void BubbleSort (char names[][30], int size);
void showArray(char names[][30], int size);
void strcpy(char x[], char y[]);

// Forgot to declare Swap up here.
void Swap (char names[][30], int n, int k);

int main()
{
    const char TOTALNAMES = 20;

    // You have to declare names as a 2D array of 30 character
    // strings to match your functions.
    char names[TOTALNAMES][30];
    int numberOfNames = 0;

    getData(names, numberOfNames);

    // Display the values
    cout << "The unsorted values are:\n";
    showArray(names, 20);

    // Sort the values
    BubbleSort(names, 20);

    // Display them again
    cout << "The sorted values are:\n";
    showArray(names, 20);

    return 0;
}

void getData (char names[][30], int& sizeOfArray)
{
    int pos = 0;
    char name[30];

    cout << "Please enter a last name" << endl;
    cin >> name;

    while (pos < 20)
    {
        // You have to copy the name they entered to the array
        // at the specified position, value wasn't even being
        // touched. 

        strcpy(names[pos],name);
        pos++;

        cout << "Please enter another last name: " << endl;
        cin >> name;
    }
    sizeOfArray = pos;
}

void BubbleSort (char names[][30], int size)
{    
     int k = 0;
     bool done = false;
     int limit = 0;
     char name[30];
      while (!done)
      {      
          done = true;
          for (int n=0; n<size-1-limit; n++)

          // Your for loop is manipulating "n" not "k"
          if (strcmp(names[n], names[n+1]) > 0)
           {      
                // You wanted to pass names array, not name
                Swap (names, n, n+1);
                done = false;
           }
        limit++;
      }
}

void Swap (char names[][30], int n, int k)
{    
     char temp[30];
     strcpy(temp,names[n]);
     strcpy(names[n], names[k]);
     strcpy(names[k], temp);
}

void strcpy(char x[], char y[])
{
    int n=0;
    while(y[n] != '\0')
    {
        x[n] = y[n];
        n++;
    }
    x[n] = '\0';
}

void showArray (char names[][30], int size)
{
    int count = 0;
	
    // Forgot the put the actual count in names[]
    for (int count=0; count < size; count++)
        cout << names[count] << " " << endl;
}



Read the in-code comments to see where I have made changes. Enjoy!

"At DIC we be 2D string manipulating code ninjas, but we operate on a 3D mental level!" :snap:
Was This Post Helpful? 1
  • +
  • -

#3 coolgymnast01  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 26-March 08

Re: Sorting words in alphabetical order using BubbleSort

Posted 27 March 2008 - 08:42 PM

Thank you so much for helping. I appreciated all the effort you put into this!!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1