I'm working with 2d arrays (which I kinda understand) and a bubbleSort, which I sorta but don't really understand and perhaps those * and & which I'm totally lost on, especially the * which can mean what it means or the opposite of what it means? I don't get that.

The problem I'm having is converting the bubblesort given in class to one that works with 2d arrays. It should list them alphabetically and I have to use char arrays, not String - which would probably simplify it considerably.
Here's the main method and my bubblesort method, I have alternate methods in use in this; but they seem to work and don't have a direct effect on my problems. These are mainly syntax problems mind you.

#include <iostream>
#include <stdlib.h>
#include <cctype>
#include <memory.h>
#include "logic.h"

using namespace std;
 // & means location
// * value of location

char Names [ROW] [COLUMN];
	bool	Continue;
	char *	personsName; //this means location with *
	int		numNames;

void main ()
	{
		do	{
			for(int x = 0; x < 20; x++){
			cout << "Enter a name, type END to stop: ";
			personsName = readString ();


			if(strcmp (personsName, "END") !=0){
				strcpy_s(Names [x], personsName);
				numNames++;
			}
			Continue = (strcmp (personsName, "END") != 0);
			delete [] personsName;
			
			}
			printNames(Names);

			bubbleSort(Names,numNames);

			printNames(Names);


	} while (Continue);
}
void bubbleSort(int C [] [COLUMN], int arraySize){
			int		NumChars = arraySize;
			int		i;
			bool	Sorted;
			char	Temp;
			int		NumberOfCompares;

	NumberOfCompares = NumChars - 1;
	do	{	//	outer loop - keeps doing the inner loop until nothing changes
		Sorted = true;
		for (i = 0; i < NumberOfCompares; i++)	//	inner loop	- compares each pair of values
			if (toupper(*(C [i])) >toupper( *(C [i + 1])))
					{
					strcpy(Temp, *(C [i]));
					strcpy(C [i],C [i + 1]);
					strcpy(C [i + 1],Temp);
					Sorted		= false;
					}
				else;
		NumberOfCompares--;
		} while (!Sorted);
}

Recommended Answers

All 7 Replies

Whoa there, partner.

Sorting the array of strings is pretty much like sorting an array of numbers.
Don't get involved with the fact that your string array is 2D - you're only concerned with the 1D aspect.

First, your function header

void bubbleSort(int C [] [COLUMN], int arraySize)

Why is the array parameter type int? You're working with char data, so that should be a char parameter.

Your temp variable for the swaps - that needs to be a char array at least as big as your column dimension, not a single char.

Now for your comparison step

if (toupper(*(C [i])) >toupper( *(C [i + 1])))

What is *C[i] supposed to mean?
C is the address of a row, and in this context it is pointing to a string. You can't use the character function toupper() on this. Why are you doing so?
As you did in main( ) where you tested for the ending flag, you need to use strcmp() (or a variation) here to determine if two strings are out of order.

If your assignment is to ignore case in doing the compares, you can use stricmp( ) if your compiler supports it. Otherwise, you may need to create copies of the strings in question, convert them to all upper or lower case, then compare the copies.

There is no need for the empty else clause. It's OK to have an if without an else.

Thank you! I shall do these things and attempt at becoming less overwhelmed and overthinking this. :) I shall let you know in about 30 minutes if I figure it all out.

Alright. I fixed the other two issues. Now I'm getting something weird. Can someone run this and tell me what is going on?

#include <iostream>
#include <stdlib.h>
#include <cctype>
#include <memory.h>
#include "logic.h"

using namespace std;
 // & means location
// * value of location

char Names [ROW] [COLUMN];
	bool	Continue;
	char *	personsName; //this means location with *
	int		numNames;

void main ()
	{
		do	{
			for(int x = 0; x < 20; x++){
			cout << "Enter a name, type END to stop: ";
			personsName = readString ();


			if(strcmp (personsName, "END") != 0){
				strcpy_s(Names [x], personsName);
				numNames++;
			}
			else{
			Continue = (strcmp (personsName, "END") != 0);
			delete [] personsName;
			break;
			}

			}
			printNames(Names);

			bubbleSort(Names,numNames);

			printNames(Names);


	} while (Continue);
}

void printNames(char C [] [COLUMN]){
	for(int x = 0; x < 20; x++){
		for(int y = 0; y < 15;y++)
			cout << C [x] [y] << endl;
	}

}


