   Convert Matrix into a String
  Hello everyone, I'm somewhat new to the GameDev forums, but I had a problem with a function I was writing last week, and the help I got was so good that I want to discuss my current function as well.  :)I'm trying to write a function to convert a matrix of integers, into a string where each entry is separated by a comma.void matrixtostring (int* matrix, int rows, int columns, char* buffer) {...}So for example, if I had a matrix as follows:[  10  11  12  13  ][  14  15  16  17  ][  18  19  20  21  ][  22  23  24  25  ]It would return the string 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25.  Or if there is a more natural order to store a matrix as a string, I'm fine with that too (like 10, 14, 18, 22, 11, 15, 19, ...).So like, my gameplan for something like this is really to just create some counter variables and increment them to rows and/or columns as appropriate to get the integer stored at that point.My initial hurdles come in the form of the following:1. How exactly should I access the int* matrix?  I'm not exactly sure how to pull individual numbers from there.2. Converting the ints to chars and storing them seems like it may be challenging... but I'm not sure.  I figure if 'currentint' represents the currently pulled integer.  If I:for(...)  *buffer++ = "0123456789abcdef"[currentint];  *buffer++ = ',';  *buffer++ = ' '; //a space?  Not sure how to do this either.orfor(...; i++)  buffer = "0123456789abcdef"[currentint];  Would this work?And then I could just output them accordingly.return buffer;or cout << buffer;for(...; i++)  cout << ", " << buffer[i+1];Anyway, input from you guys would be great.  :)   
  Since you're using C++ (or at least appear to be), why not use the streaming capabilities of the standard C++ library?Generally, in C++ we would represent a matrix or other similar structure using a class or struct, rather than passing the data around using raw pointers. Sticking with your example though, here's one way to do what you're asking (no error checking, and not compiled or tested):#include <sstream>#include <string>std::string matrixtostring (int* matrix, int rows, int columns){std::ostringstream s;int numElements = rows * columns;for (int i = 0; i < numElements; ++i) {s << matrix;if (i < numElements - 1) {s << ", ";}}return s.str();}As for how to access the individual matrix elements, it depends on how the matrix data is represented, as well as (possibly) whether the data is stored in row-major or column-major order.   ;Quote:Original post by AnujSuper9So for example, if I had a matrix as follows:[  10  11  12  13  ][  14  15  16  17  ][  18  19  20  21  ][  22  23  24  25  ]If you ask me, this seems like a much more natural way to represent a matrix than a line of values.  For instance, you just used it to demonstrate what matrix you were referencing, hehe [wink]   ;  Hmm, your method seems good jyk, but the problem is that I'm not sure how the elements are being entered into the string.Ideally I'd like to be able to control the order in which the elements are being added to the string.Is there a good way to do it without using streaming?  Streaming seems like a great way to do it, but I'm not sure it allows the control I want over the string.Anyway, hmm, I'll see if I can work out a function a bit.But does anyone else have any advice as far as the two hurdle points I listed?Thanks!   ;Quote:Original post by AnujSuper9Hmm, your method seems good jyk, but the problem is that I'm not sure how the elements are being entered into the string.They're put in the string in the order that you're pushing them into the stream. You have full control over that. Have you tried his approach already?   ;Quote:Original post by AnujSuper9Hmm, your method seems good jyk, but the problem is that I'm not sure how the elements are being entered into the string.Ideally I'd like to be able to control the order in which the elements are being added to the string.Is there a good way to do it without using streaming?  Streaming seems like a great way to do it, but I'm not sure it allows the control I want over the string.Anyway, hmm, I'll see if I can work out a function a bit.But does anyone else have any advice as far as the two hurdle points I listed?Thanks!Captain P already covered it, but you have just as much or more control using streaming as with other methods - you just have to become familiar with how the SC++L stream classes work (it can be a little non-intuitive at first).This is a pretty good reference, I think, although it might be a little overwhelming if you're new to the material.Like Captain P said, I'd try the method I posted and see what happens. The key things to look at are the << operator (in this case), and the >> operator (which you don't need for this particular task, but will want to become familiar with eventually).   ;Quote:Original post by AnujSuper91. How exactly should I access the int* matrix?  I'm not exactly sure how to pull individual numbers from there.As jyk's example shows you can use the subscript, [], operator and treat it like an array - this works because arrays and 'pointers to memory ranges' are very similar things.If you tell us how you're storing a matrix then there are other things you can do too, for example if you're using a 2-dimensional array then you can use double subscripts, [row][col], but you will have to use a template argument for the type:template <class MatT>std::string matrixtostring(MatT matrix, int rows, int columns);This is so the compiler can infer the type (which involves knowing the number of columns) for you at compile time.You could also be using a matrix class which overloads operator()(i, j), in which case simply pass that through to the function so that elements can be accessed as my_mat(i, j), this is the solution I would use personally.Quote:2. Converting the ints to chars and storing them seems like it may be challenging... but I'm not sure.C++ streams are designed to handle the otherwise icky conversions for you, a string stream in particular will append the values to an internal string object which is convenient for you since you can just return that string, as in jyk's example.Quote:Hmm, your method seems good jyk, but the problem is that I'm not sure how the elements are being entered into the string.CaptainP explained one side to that question, the other aspect is that jyk's example writes numbers in the order that they are layed out in memory, which is a well defined thing but it changes depending on how you're storing them (one or two dimensional array?) and whether you're using row major or column major ordering for your matrix.   ;Quote:Original post by jykSince you're using C++ (or at least appear to be), why not use the streaming capabilities of the standard C++ library?Generally, in C++ we would represent a matrix or other similar structure using a class or struct, rather than passing the data around using raw pointers. Sticking with your example though, here's one way to do what you're asking (no error checking, and not compiled or tested):Or a variation on that:#include <sstream>#include <string>#include <iterator>#include <algorithm>std::string matrixtostring (int* matrix, int rows, int columns){std::ostringstream s;int* last = matrix + (rows * columns) - 1;std::copy(matrix, last, std::ostream_iterator<int>(s, ", "));s << last;return s.str();};Quote:Or a variation on that:*** Source Snippet Removed ***Indeed (I was trying to break him in easy ;)   ;  Sorry I am replying so late, I could not make it to the computer lab.So, I'm wanting to make sure I output with my char* buffer.  Is it possible to steam the strings into the buffer, or move the strings from the stream to the buffer somehow?  (I haven't yet read the link you provided jyk, but I will be doing so right away after this post.)Additionally, I'd really like to treat the matrix as a two dimensional array, but to do this I would have to change it from int* to MatT?  I'm not sure I understand.  Is there a way to ensure within the function that it will be a two dimensional array if passing in int* matrix?Also, let's assume it's row-major sorting order because I think that is simpler for me.I mean, the reason I'm asking about control of the individual matrix entries is because, what if I want to grab specific entries in the matrix, and output only those to the string.For example:[  10  11  12  13  ][  14  15  16  17  ][  18  19  20  21  ][  22  23  24  25  ]Say I want to go, left to right, shift down, right to left, shift down... etc., like: "10, 11, 12, 13, 17, 16, 15, 14, 18, 19, ..."Or just grab the "x1" "y2" "z3" and "w4" if you know what I mean, as in: "10, 15, 20, 25"You know what I mean?Sorry if I am being confusing.  Thank you very much for your help.  :)   