|
anonymous wrote:
it's part of my project
Recently CP has been attracting a lot of project help requests.
Nish
Nish was here, now Nish has gone;
He left his soul, to turn you on;
Those who knew Nish, knew him well;
Those who didn't, can go to hell.
I like to on the Code Project
Sonork ID 100.9786 voidmain
www.busterboy.org
|
|
|
|
|
If insertion sort is what I think it is, you insert an element into a sorted array at the right position so that the resulting array is again sorted. If you start with an array with zero elements, simply insert it at position 1. From now on for each element added, compare with each existing element till you find one that is equal or greater than it in sort order and insert it there.
Nish
Nish was here, now Nish has gone;
He left his soul, to turn you on;
Those who knew Nish, knew him well;
Those who didn't, can go to hell.
I like to on the Code Project
Sonork ID 100.9786 voidmain
www.busterboy.org
|
|
|
|
|
Well, if you absolutely have to use arrays, then the basic idea would be:
1) If your array has n items in it:
2) Search the array for the position to insert the new item, call it i
3) Create a new array of size (n+1)
4) Copy items 0..(i-1) from the old array to positions 0..(i-1) in the new array
5) Copy items i..(n-1) from the old array to positions (i+1)..n in the new array
6) Copy the new item into position i in the new array.
As Nish pointed out, this is not a really good way to do it, you should check and see if you can use a linked list, as it will perform much better.
------------------------
Derek Waters
derek@lj-oz.com
|
|
|
|
|
And the other easy method of sorting is a bubble sort. this is efficient for small lists, but sh!te for big ones. But it's simple and should score big points in class.
Search the web for the theory and have a go at implementing it (rather than just me telling you)
Sorry to dissapoint you all with my lack of a witty or poignant signature.
|
|
|
|
|
Don't forget about good ole Mr. qsort(). He loves to sort arrays. qsort() is part of the standard C library. Here is a little console application that shows qsort's useage. It will sort an array of objects and an array of pointers to objects.
#include "stdafx.h"
#include "stdlib.h"
#include "search.h"
#include "conio.h"
class Obj
{
public:
int nValue;
};
typedef int (*compfn)(const void*, const void*);
class MyClass
{
public:
Obj m_objList1[10];
int m_nList1Size;
Obj *m_pobjList2;
int m_nList2Size;
void Sort1()
{
qsort(m_objList1, m_nList1Size, sizeof(Obj), (compfn)Compare);
}
void Sort2()
{
qsort(m_pobjList2, m_nList2Size, sizeof(Obj*), (compfn)Compare);
}
private:
static int Compare(const Obj *a, const Obj *b)
{
if(a->nValue < b->nValue)
return -1;
else
if(a->nValue > b->nValue)
return 1;
else
return 0;
}
};
int main(int argc, char* argv[])
{
MyClass data;
int i;
data.m_objList1[0].nValue = 4;
data.m_objList1[1].nValue = 3;
data.m_objList1[2].nValue = 1;
data.m_objList1[3].nValue = 5;
data.m_objList1[4].nValue = 2;
data.m_nList1Size = 5;
data.Sort1();
for(i = 0; i < data.m_nList1Size; i++)
{
printf("%ld\n", data.m_objList1[i].nValue);
}
printf("%\n");
data.m_pobjList2 = new Obj[5];
data.m_nList2Size = 5;
data.m_pobjList2[0].nValue = 4;
data.m_pobjList2[1].nValue = 3;
data.m_pobjList2[2].nValue = 1;
data.m_pobjList2[3].nValue = 5;
data.m_pobjList2[4].nValue = 2;
data.Sort2();
for(i = 0; i < data.m_nList2Size; i++)
{
printf("%ld\n", data.m_pobjList2[i].nValue);
}
delete [] data.m_pobjList2;
_getch();
return 0;
}
Note if the objects you are sorting are big, sorting the array of pointers may be faster. Only the pointer values get moved around not the large objects. This also keeps you from have to write copy operators for the objects being sorted.
If you need to sort on more that one value in the object you can do something like this:
int compare(const void *a, const void *b)
{
if(a->value1 < b->value1)
return -1;
else
if(a->value1 > b->value1)
return 1;
else
{
if(b->value2 > b->value2)
return -1;
if(b->value2 < b->value2)
return 1;
else
return 0;
}
}
Hope this helps...
Jonathan Craig
MCW Technologies, Inc.
www.mcw-tech.com
|
|
|
|
|
Is anyone familiar with the error below? I am not sure what I am doing wrong, just trying to call a function compiled into a .dll in C++ from VB.
Thanks again.
I've created what I believe should be the method to do this:
1. Created a Win32 Dynamic Linked Library
a. Choose a simple .dll project
2. Wrote the function inside the header file.
3. Compiled the .dll ok and exited VC++
Launched VB
1. Added a module
2. Included the following code for the module
Declare Function SumValue Lib "sum.dll" (ByVal a As Integer) As Integer
3. Put a button on a form where I try to call the function SumValue
4. Get an error:
Run-time error '453' Can't find DLL entry point SumValue in sum.dll
Does anyone have a clue?
Thanks again
Nick Parker
Nick Parker
|
|
|
|
|
Have you declared your function like this?
extern "C" __declspec(dllexport) int WINAPI SumValue(int); If not, you might want to give it a try to see if this fixes your problem.
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
I use Visual C++ 6.0 Professional with Visual Studio. In the Dialog design
tool I would like to put a raised bump line or a sunken line grove onto
the dialog. The problem is that for reasons unknown to me there is no
LINE tool on the Dialog Controls Palette Tool bar. Can anyone tell me
how to design a line into my dialog at design time? Then also is there
some way to add a line tool to the Dialog Controls Tool Bar?
Thanks
Mike
|
|
|
|
|
THis bevel line control should do what you need:
http://www.codeproject.com/staticctrl/rotated_bevel.asp
Roger Allen
Sonork 100.10016
If I'm not breathing, I'm either dead or holding my breath.
A fool jabbers, while a wise man listens. But is he so wise to listen to the fool?
Please step to the rear of the car and make room for more victims. - John Simmons the Outlaw programmer, 1st Feb 2002, in the lounge
|
|
|
|
|
Thankyou very much Roger,,,,I will take a look at it.
|
|
|
|
|
Add a "Picture" control from the resource editor's Controls pallette. Set its "Type" to "Frame" and its "Color" to "Etched". To create a horizontal line, resize the control so that its height is zero; to create a vertical control, set its width to zero.
/ravi
"There is always one more bug..."
ravib@ravib.com
http://www.ravib.com
|
|
|
|
|
Ravi Bhavnani wrote:
To create a horizontal line, resize the control so that its height is zero; to create a vertical control, set its width to zero
you couldn't set it to zero. Instead, you can set it to one. I think that is what you want to say.
|
|
|
|
|
Picky, picky. Yes.
/ravi
"There is always one more bug..."
ravib@ravib.com
http://www.ravib.com
|
|
|
|
|
Ravi
Use this [ ] instead of pinky pinky
Nish
Nish was here, now Nish has gone;
He left his soul, to turn you on;
Those who knew Nish, knew him well;
Those who didn't, can go to hell.
I like to on the Code Project
Sonork ID 100.9786 voidmain
www.busterboy.org
|
|
|
|
|
This is a great idea Ravi. I like this solution because it can be totally handled at design time and requires no code to be added. After all who wants to have to add more code to draw a line on a Dialog Box.....
I still miss the old Borland BC3.1 C C++ compiler from 10 years ago for programming with Win 3.1 & WFW. Their RESOURCE WORKSHOP tool had a nice line control with six separate styles to pick from....Why today does MS not support the lowly line????
Mike
|
|
|
|
|
Yes, I remember Borland's RW and OWL!
/ravi
"There is always one more bug..."
http://www.ravib.com
ravib@ravib.com
|
|
|
|
|
I get the following error trying to map a network drive. Obviously it's a conversion issue. How do I convert a CString to the proper type that is needed?
:ERROR C2679:
"binary '=' : no operator defined which takes a right-hand operand of type 'class CString' (or there is no acceptable conversion)"
//Begin Source
CString m_strResource;
NETRESOURCE resource;
resource.dwType = RESOURCETYPE_ANY;
resource.lpRemoteName = m_strResource; // Doesn't work
resource.lpRemoteName = "\\\\Server\\Share"; // Works
resource.lpProvider = NULL;
DWORD result = WNetAddConnection2(&resource, m_strPassword, m_strUsername, 0);
//End Source
|
|
|
|
|
The problem is that resource.lpRemoteName is a LPTSTR , and CString casts only to LPCTSTR (note the additional C for "const" ). In this particular case, you know the conversion from LPCTSTR to LPTSTR is safe, as WNetAddConnection2 (hopefully) does not change the NETRESOURCE passed in, so you can force the casting with
resource.lpRemoteName = const_cast<LPTSTR>(LPCTSTR(m_strResource))
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
Hello,
I assume that by the time we get from :
CString m_strResource;
to :
resource.lpRemoteName = m_strResource;
"m_strResource" would have been set to a valid string value.
Now, from the documentation of the "NETRESOURCE" struct, the field "lpRemoteName" is LPTSTR. Thus, you need to :
1. First cast "m_strResource" to an LPCTSTR.
2. Next cast the resultant "(LPCTSTR)m_strResource" to an LPTSTR.
For example :
resource.lpRemoteName = (LPTSTR)((LPCTSTR)m_strResource);
The casting from a CString to an LPCTSTR will be successful because CString has a "LPCTSTR" casting operator.
The casting from the resultant "(LPCTSTR)m_strResource" to an LPTSTR will be successful because it is a normal casting operation in C/C++ albeit not something to be encouraged.
Please give it a try and let us know how things turn out. Good luck !
Best Regards,
Bio.
|
|
|
|
|
Thanks for the help - it's now working with the following statement.
resource.lpRemoteName = const_cast<lptstr>((LPCTSTR)m_strResource);
The program reads a text file line by line contains drive mappings. I then pass the line as a CString to a function. The function then extracts the Drive letter and Resource name from the CString and then passes those values to NETRESOURCE and creates the connections.
It was mention that this type of casting should not be encouraged... what would be the proper way of performing this?
|
|
|
|
|
It was mention that this type of casting should not be encouraged... what would be the proper way of performing this?
This is the proper way --it is not your fault, but that of the MS guy who defined NETRESOURCE and WNetAddConnection2 witthout caring about const ness.
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
See the VC forum FAQ.
--Mike--
"There are only a limited number of jobs where they will ask to see the sausage. Most of them are in movies."
-- Christian Graus, 2/11/2002
My really out-of-date homepage
Sonork - 100.10414 AcidHelm
Big fan of Alyson Hannigan.
|
|
|
|
|
Hello,
I am trying to figure out if it is possible to display 256 colors in a SDI tool bar button.
Any ideas?
Rob
|
|
|
|
|
Apparently, regular CToolBar s support more than 16 colors. Check Kirk Stowell's article Using Hot Toolbar Buttons, where he discusses this issue (among other things).
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
It depends on your image list. When you create your CImageList use the ILC_COLOR8 flag for 256 colors. You can even use ILC_COLOR24 for a true color bitmap.
Nish
Nish was here, now Nish has gone;
He left his soul, to turn you on;
Those who knew Nish, knew him well;
Those who didn't, can go to hell.
I like to on the Code Project
Sonork ID 100.9786 voidmain
www.busterboy.org
|
|
|
|