# Thread: Sorting a 3-d matrix

1. ## Sorting a 3-d matrix

How would you efficently create a 3-d matrix that asks the user for inputs (i.e. A person named X, has a book named Y, which is written by Z)?
Then the information must be sorted. What function would you use? Is it possible to sort them by the alphabet order, without functions?

Thank you.

2. Why is it a 3D matrix?
Code:
```struct data {
string name;
string title;
string author;
};

vector <data> books;```
You can use sort in algorithm to sort a vector.

3. Originally Posted by Salem
Why is it a 3D matrix?
Code:
```struct data {
string name;
string title;
string author;
};

vector <data> books;```
You can use sort in algorithm to sort a vector.
The application must be able to store several X's, Y's and Z's. Will that really work in your example?

4. Um yeah.

Code:
```struct Vertex
{
float x;
float y;
float z;

Vertex():x(0.0f),y(0.0f),z(0.0f) { }
};

Vertex *pVertices = new Vertex[500];
Vertex Vertex_array[500];

std::vector<Vertex *> ptrVertices;
std::vector<Vertex> Vertices;```
How would you efficently create a 3-d matrix....
A 3D matrix as in a matrix used for 3D or do you want a 3D array? There is no efficient way to create and use a 3D array and you should not be doing it.

A 3D matrix is easy.

Code:
```void BuildTranslationMatrix(float matrix[4][4],float dx,float dy,float dz)
{
float matrix[4][4];

matrix[0][0] = 0.0f;
matrix[0][1] = 0.0f;
matrix[0][2] = 0.0f;
matrix[0][3] = dx;

matrix[1][0] = 0.0f;
matrix[1][1] = 0.0f;
matrix[1][2] = 0.0f;
matrix[1][3] = dy;

matrix[2][0] = 0.0f;
matrix[2][1] = 0.0f;
matrix[2][2] = 0.0f;
matrix[2][3] = dz;

matrix[3][0] = 0.0f;
matrix[3][1] = 0.0f;
matrix[3][2] = 0.0f;
matrix[3][3] = 1.0f;
}```
That is a translation matrix in Direct3D. I don't think that's what you wanted

5. Originally Posted by Bubba
Um yeah.

Code:
```struct Vertex
{
float x;
float y;
float z;

Vertex():x(0.0f),y(0.0f),z(0.0f) { }
};

Vertex *pVertices = new Vertex[500];
Vertex Vertex_array[500];

std::vector<Vertex *> ptrVertices;
std::vector<Vertex> Vertices;```

A 3D matrix as in a matrix used for 3D or do you want a 3D array? There is no efficient way to create and use a 3D array and you should not be doing it.

A 3D matrix is easy.

Code:
```void BuildTranslationMatrix(float matrix[4][4],float dx,float dy,float dz)
{
float matrix[4][4];

matrix[0][0] = 0.0f;
matrix[0][1] = 0.0f;
matrix[0][2] = 0.0f;
matrix[0][3] = dx;

matrix[1][0] = 0.0f;
matrix[1][1] = 0.0f;
matrix[1][2] = 0.0f;
matrix[1][3] = dy;

matrix[2][0] = 0.0f;
matrix[2][1] = 0.0f;
matrix[2][2] = 0.0f;
matrix[2][3] = dz;

matrix[3][0] = 0.0f;
matrix[3][1] = 0.0f;
matrix[3][2] = 0.0f;
matrix[3][3] = 1.0f;
}```
That is a translation matrix in Direct3D. I don't think that's what you wanted
Thanks, but it's not going to be a 3d program in that way. It's more like a 3d array - or how would you do it?

6. I think your example is more complex than necessary, although it's obviously professional.

7. Code:
```struct data books[] = {
{ "Fred Flintstone", "Rock breaking for profit", "Mr Flint" },
{ "Wilma Flintstone, "Care of pet dinosaurs", "James Herriot" },
};```
That's an example for an array, you can do the same thing with vectors.

As for sorting, you just need to write a comparison function which orders the entries in the order you want.

8. What if you want the names to be unknown values, until the user input?

9. Well a vector is clearly superior as you can append data to a vector without having to worry about the messy detail of extending an array.

10. Originally Posted by Salem
Well a vector is clearly superior as you can append data to a vector without having to worry about the messy detail of extending an array.
If you got some time, could you please provide an example of what you said? I understand it in theory, but something like:

This is how you extend an array:
Simple example

This is how you extend a vector:
Simple example

Thank you.

11. Originally Posted by Salem
Code:
```struct data books[] = {
{ "Fred Flintstone", "Rock breaking for profit", "Mr Flint" },
{ "Wilma Flintstone, "Care of pet dinosaurs", "James Herriot" },
};```
That's an example for an array, you can do the same thing with vectors.

As for sorting, you just need to write a comparison function which orders the entries in the order you want.
So would you store data inside of the struct in the order you want, you you originally don't have the values (Fred Flintstone etc.)?
Code:
```struct data
{
int id;
string name;
string book;
string author;
};```
And so that you could later request the value of one certain string or all of the strings associated with an id using cout?
And if you have a for loop, that determines how many players there are, how would you cin all of the strings in the struct, and associate an id to them?

12. I'd rather see how far you could get on your own...

13. Originally Posted by Salem
I'd rather see how far you could get on your own...
Would you at least say how you insert values into the vector?

14. Originally Posted by Glauber
Would you at least say how you insert values into the vector?
Um, how about looking in a C++ book, or Googling for std::vector?

15. Thanks, but it's not going to be a 3d program in that way. It's more like a 3d array - or how would you do it?
You have been shown twice in code how to do it.

Popular pages Recent additions