gashtio 25 Light Poster

Ok and the code OP showed should be similarly proved right? Like so :

Summation from i = 1 to N
Summation from j = x, where x = 2^i

Thus N * Summation from j = x, where x = 2^i = {1,2,4,8,16...}

If you look on wiki_summation. In the growth rate section it tells you that: Summation from i to N of c^i = THETA(c^n).
In our case c = 2, thus Summation from i to N of 2^i = THETA(2^n);

Thus N * Summation from j = x, where x = 2^i
= N * THETA(2^n)
= THETA(n*2^n)

"Summation from i to N of c^i" is exactly what the code does, you're confusing the second loop with something that it is not :). Maybe you're considering "Summation from j = x, where x = 2^i" as O(2^N) and summing that N times, which does indeed result in O(N * 2^N), but a better bound can be found. Here's a picture that might clear things up:

myk45 commented: Nice Explanation +1
gashtio 25 Light Poster

You can use BinaryFormatter 's Serialize and Deserialize methods instead of StreamWriter / StreamReader . Here's a little sample I wrote (with no error checking at all):

Receiver

static void Main(string[] args)
{
    TcpListener listener = new TcpListener(IPAddress.Any, 5005);
    listener.Start();
    Console.WriteLine("Server started.");

    Socket client = listener.AcceptSocket();
    Console.WriteLine("Accepted client {0}.\n", client.RemoteEndPoint);

    List<string> l = null;

    using (NetworkStream ns = new NetworkStream(client))
    {
        BinaryFormatter bf = new BinaryFormatter();
        l = (List<string>)bf.Deserialize(ns);
    }

    if (l != null)
        foreach (var item in l)
            Console.WriteLine(item);

    client.Close();
    listener.Stop();
}

Sender

static void Main(string[] args)
{
    TcpClient client = new TcpClient();
    client.Connect(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 5005));
    Console.WriteLine("Connected.");

    List<string> l = new List<string>() { "Lorem", "ipsum", "dolor", "sit", "amet" };

    BinaryFormatter bf = new BinaryFormatter();
    NetworkStream ns = client.GetStream();
    bf.Serialize(ns, l);
    Console.WriteLine("Data sent.");

    ns.Close();
    client.Close();
}
gashtio 25 Light Poster

The problem lies in the loop filling the bitmap data.

You can make a simple calculation of how many bytes you are writing - 3 each iteration, width*height/3 iterations - a total of width*height bytes, not pixels. Essentially, you're writing to every third pixel, hence the diagonal lines.

To correct the problem you should just replace the j+=3 in the loop with j++ .

hanvyj commented: Thanks for taking the time to spot my mistake! +1
gashtio 25 Light Poster

You have to process the WM_CTLCOLORSTATIC message in the parent window (WM_CTLCOLOREDIT should you change the edit box style so it is neither disabled nor read-only). It is sent by the control to its parent before being drawn, passing its device context in the WPARAM, so you can change text/background color in the message handler.

The code would be something like this:

case WM_CTLCOLORSTATIC:
{
	HDC hEdit = (HDC)wParam;

	SetTextColor( hEdit, RGB(0, 0, 0) );
	SetBkColor  ( hEdit, RGB(255, 255, 255) );

	// Do not return a brush created by CreateSolidBrush(...) because you'll get a memory leak
	return (INT_PTR)GetStockObject( WHITE_BRUSH );
}

The return value is the brush used to paint the background. Also, the MSDN documentation is incorrect, the brush is not deleted by the system (at least not on Vista/7) so if you need a custom color brush, create it once and delete it when the window closes.

Bear in mind that the system overrides the text color of disabled controls, so you wouldn't be able to change it this way. Instead, you can specify ES_READONLY when creating the edit box (the text will be selectable, though).

Graphix commented: Thanks for the solution! +1
gashtio 25 Light Poster

It's always nice to see people posting tutorials, but I have to disagree with both pieces you posted.

You need not do any of these things, because that's the compiler's job - making the machine code optimal. Precalculating constants and rearranging expressions in weird ways only makes code hard to read and follow. You should write code that is easy to maintain and leave optimizations to the compiler - it usually does a better job than you would anyway :).

Here are some codes and the pertinent parts of the disassembly using MSVC 2008 and /O2 for optimization:

#include <cstdio>
#define _USE_MATH_DEFINES
#include <cmath>

double DegToRad( const double& lfDeg )
{
	return lfDeg * (M_PI / 180.0);
}

