|
I'm trying to code a function that checks whether a number is a prime number.
When trying to implement a known algorithm...I'm getting unexpected results.
I know it's not managed c++, but I would appreciate help. Any ideas pls?
#pragma once
#include <iostream>
#include <math.h>
#include <stdio.h>
using namespace std;
bool isPrime (int Number)
{
/* hard-setting prime numbers */
if (Number <= 1) return false;
/* Algorithm: Sieve of Erastosthenes*/
int *mPrimeNumber;
mPrimeNumber = new int[Number + 1];
int iIndex;
/* initialize values */
for (iIndex = 0; iIndex <= Number; iIndex++)
{mPrimeNumber[iIndex] = 0;}
iIndex = 2;
mPrimeNumber[iIndex] = 1;
for (iIndex = iIndex * iIndex; iIndex <= Number; iIndex += 2)
{mPrimeNumber[iIndex] = 2;}
iIndex = 2;
while (iIndex <=Number)
{ iIndex++;
if (mPrimeNumber[iIndex]==2)
{mPrimeNumber[iIndex] = 1;
for (int iInnerIndex = iIndex; iInnerIndex <= Number; iInnerIndex += iIndex)
{mPrimeNumber[iIndex]= 2;}
}
}
// set return value
switch (mPrimeNumber[Number])
{ case 1:
return true;
break;
case 2:
return false;
break;
case 0:
cout << "Error!: " << Number << " not checked!!" << endl;
break;
default:
cout << "Error!: Invalid return value"<< endl;
}
delete [] mPrimeNumber;
};
void main ()
{
int iNumber = 0;
while(iNumber != 9999)
{
cout << "Enter an integer:" << endl;
cin >> iNumber;
// test isPrime function
if (isPrime(iNumber) !=0)
{cout << "Prime" << endl;}
else
{cout << "Not-Prime" << endl;}
}
}
Jon
|
|
|
|
|
I have an NT Service program written with VC++ 6.0 as ATL/COM components that runs on W2K or XP. Now I want to upgrade to VC++ 2005 but not sure where to start. It has nothing at all to do with the web, just processes database requests from clients on wireless scanning devices. The wireless device needs some data from a database, calls my service which processes the request, formats the data for the device then returns the formatted data all through various COM calls.
I guess my question is: is there any advantage to migrating to VC++ 2005 other than getting a new compiler? Are there any books you would recomment that might help me migrate the program to managed c++?
Thanks for any inputs.
|
|
|
|
|
You can still compile it as native code, VC8 doesn't force you to convert it all to managed.
|
|
|
|
|
Is there anyone who would be so kind to tell me where can I get some sorce code about a software which can read or edit the powerpoint file? Many thanks!
thinkpader
|
|
|
|
|
Hi !
I would like to ask your help in a problem I got stuck completely for weeks now.
I need to check and clear the Enhance pointer precision checkbox whitin Mouse settings under Control Panel of XP.
Could somebody help me please ?
Many thanks in advance,
Moore
|
|
|
|
|
Programatically?
Best,
Jun
|
|
|
|
|
Of course programtically, yes. Manually with mouse it is easy, but I would need to do it from source code from an application.
Any idea how ?
Many thanks,
Moore
-- modified at 17:52 Friday 30th June, 2006
|
|
|
|
|
hi, i am tyring to use OpenCV code in VS .net 2003 Visual C++ Windows forms.
when i build, it give me no error but when it start it give an error :
An unhandled exception of type 'System.TypeLoadException' occurred in cam1.exe
Additional information: Could not load type CvCapture from assembly cam1, Version=1.0.2371.20291, Culture=neutral, PublicKeyToken=null.
The error seen to be from this line:
capture = cvCaptureFromCAM(0);
new to programming
-- modified at 3:01 Thursday 29th June, 2006
|
|
|
|
|
|
Hey eveybody,
I have written a WndProc() function that detects the insert and removal of a USB drive and
want to pull out the specific LParam values to get the name of the drive and other info.
The examples I've found so far use a particular C# code that goes like this...
<br />
[StructLayout(LayoutKind.Sequential)]<br />
public struct DEV_BROADCAST_VOLUME<br />
{<br />
public int dbcv_size;<br />
public int dbcv_devicetype;<br />
public int dbcv_reserved;<br />
public int dbcv_unitmask;<br />
}<br />
protected override void WndProc(ref Message m)<br />
{<br />
const int WM_DEVICECHANGE = 0x0219;<br />
const int DBT_DEVICEARRIVAL = 0x8000;
const int DBT_DEVTYP_VOLUME = 0x00000002;
if (m.Msg == WM_DEVICECHANGE && m.WParam.ToInt32() == DBT_DEVICEARRIVAL)<br />
{<br />
int devType = Marshal.ReadInt32(m.LParam,4);<br />
if(devType == DBT_DEVTYP_VOLUME)<br />
{<br />
DEV_BROADCAST_VOLUME vol;<br />
vol = (DEV_BROADCAST_VOLUME) Marshal.PtrToStructure(m.LParam,typeof(DEV_BROADCAST_VOLUME));<br />
MessageBox.Show(vol.dbcv_unitmask.ToString("x"));<br />
}<br />
}<br />
base.WndProc (ref m);<br />
}<br />
And I've been trying to get the LParam into my structure like this:
<br />
__gc struct Volume<br />
{<br />
int size;<br />
int devicetype;<br />
int reserved;<br />
int unitmask;<br />
}<br />
<br />
protected: void WndProc(System::Windows::Forms::Message* message) <br />
{<br />
...<br />
if(message->Msg == WM_DEVICECHANGE)<br />
{<br />
if(message->WParam == DBT_DEVICEARRIVAL)<br />
{<br />
lVolume = (Volume*)Marshal::PtrToStructure(message->LParam,__typeof(Volume));<br />
<br />
String* str = MaskToLogicalPaths(lVolume->Mask);<br />
this->m_FoundBox->Text = String::Format("Plugged In: {0}",str);<br />
}<br />
else <br />
if(message->WParam == DBT_DEVICEREMOVECOMPLETE)<br />
{<br />
this->m_FoundBox->Text = String::Format("UnPlugged: {0}",message->WParam.ToString());<br />
} <br />
}<br />
}<br />
This continues to not work in MC++.
For some reason I cannot find a viable alternative to the C# code in MC++. My errors include
the dreaded run-time:
"The specified structure must be blittable or have layout information..."
among other more standard compile errors for instantiating the structure incorrectly.
I know theres a way to do this but havent found it yet.
Any help you all could be would be greatly appreciated!
ScottLeff
sleff@yahoo.com
|
|
|
|
|
How to apply "Force file output" option in Visual Studio.NET i.e. VC7. In Visual Studio 6.0, it is available in "Link-->Customize" option in Project Settings of VC++ project.
Can any one help me out please
|
|
|
|
|
Hi All,
In C++/CLI I have used a 2 Dimensional String Array.
My code:
array<String^,2> ^strarr=gcnew array<String^,2>(2,2){"focus1","focus2","focus3","focus4",};
and when I want a string to print on console
ie
Console::Writeline("{0}",strarr[0][0]);
compiler is giving Error.
can any one suggest
Bye..
G.Nagaraju
|
|
|
|
|
Hi,
if want to directly initialize your array then you'll have to do it like this:
<code>
array<String^,2>^ strarr = gcnew array<String^,2>{{"focus1","focus2"},{"focus3","focus4"}};
Console::WriteLine("{0}",strarr[0,0]);</code> In additon I would recommend you to read this article.
http://www.codeproject.com/managedcpp/cppcliarrays.asp[^]
best regards Tobias
|
|
|
|
|
hi.I am using visual studio .net 2003 and I am new to managed c++.
I have to write joystick controller(mouse stick)but I dont now where to start.
|
|
|
|
|
What do you mean by a joystick controller? To me the hardware is the controller and the software is the driver and your program communicates with this driver by an api.
I assume you need to have your program use the joystick support that is built in the operating system? DirectInput (part of directx) has support for joysticks.
John
|
|
|
|
|
I have to create a GUI which control the camera.in my GUI i need to create a mouse stick which controls the camera like rotate left or right. but i get stuck on the mouse stick.In order to create this mouse stick where should i start.
|
|
|
|
|
|
thank you very much for the article.I almost finish qui part of my project
Mert Aydin
|
|
|
|
|
Hey guys, I am trying to understand some codes and came across this.
In the code below, I don't know if its a pointer to RTIambassador object. I don't see why there is RTI:: in front of RTIambassador* and also ms_rtiAmb is not a method of class ObjectRoot. I don't see why they have ObjectRoot:: in front of ms_rtiAmb.
Thanks,
Jay
RTI::RTIambassador* ObjectRoot::ms_rtiAmb = NULL;
|
|
|
|
|
don't cross post the forums
we already answered you on the other board... can't you just wait for an answer more than 5 minutes ??
TOXCCT >>> GEII power
[VisualCalc 3.0 updated ][Flags Beginner's Guide new! ]
|
|
|
|
|
|
Hi,
I've got an application that uses mixed C++.
Sometimes, the application crashes with AccessViolationException
It seems that the cause is that I'm using unmanaged pointers without pinning.
Here is the code that crashes sometimes
<br />
__gc class CRichTextBuffer<br />
{<br />
public:<br />
CRichTextBuffer()<br />
{<br />
m_BufferSize = 32;<br />
m_BufferPosition = 0;<br />
m_buf = new char[m_BufferSize];<br />
}<br />
<br />
public:<br />
bool CreateCDParagraph(CDPABDEFINITION __gc *Definition)<br />
{<br />
Int32 nSize;<br />
bool bResult = false;<br />
int nStep = 0;<br />
<br />
try<br />
{<br />
nSize = ODSLength(_CDPABDEFINITION); nStep++;<br />
<br />
<br />
if ( CheckSize(nSize) ) <br />
{<br />
LPTSTR LPStrResult = new TCHAR[nSize]; nStep++;<br />
LPTSTR LPStrTmp = LPStrResult;
nStep++;<br />
<br />
CDPABDEFINITION Def;<br />
CopyStructure( &Def, Definition ); nStep++;<br />
<br />
ODSWriteMemory( &LPStrTmp, 10, &Def, 1 ); nStep++;<br />
<br />
CopyMemoryInBuffer(LPStrResult, nSize); nStep++;<br />
delete [] LPStrResult; nStep++;<br />
<br />
bResult = true;<br />
}<br />
}<br />
catch (Exception* e)<br />
{<br />
String *sMsg = String::Concat("Exception: ", e->get_Message()," - Step=", nStep.ToString());<br />
System::Diagnostics::Debug::Assert(false, sMsg);<br />
bResult = false;<br />
}<br />
<br />
return bResult;<br />
}<br />
<br />
protected:<br />
char *m_buf;<br />
int m_BufferPosition;<br />
int m_BufferSize;<br />
}<br />
I'm new with __pin pointers
where is the problem? where need I use __pin pointers?
I know that I can't __pin the class member m_buf. How can I prevent the application from crash?
Thanks in advance for your help
Stf
|
|
|
|
|
I've got an unmanaged C++ library which I've now compiled as a managed assembly (changed the output type etc.) When I try to use this in another Managed C++ project, I get C2871 - a namespace with this name does not exist. I have tried using both:
#using (with the Resolve #using references setting)
Adding a reference (automatic /FU switch)
but neither works; I still get C2871 on the first "using namespace" line.
The really strange thing is that I have built a test c# application and this is able to use the assembly just fine!
Any clues anyone?
Mark
|
|
|
|
|
if u r sure that u can use the assembly from C#, no doubt u should be able to use the same from managed cpp as well.
To confirm the namesapce/class names u used in managed cpp:
* chk it in c#, by press ctrl+alt+j and see the namespace details in the object browser.
* or use ildasm.exe to di assemble the assembly and see the namespace type informations.
cheers...milton kb
|
|
|
|
|
Thanks for the advice. The strange thing is, I've already checked this.
ildasm.exe shows the namespace just as I'm trying to use it and C# uses it without a problem. The Managed C++ compiler picks up the dll from the #using statement but can't seem to find the namespace in a subsequent "using namespace ..." statement.
I've tried using a different dll in the project and this works OK. I've also tried compiling the included dll in a complete new project - same problem. So it seems to be something wrong with this particular dll. I just don't understand how the C# compiler can see the namespace whereas the C++ one can't.
Ideally I want to get the C++ compiler to verbosely tell me exactly what it's searching and what it 'sees'. I guess that's not possible!
Mark.
|
|
|
|