Selection sort 2d char array

I am not sure on how to sort the last name instead of first name.

Everything works so far and sorting is done on the first character.
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
#include <cstring>
using namespace std;

void sort(int, char[10][40]);

int main()
{
	char twoD[10][40];
	int input = 0;

	cout << "Please enter the number of names: ";
	cin >> input;
	cout << endl;

	cout << "Enter first names and last names:" << endl;
	cin.ignore();
	for (int i = 0; i < input; i++)
	{
		cin.getline(twoD[i], sizeof(twoD[i]));
	}
	cout << endl;

	sort(input, twoD);

	for (int i = 0; i < input; i++)
	{
		cout << twoD[i] << endl;
	}
	cout << endl;

	return 0;
}
void sort(int n, char array[10][40])
{
	char min[40];
	int a;

	for (int i = 0; i < n - 1; i++)
	{
		a = i;
		strcpy_s(min, array[i]);

		for (int j = i + 1; j < n; j++)
		{
			if( (strcmp(array[j], min)) < 0)
			{
				strcpy(min, array[j]);
				a = j;
			}
		}
		strcpy_s(array[a], array[i]);
		strcpy_s(array[i], min);
	}
}
You must create a function to extract the surname only. Since you are using C-strings, why not just get another char* to the proper data?

1
2
3
4
const char* find_surname( const char* name )
{
	...
}

Use an example to help:

Luke Skywalker

A string containing fifteen used characters (the char array itself may be longer, as in your program at 40 characters long). The fifteenth character (at index 14) has the value zero, to tell where the end of the string is.


If you are unsure about this stuff, take a look at the FAQ
http://www.cplusplus.com/faq/sequences/strings/c-strings-and-pointers/


Now to find just "Skywalker", find the first character following the first space. Return a pointer (char*) to it.

Your sort function (which I would name sort_by_surname()) can use the find_surname() function on every string it gets. Once you have the surname, you can use std::strcmp() to sort properly.

Good luck!
I figured out how to find the index after the first space. Is it possible to use the index that was found in the sort function? I am not sure how to use pointers as you mentioned.

1
2
3
4
5
6
7
8
9
10
11
12
13
for (int i = 0; i < 1; i++)
{
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < 20; j++)
			if (twoD[i][j] == ' ')
			{
				int index = j + 1;
				break;
			}
	}
}
return index;
You didn't read the link.

Remember, a character string is indexed by a pointer:

1
2
3
const char* foo = "Hello world!";
"Hello world!"
 ↑
 foo

A pointer points to the memory location of the first character in the array.

Your code from lines 5 through 10, with one important adjustment:

1
2
3
4
5
6
7
8
9
10
11
12
13
const char* find_surname( const char* name )
{
	int index = 0;
	for (int j = 0; name[j] != 0; j++)  // remember, all strings end at zero
	{
		if (name[j] == ' ')
		{
			index = j + 1;
			break;
		}
	}
	return name + index;  // result string is just the surname
}

Now you can use the function:

1
2
3
const char* bar = find_surname( foo );
"Hello world!"
 ↑     ↑
 foo   bar

Notice that the character array is not changed. But you have a new pointer to a new "beginning" of an array -- which really isn't the true beginning, but no function will know the difference, since it only knows about the 'bar' pointer (to print, or sort, or whatever); it cannot know anything about 'foo'.

For your data, you have an array of 10 names. Each name is an array of 40 characters.

Find the surname by passing a name to the function:

1
2
3
4
5
for (int i = 0; i < 10; i++)
{
	const char* surname = find_surname( twoD[i] );
	puts( surname );
}

Hope this helps.
Duoas, thank you for your helpful responses. I am now able to get my sort function to work correctly.
Topic archived. No new replies allowed.