|
Write a routine that does the following: for a given value N, create and initialize an N*N matrix. deduce the initialization algorithm from the following examples.
Example1: N=2
1 2
4 3
Example2:N=3
1 2 3
8 9 4
7 6 5
Example3: N=4
1 2 3 4
12 13 14 5
11 16 15 6
10 9 8 7
|
|
|
|
|
I doubt this is even remotely optimized, but here is a qad solution:
#define UP (UCHAR)0
#define DOWN (UCHAR)1
#define LEFT (UCHAR)2
#define RIGHT (UCHAR)3
UCHAR **Matrix( UCHAR n )
{
UCHAR **Result = new UCHAR *[n];
for ( UCHAR i = 0; i < n ; i++ )
{
Result[i] = new UCHAR[n];
memset( Result[i], 0, sizeof(UCHAR)*n );
}
UCHAR r, c;
UCHAR Dir;
UCHAR cc;
r = 0;
c = 0;
i = 1;
Dir = RIGHT;
while ( true )
{
cc = Result[c][r];
if ( cc != 0 )
{
if ( Dir == RIGHT )
{
if ( Result[c-1][r+1] == 0 )
{
Dir = DOWN;
c--;
r++;
}
else
{
break;
}
}
else
if ( Dir == DOWN )
{
if ( Result[c-1][r-1] == 0 )
{
Dir = LEFT;
c--;
r--;
}
else
{
break;
}
}
else
if ( Dir == LEFT )
{
if ( Result[c+1][r-1] == 0 )
{
Dir = UP;
r--;
c++;
}
else
{
break;
}
}
else
if ( Dir == UP )
{
if ( Result[c+1][r+1] == 0 )
{
Dir = RIGHT;
c++;
r++;
}
else
{
break;
}
}
}
Result[c][r] = i;
switch ( Dir )
{
case RIGHT:
if ( c == n - 1 )
{
r++;
Dir = DOWN;
}
else
{
c++;
}
break;
case DOWN:
if ( r == n - 1 )
{
c--;
Dir = LEFT;
}
else
{
r++;
}
break;
case LEFT:
if ( c == 0 )
{
r--;
Dir = UP;
}
else
{
c--;
}
break;
case UP:
if ( r == 0 )
{
c++;
Dir = RIGHT;
}
else
{
r--;
}
break;
}
i++;
}
for ( r = 0; r < n; r++ )
{
for ( c = 0; c < n; c++ )
{
printf( "%3u ", Result[c][r] );
}
printf( "\n" );
}
return Result;
}
int main(int argc, char* argv[])
{
Matrix( 5 );
return 0;
}
viola.
|
|
|
|
|
|
Hi I was reading Modern C++ Design by Andrei Alexandrescu. The author mentions an interesting technique in chapter 2 where you can use template specialization to select between the overloaded functions. To try it out I had written the following code
template <int v="">
struct Int2Type
{
enum { value = v };
};
enum { SimpleDispatch = 1, ComplexDispatch = 2 };
class Dispatcher
{
private:
int m_iDispatchType;
protected:
void DoDispatch(Int2Type<simpledispatch>)
{
AfxMessageBox("Simple Dispatch called");
}
void DoDispatch(Int2Type<complexdispatch>)
{
AfxMessageBox("Complex Dispatch called");
}
public:
Dispatcher() : m_iDispatchType(SimpleDispatch)
{}
Dispatcher(int iDispatchType):
m_iDispatchType(iDispatchType)
{}
void DoDispatch()
{
DoDispatch(Int2Type<m_idispatchtype>()); -------- [1]
switch(m_iDispatchType)
{
case SimpleDispatch:
DoDispatch(Int2Type<simpledispatch>());
break;
case ComplexDispatch:
DoDispatch(Int2Type<complexdispatch>());
break;
}
}
};
unfortunately unless I comment out line marked [1] the class does not compile, to make the class compile I had to use to switch alternative. So my question is why can't I write a above code. In the book the author has taken a bool variable example which is passed as a template variable.
I am missing something here? Doesn't the use of switch makes the code behave like the standard way, I mean If I have to use switch then I do not need template specialization here.
Any help is appriciated.
Thanks
|
|
|
|
|
|
Yep I am using VC++ 6.0.
Did microsoft fixed it in VC++ 7.0 or VC++ 7.1 ??
|
|
|
|
|
Hello!
I'm working on ARM embedded device. I have Flash device mapped into memory space trough which I controll it. I have the following problem:
I have to write some 32bit data to some address in memory. When I write to the location that is not modulo of number 4, I get exception;
example:
0x01000000 - ok
0x01000001 - exception
0x01000002 - exception
0x01000003 - exception
0x01000004 - ok
How to overcome this? Is it possible to write 32 bit data in atomic way and not first upper and then lower 2 bytes?
Rostfrei
-- modified at 3:30 Thursday 20th October, 2005
|
|
|
|
|
pragma pack 1 ? Haven't really thought this thru, but worth trying ~
|
|
|
|
|
Can software agent be written in VB.net to control consumption of computer energy such as monitors,i.e.putting monitor in sleep mode or stand by?
|
|
|
|
|
I create a server to listen the TCP command , such as HTTP command GET/POST etc.
However , I do not want to parse the command text.
How to know the client , such as IE, has completed transfering the command?
Thanks!
|
|
|
|
|
According to the HTTP spec, the request is finished with an empty line, so you have to wait for a double line feed.
POST is more complicated, your server has to use the return code "100 Continue" (unless the client is HTTP/1.0) and then you'll get the POST data, though many clients send it directly and don't wait for/require the 100-Continue.
|
|
|
|
|
Hello,
Can anybody help me in determining lan connectivity.
I want that whenever my lan cable is unplugged my program should be
get a notification.
With Rgds
VC++
|
|
|
|
|
WebConn error LNK2019: unresolved external symbol __imp__InternetOpenA@20 referenced in function _main
My program "WebConn" keeps getting this error when ever I use items in the wininet.h class.
I'm using Visual Studio .Net 2003, C++.
Any help would be great!
Brad
<br />
#define WINVER 0x0400<br />
#include "stdafx.h"<br />
#include <windows.h><br />
#include <wininet.h><br />
<br />
int main()<br />
{<br />
HINTERNET hInternetRoot;<br />
hInternetRoot = InternetOpen("My Agent", INTERNET_OPEN_TYPE_PROXY, "127.0.0.1", "<local>",0);<br />
<br />
return 0;<br />
}<br />
|
|
|
|
|
link Wininet.lib in project settings
|
|
|
|
|
Hi fellows
I have the jpeglib.h included in my code to do some works with images.
When I compile, the compiler show the following message:
[Link error] undefined reference to jpeg_destroy_decompress(jpeg_decompress_struct*)
There are a lot of messages like this, all of them are Link error problems.
The jpeglib.h lies in the include folder of my Dev C++ with the jerror.h, jconfig.h.
What's going on?
|
|
|
|
|
Hello,
I am making a virtual disk driver based on filedisk,
it is working very well under winxp but when i unmount the drive it doesn't disappear, instead it appears with a question mark
however it formats with all file systems under winxp.
on win 2000, it formats only under NTFS, not fat and fat32.
can u please help me with these 2 problems,
i mean the question mark under xp and the format problem under win2k
thanks in advance
Ahmed Abbas
|
|
|
|
|
Where can you create your virtual drive?
ps: This question supposted to be under "Operating Systems / SysAdmin" not in C++/CLI aka managed c++
|
|
|
|
|
Hi fellows
Someone knows how can I convert a BYTE to an integer?
Thanks
|
|
|
|
|
You should be able to use
BYTE myByte = 4;
int myInt = (int) myByte;
There is probably some newfangled way to do this, but C-style casting usually works for me.
C++ can use cooler casting operators, just google "c++ casting." But in this case C++ casting is probably unnecessary.
|
|
|
|
|
if it's a BYTE *, then int n = (int)*myBytePtr; should do it.
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
Christian Graus wrote:
if it's a BYTE *, then int n = (int)*myBytePtr; should do it.
Yeah, but it would be much better to cast to DWORD_PTR instead.
My programming blahblahblah blog. If you ever find anything useful here, please let me know to remove it.
|
|
|
|
|
not really since he's dereferencing the pointer.
but if it were this: int n = (int)myBytePtr, then yes, int n = (INT_PTR)myBytePtr may be better.
|
|
|
|
|
Hi
I want to hide an icon (of another process) from the System tray. Is it possible?
|
|
|
|
|
Hi all,
I am writing a simple application in VS.NET 2003 which uses my own dll (vz.dll). The dll is for my C++ library which the application uses. See the scenario below:
app (my own application) --> uses vz.dll (a C++ API exporting its class functions, my own classes). I am building the DLL with /MTd flag enabled rather than using flag /MDd. Also I am calling the constructor within the DLL with new.
Debugging the application, when I exit the application I do deletion of my data(calling delete on an instance created using new) I get the following exception with the stack trace
Unhandled exception at 0x7c822583 in app.exe: User breakpoint
ntdll.dll!7c87512a()
ntdll.dll!7c8754d1()
ntdll.dll!7c8409a8()
kernel32.dll!77e6b28e()
kernel32.dll!77e755b9()
vzclient.dll!_CrtIsValidHeapPointer(const void * pUserData=0x01da27a0)
Line 1807 C
vzclient.dll!_free_dbg_lk(void * pUserData=0x01da27a0, int nBlockUse=1)
Line 1132 + 0x9 C
vzclient.dll!_free_dbg(void * pUserData=0x01da27a0, int nBlockUse=1)
Line 1070 + 0xd C
vzclient.dll!operator delete(void * pUserData=0x01da27a0) Line 54 +
0x10 C++
vzclient.dll!vzTMRenderAction::`scalar deleting destructor'() +
0x3c C++
app.exe!CleanVolumizer() Line 438 + 0x34 C++
I commented out the deletion of the data which was causing this. After which I am still getting the same exception
Unhandled exception at 0x7c822583 in app.exe: User breakpoint.
But now the location of exeption has shifted to dbgheap.c when the _CrtIsValidHeapPointer returns.
Though it seems to me a heap corruption but I have no clue why it is happening and where. I also tried using gflags utility but not of much use.
Please help me, I am stuck.
Any help/ suggestion is welcome.
Thanks in advance,
Divick
|
|
|
|
|
you're probably deleting an object multiple times or the memory is being overwritten, possibly because of a dangling pointer. It could also be an uninitialized pointer. A dangling pointer is one that points to unallocated memory that was once allocated but is now being used by some other object. Later on, the program overwrites some object through the dangling pointer.
good luck.
memory problems are some of the hardest nuts to crack. but it'll put hair on your chest (LOL).
|
|
|
|