|
|
hello @all,
i need a dynamic two-dimensional matrix. how can i do this?
thank you very much!
sunny
|
|
|
|
|
You can use malloc and realloc to do it all yourself, or use a STL vector or vectors.
Neville Franks, Author of ED for Windows. Free Trial at www.getsoft.com
|
|
|
|
|
if you just need 2d array that doesnt shrink or grow frequently then you can use two approaches, pointers to pointers or a 1d array accessed by formula.
for pointers to pointers this is how it's done:
int ** p;
p = new int * [10];
for(int i = 0; i <10; i++)
p[i] = new int[10];
for(int i = 0; i < 10; i++)
for(int j = 0; j < 10; j++)
p[i][j] = rand();
for(int i = 0; i <10; i++)
delete [] p[i];
delete [] p;
for 1d arrays, you make an array with size m * n and access elements at i,j by formula j * m + i, where m are rows and n are columns.
int * p = new int[m * n];
for(int i = 0; i < n; i++)
for(int j = 0; j < m; j++)
p[j * m + i] = rand();
delete [] p;
this approach is better since the whole array elements are compact together rather than having group of 1d arrays scattered in the memory.
if you need an array that grows and shrink frequently, then you may use template containers to make array or arrays, rather than making your own class for it.
for example, if you gonna use the STL vector container you can declare the array as follows:
vector< vector<int> > my_matrix;
my_matrix.resize(10);
for(int i = 0; i <10; i++)
my_matrix[i].resize(10);
for(i = 0; i < 10; i++)
for(int j = 0; j < 10; j++)
my_matrix[i][j] = rand();
this also can be done using the MFC's CArray using the declaration
CArray< CArray<int, int>, CArray<int, int> > my_matrix;
|
|
|
|
|
MAAK wrote:
this approach is better since the whole array elements are compact together rather than having group of 1d arrays scattered in the memory.
Does it really matter if they are scattered in the memory? My impression was quite different because the elements of the array will be accessed through indirect addressing and the general formula is [ base reg + factor *index reg + constant ]. I don't see a large overhead in computing the index. I am really opened to suggestions because I am geting very curios now.
// Afterall, I realized that even my comment lines have bugs
When one cannot invent, one must at least improve (in bed).-My latest fortune cookie
|
|
|
|
|
For people who develop high-end software.... like spreadsheets and data models. This makes a huge difference. There's all kinds of fancy algorithms to make a true dynamic and fast array.
For this case, it probably doesn't....
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~<br />
Peter Weyzen<br />
Staff Engineer<br />
Santa Cruz Networks
|
|
|
|
|
That is very interesting. Thank you very much Peter for your reply.
// Afterall, I realized that even my comment lines have bugs
When one cannot invent, one must at least improve (in bed).-My latest fortune cookie
|
|
|
|
|
I've had more than one programming job in the past working for companies that developed spreadsheets. They used a pretty fancy algorithm, based on something called "sparse matrix arrays" [You can search on the net for that, and you'll find all kinds of sites, with people posting and discussing these algorithms.]
It was all about letting you create a spreadsheet with a virtual array of large proportions, and making it both quick to access entries in the array, and make it occupy as little memory as possible.
Excel does a virtual array of 64K rows, 255 columns, and a third dimension of page accesses -- a 3D sparse array... with a possible capcity of 4,261,413,375 array entries (cells).
There's no way you can pre-allocate an array of that size... But the arrays do allow you to place data anywhere, and it works and it remains small and fast.
People get paid a lot of money to develop data structures like these... too bad it wasn't me.
-p
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~<br />
Peter Weyzen<br />
Staff Engineer<br />
Santa Cruz Networks
|
|
|
|
|
Well I think I didn't think about it correctly, cuz when I think about it again I found that the int ** seems to have better performance than the 1D array implemenation, and it may be not related to how data is near to each other.
The int ** needs 4 mov micro instruction to get the effective address, while the 1D needs 2 mov, one multiplication (which I think more costy and which make the whole overhead) and one addition operation to get the effective address.
This is the assembly code for getting the effective address in the 1D implementation for a matrix 10 x 10 to access element (i, j)
ar[j * w + i] = 5;
mov eax,dword ptr [j]
imul eax,dword ptr [w]
add eax,dword ptr [i]
mov ecx,dword ptr [ar]
mov dword ptr [ecx+eax*4],5
This is code for the int ** implementation for a matrix 10 x 10
ar[i][j] = 5;
mov eax,dword ptr [i]
mov ecx,dword ptr [ar]
mov edx,dword ptr [ecx+eax*4]
mov eax,dword ptr [j]
mov dword ptr [edx+eax*4],5
however, I found that the the 1D representation is more commonly used for example, this is how the same thing work for a static 2D array declared in the compiler:
ar[i][j] = 5;
mov eax,dword ptr [i]
imul eax,eax,28h
lea ecx,ar[eax]
mov edx,dword ptr [j]
mov dword ptr [ecx+edx*4],5
If you checked it thourougly you will find that it's similar to the 1D, but it has optimization due to previous knowledge of the array size.
[All assembly code is obtained using VC++ debugging disassembly]
This approach is useful in data serializzation since serialization can be done using single call to I/O device, bitmaps for example are stored in memory as a single 1D array.
I hope if anyone has more infomation about this issue shares it with us, cuz am more curios about it.
|
|
|
|
|
W. Richard Stevens:
Advanced Programming in the UNIX(R) Environment(my price:$30, amazon's price:$69.99)
UNIX Network Programming Volume1 2nd Ed.(my price:$30, amazon's price:$69.93)
UNIX Network Programming Volume 2 : (my price:$30, amazon's price:$66)
TCP/IP Illustrated, Volume 1:(my price:$30, amazon's price:$69.99)
TCP/IP Illustrated, Volume 2:(my price:$35, amazon's price:$69.99)
TCP/IP Illustrated, Volume 3:(my price:$30, amazon's price:$54.99)
volume 1-3(my price:$85)
Bjarne Stroustrup:
The c++ programming Language(Special Edition)(my price:$32, amazon's price:$64.99)
The Design and Evolution of c++ (my price:$20, amazon's price:$44.99)
scott meyers:
effective c++(my price:$20, amazon's price:$37.95)
effective STL(my price:$25, amazon's price:$39.99)
Andrei Alexandrescu:
Modern C++ Design(my price:$20, amazon's price:$42.99)
Brian w.Kernighan:
The Practice of Programming(my price:$20, amazon's price:$26.99)
Erich Gamma:
Design Patterns(my price:$25, amazon's price:$54.99)
Alan Shalloway:
Design Patterns Explained(my price:$25, amazon's price:$39.99)
Martin Fowler:
Refactoring(my price:$27, amazon's price:$49.99)
All of these are new.Some books are not list,if you are
interested in them,please send me an E-mail.(chester163@hotmail.com)
code,code,code...
chester163@hotmail.com
|
|
|
|
|
SPAMMER
Neville Franks, Author of ED for Windows. Free Trial at www.getsoft.com
|
|
|
|
|
|
he/she probably voted for himself.
// Afterall, I realized that even my comment lines have bugs
When one cannot invent, one must at least improve (in bed).-My latest fortune cookie
|
|
|
|
|
I have a modeless dialog box where I create all the controls myself in the WM_INITDIALOG message. My question is how to set the tab order of these controls. (Don't ask why I'm not creating them in the dialog resource )
Thanks,
Melekor
|
|
|
|
|
The tab order is determined by the Z order of the controls. You'll need to use SetWindowPos() to position the controls after one another in the desired order.
--Mike--
"So where does that leave us? Well, it leaves us right back where we started, only more confused than before." -- Matt Gullett
Ericahist | Homepage | RightClick-Encrypt | 1ClickPicGrabber
|
|
|
|
|
That sounds good, but I guess I don't know how to use it right because it's not working.
I created this
<br />
Inline void IncZOrder(HWND hWnd)<br />
{<br />
static HWND LastWindowCreated = 0;<br />
SetWindowPos(hWnd, LastWindowCreated, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);<br />
LastWindowCreated = hWnd;<br />
}<br />
now after I create every control, I call that function with its hWnd but it doesn't work, I still can't tab between the controls. I'm pretty sure my message pump is correct to handle dialogs:
<br />
while(GetMessage(&msg, NULL, 0, 0) > 0)<br />
{<br />
HWND ActiveWindow = GetActiveWindow();<br />
<br />
if(!IsWindow(ActiveWindow) || !IsDialogMessage(ActiveWindow, &msg))<br />
{<br />
TranslateMessage(&msg);<br />
DispatchMessage(&msg);<br />
}<br />
}<br />
What else do I need to do?
Thanks!
|
|
|
|
|
Are you setting the Tabstop Style for the controls you want to Tab between?
Neville Franks, Author of ED for Windows. Free Trial at www.getsoft.com
|
|
|
|
|
WS_TABSTOP! I didn't even know that style existed.
Thank you Michael and Neville, with your two suggestions together, it is now working like a charm
|
|
|
|
|
I have a problem. I see the text within a box on the screen in one font size, then when I look at the output, it wants to print it bigger than the area.
Can anyone give me a hint on how to set the printed size so if fits within the area?
|
|
|
|
|
I'm trying to create a small jukebox. Do the .wav files need to be placed in the "resources" folder or no? Also, can mp3's be used. Where are these type files normally stored?
Thanks
|
|
|
|
|
Firends i am having a problem and i like your advice in this regard. I would appreciate the non-MFC answer.
I've a thread. The purpose of this thread is to read string from somewhere and then display it on the console. Lets call it thread A. So simple, so easy.
I've two other threads. The purpose of these threads is to get some data from internet.
Now, what i want is that the two threads getting data from internet should "send" the string to thread A, so that it can be displayed. But i don't want those two threads to block. And this is the problem. Of course two threads should write string somewhere and then thread A get those strings periodically. As such critical section is necessary which i like to prevent because as such there is a blocking.
In order to overcome the problem i am using IO completion ports. But the problem is that IOCP is not available in Win9X family operating systems.
So what you like to advice me.
|
|
|
|
|
If it's simply a reader/writer paradigm, why not use a volatile string in the calling thread and write static Get/Set member functions that can be called via a pointer to the calling thread?
- Nitron
"Those that say a task is impossible shouldn't interrupt the ones who are doing it." - Chinese Proverb
|
|
|
|
|
Nitron wrote:
If it's simply a reader/writer paradigm,
No, its not simply like that. I need to implement some sort of message queue just like IO completion port.
|
|
|
|
|
One solution is a queue STL container. Pop and insert new messages as string objects.
Kuphryn
|
|
|
|
|
kuphryn wrote:
One solution is a queue STL container.
But as such i need to make this queue thread safe using critical section; otherwise two different threads may want to read and write at this object the same time.
|
|
|
|
|