Sorting a 3-d matrix

This is a discussion on Sorting a 3-d matrix within the C++ Programming forums, part of the General Programming Boards category; How would you efficently create a 3-d matrix that asks the user for inputs (i.e. A person named X, has ...

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    62

    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. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,555
    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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    62
    Quote Originally Posted by Salem View Post
    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. #4
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    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
    Last edited by VirtualAce; 05-25-2008 at 07:43 AM.

  5. #5
    Registered User
    Join Date
    May 2008
    Posts
    62
    Quote Originally Posted by Bubba View Post
    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. #6
    Registered User
    Join Date
    May 2008
    Posts
    62
    I think your example is more complex than necessary, although it's obviously professional.

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,555
    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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Registered User
    Join Date
    May 2008
    Posts
    62
    What if you want the names to be unknown values, until the user input?

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,555
    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 dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  10. #10
    Registered User
    Join Date
    May 2008
    Posts
    62
    Quote Originally Posted by Salem View Post
    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. #11
    Registered User
    Join Date
    May 2008
    Posts
    62
    Quote Originally Posted by Salem View Post
    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?
    Last edited by Glauber; 05-25-2008 at 10:31 AM.

  12. #12
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,555
    I'd rather see how far you could get on your own...
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  13. #13
    Registered User
    Join Date
    May 2008
    Posts
    62
    Quote Originally Posted by Salem View Post
    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. #14
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Quote Originally Posted by Glauber View Post
    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. #15
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    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.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C - access violation
    By uber in forum C Programming
    Replies: 2
    Last Post: 07-08-2009, 01:30 PM
  2. Matrix Help
    By HelpmeMark in forum C++ Programming
    Replies: 27
    Last Post: 03-06-2008, 04:57 PM
  3. What is a matrix's purpose in OpenGL
    By jimboob in forum Game Programming
    Replies: 5
    Last Post: 11-13-2004, 11:19 PM
  4. Matrix and vector operations on computers
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 05-11-2004, 06:36 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21