char * readString ()
	{
			char	c;
			int		CurrSize;
			int		NumCharsRead	(0);
	const	int		StartSize		(20);
			char *	pStr;

	pStr		= new char [StartSize + 1];
	CurrSize	= StartSize;
	while ((c = cin.get ()) != '\n')	// \n is the enter key with cin
		{
		pStr [NumCharsRead] = c;
		NumCharsRead++;
		if (NumCharsRead >= CurrSize)
				{
				char * NewStr;
				CurrSize	+= StartSize;
				NewStr		 = new char [CurrSize + 1];
				memcpy (NewStr, pStr, NumCharsRead);
				delete [] pStr;
				pStr = NewStr;
				}
			else;
		}
	pStr [NumCharsRead] = '\0';
	return pStr;
}

void bubbleSort(char C [] [COLUMN], int arraySize){
			int		NumChars = arraySize;
			int		i;
			bool	Sorted;
			char	Temp [15];
			int		NumberOfCompares;

	NumberOfCompares = NumChars - 1;
	do	{	//	outer loop - keeps doing the inner loop until nothing changes
		Sorted = true;
		for (i = 0; i < NumberOfCompares; i++)	//	inner loop	- compares each pair of values
			if (stricmp(C [i],C [i +1 ]) !=0)
					{
					strcpy(Temp, C [i]);
					strcpy(C [i],C [i + 1]);
					strcpy(C [i + 1],Temp);
					Sorted		= false;
					}
				else;
			NumberOfCompares--;
		} while (!Sorted);
}

What is the "something weird"? What you have posted won't compile because you didn't provide the "logic.h" header file...

I suspect it's something like:

n
a
m
e
1
0
a
v
g
e
g
r
e
y
n

n
a
m
e
2
0
d
h
i
n
f
e
w
v
h

Here's a hint: look at how your "printNames" function is written.

How is this

if (stricmp(C [i],C [i +1 ]) !=0)

going to tell you whether the two strings are out of order? All this tells you is that they are not the same. Review your notes or text on how to use the strcmp( ) functions.

As to the output, you're dealing with strings in an array. You don't deal with each character, which is what your statement

cout << C [x] [y] << endl;

is doing.
Like I said previously, the row index alone is all you need to use with strings like this:

cout << C [x] << endl;

Alright; I think it works now! Anyone see any problems with this? Thank you all for your help :)

#include <iostream>
#include <stdlib.h>
#include <cctype>
#include <memory.h>
#include "logic.h"

using namespace std;
 // & means location
// * value of location

char Names [ROW] [COLUMN];
	bool	Continue;
	char *	personsName; //this means location with *
	int		numNames = 0;

void main ()
	{
		do	{
			for(int x = 0; x < 20; x++){
				cout << "Enter a name, type END to stop: ";
				personsName = readString ();


				if(strcmp (personsName, "END") != 0){
					strcpy_s(Names [x], personsName);
					numNames++;
				}
				else{
					Continue = (strcmp (personsName, "END") != 0);
					delete [] personsName;
					break;
				}

			}
			cout << "Pre-sorted Names" << endl;
			printNames(Names,numNames);

			bubbleSort(Names,numNames);

			cout << "Post-sorted Names" << endl;
			printNames(Names,numNames);


	} while (Continue);
}

void printNames(char C [] [COLUMN],int arraySize){
	int size = arraySize;
	for(int x = 0; x < size; x++){
				cout << C [x] << endl;
		}
}


char * readString ()
{
			char	c;
			int		CurrSize;
			int		NumCharsRead	(0);
	const	int		StartSize		(20);
			char *	pStr;

	pStr		= new char [StartSize + 1];
	CurrSize	= StartSize;
	while ((c = cin.get ()) != '\n')	// \n is the enter key with cin
		{
		pStr [NumCharsRead] = c;
		NumCharsRead++;
		if (NumCharsRead >= CurrSize)
				{
				char * NewStr;
				CurrSize	+= StartSize;
				NewStr		 = new char [CurrSize + 1];
				memcpy (NewStr, pStr, NumCharsRead);
				delete [] pStr;
				pStr = NewStr;
				}
			else;
		}
	pStr [NumCharsRead] = '\0';
	return pStr;
}

void bubbleSort(char C [] [COLUMN], int arraySize){
			int		NumChars = arraySize;
			int		i;
			bool	Sorted;
			char	Temp [15];
			int		NumberOfCompares;

	NumberOfCompares = NumChars - 1;
	do	{	//	outer loop - keeps doing the inner loop until nothing changes
		Sorted = true;
		for (i = 0; i < NumberOfCompares; i++)	//	inner loop	- compares each pair of values
			if (stricmp(C [i],C [i + 1]) > 1)
					{
					strcpy(Temp, C [i]);
					strcpy(C [i],C [i + 1]);
					strcpy(C [i + 1],Temp);
					Sorted		= false;
					}
				else;
			NumberOfCompares--;
		} while (!Sorted);
}

I see the following problems:
1) Using void main() -- see this
2) Your formatting is terrible -- see this. Without good formatting, code is very difficult to follow.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.