C++ Name Sort Using BubbleSort

Write a program, which will ask the user how many names (one word, no spaces) they wish to enter. The program will then read that many names, store them in a two dimensional array of characters (one row per name), print them in the order they were entered, then sort them in increasing alphabetical order, and print them in sorted order. The sort will be done without regard to the case of the letters (i.e. case insensitive sort). The printing of each name MUST be done with the characters as they were typed, you cannot change the case.

The maximum number of names to be handled will be 20.

The maximum number of printable characters in a name is 15.

Use a two dimensional array to store the names.

Create a function to read in all the names.

Create a function to do the sort (modify the bubble sort discussed in class and in the text book).


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
  #include <iostream>
#include "ReadString.h"
#include "BubbleSort.h"

using namespace std;

	void main()
	{
		int		i;
		int		NumberN;
		char *	pName[20][16];
		char ** pNames;

		cout << "How many names would you like? ";

		cin >> NumberN;

		cin.ignore();

		pNames = new char *[NumberN];

		cout << "Enter " << NumberN << " names" << endl;
		for (i = 0; i < NumberN; i++)
		{
			cout << (i + 1) << ") ";
			pNames[i] = ReadString();
		}
		cout << "\n\tYou entered the following names" << endl;
		for (i = 0; i < NumberN; i++)
			cout << (i + 1) << ") " << pNames[i] << endl;

		BubbleSort (pNames[i],NumberN);



[/code]

void BubbleSort(char Str[])
{
int i;
int NumElements;
bool Sorted;
char Temp;

NumElements = strlen(Str);
do {
Sorted = true;
NumElements--;
for (i = 0; i < NumElements; i++)
if (Str[i] > Str[i + 1])
{
Temp = Str[i];
Str[i] = Str[i + 1];
Str[i + 1] = Temp;
Sorted = false;
}
} while (!Sorted);
}
// they modified the bubblesort to use for this particular lab, but i havent //been able to go to class so i wasnt present when they modified the code.
[/code]
I've gotten this far, having missed the last couple of classes due to a bike accident, I'm behind. Anyone mind giving me some steps to wrap this program up? Thanks for any and all help.
You're close. I believe you're missing an extra for loop in your sort function. You're current function is currently just sorting through once and not checking to see if it needs to sort any other chars.

Below is an example.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
	
	int holder= 0;
	for (int i = 0; i < size; i++) {
		for (int j = 0; j < size - 1; j++) {
			if (score[j] > score[j + 1]) {
				holder = score[j + 1];
				score[j + 1] = score[j];
				score[j] = holder;


			}
		}
	}
}
Topic archived. No new replies allowed.