int main()
{
	double lfDeg = 0.0;

	scanf( "%lf", &lfDeg );
	double lfRad = DegToRad( lfDeg );

	printf( "%lf\n", cos( lfRad ) );

	return 0;
}
double lfRad = DegToRad( lfDeg );
00BF1023  fld         qword ptr [esp+40h]				// push lfDeg on top of the FPU stack
00BF1027  fmul        qword ptr [__real@3f91df46a2529d39 (0BF2110h)] 	// multiply top of FPU stack with 0.017453... (0x3f91df46a2529d39 in memory)

The compiler premultiplied M_PI / 180.0 and inlined the function without even hinting.

...
int a, b, c, d;
scanf( "%d %d %d %d", &a, &b, &c, &d );

int res = a * b + a * c + a * d;

printf( "%d\n", res );
...
00CC1022  mov         ecx,dword ptr [esp+14h] 	// load b into ecx
00CC1026  mov         edx,dword ptr [esp+18h] …
invisal commented: Thank for your effort of posting ASM code. It is very useful. +6
jonsca commented: Good point(s) +4
gashtio 25 Light Poster

You can call ISpVoice::Speak asynchronously and then use the ISpVoice::WaitUntilDone method, which does exactly what you want.

pVoice->Speak( szText, SPF_ASYNC, NULL );
pVoice->WaitUntilDone( 3000 ); // 3 seconds
donaldw commented: Awesome! Thanks so much! Exactly what I needed. +1
gashtio 25 Light Poster

The problem lies here HashTable = new (nothrow) ListSet [size]; .

Short explanation:

You are allocating an array of ListSet objects and handing the returned pointer to a Set * . This is not how inheritance works - you should allocate an array of Set pointers instead, and then allocate a ListSet object for each pointer. In code, that is:

int nSize = ...;
Base ** ppStuff = new Base * [nSize];
for( int i = 0; i < nSize; ++i )
	ppStuff[i] = new Derived();

...

for( int i = 0; i < nSize; ++i )
	delete ppStuff[i];
delete [] ppStuff;

Long explanation:

Every class that has virtual functions has a static virtual table - that's essentially an array of function pointers, one for each virtual function. Hidden from the programmer, the compiler adds a member variable - a pointer to that v-table. So your Set class actually looks something like this (in pseudocode):

class Set
{
public:
	typedef string T;
	virtual void Add(const T& key) = 0;
	virtual bool Contains(const T& key) = 0;
	virtual void Print() = 0;
	virtual void Remove(const T& key) = 0;

	function_ptr * __vfptr; // a pointer to a function pointer
	static function_ptr __vftable[] = { &Set::Add, &Set::Contains, &Set::Print, &Set::Remove };
	__vfptr = &Set::__vftable[0];
};

and the ListSet class:

class ListSet : public Set
{
	...

	static function_ptr __vftable[] = { &ListSet::Add, &ListSet::Contains, &ListSet::Print, &ListSet::Remove }; // this table is different from the Set one
	__vfptr = …
DarkT commented: solved my problem :) +0
gashtio 25 Light Poster

You can use one of the Marshal::StringToHGlobalAnsi/Auto/Uni methods to copy the string into unmanaged memory, and then clean up with Marshal::FreeHGlobal when you're done.

There's an example here that does exactly that.

inisca commented: Thank you +rep +0
gashtio 25 Light Poster

Assuming 2^n takes O(1) time, the complexity is O(log* n), that is, the iterated logarithm of n.

Rashakil Fol commented: yes +7
gashtio 25 Light Poster

This happens because StreamReader::ReadLine returns nullptr when the end of stream is reached (which is the case when reading an empty file).

That means that on line 8 of your code line is now nullptr , and not the initial empty string. Since they do not compare equal to each other, the second if statement is entered, and because nullptr does not have a string representation, nothing is printed between the two 2's.

To observe the correct behavior, change the last part to:

if ( String::IsNullOrEmpty( line ) )
{
	MessageBox::Show("1" + line + "1");
}
else
{
	MessageBox::Show("2" + line + "2");
}
jonsca commented: Good +4
gashtio 25 Light Poster

There are quite a few things wrong :).

- All the "mdim_ = mdim_;" stuff is useless.

- In the Matrix(int cols) constructor, mdim_ remains uninitialized and you're getting a crash - you should set it to cols.

- The SetElement() function is incorrect, it should read:

void Matrix::SetElement(int m, int n, double d)
{
	data_[(m - 1) * ndim_ + n - 1] = d;
}

- In WriteToFile(), the correct formula is:

myfile << data_[i * ndim_ + j] << " ";

The idea is that you've processed i rows, each having ndim_ columns, plus j more columns from the current row.

Also, you need to open the file just once, not in each innermost iteration.

And you should probably do some error checking when adding (are the matrices the same size), setting element (is it within bounds), etc.