|
I have written a program in c++ to check whether a number is prime or not.... I want to know if this is optimized or not..
#include<iostream>
using namespace std;
int main()
{
int num;
cout<<"Enter a number:";
cin>>num;
if(num%6==1 || num%6==5 || num==2 || num==3)
{
cout<<"\n"<<num<<" is a prime number";
}
else
{
cout<<"\n"<<num<<" is not a prime number";
}
return 0;
}
Please help me out...
|
|
|
|
|
If you start programming, lesson number one : do not ask whether something is optimized or not without having performance target in mind.
People that ask you to optimize your code without reason just want to waste your time on trivia.
Takes 1 000 000 random numbers. Then measure how much times it takes to find out whether they are prime on your machine.
Then see if you can make it better with random changes and observe.
|
|
|
|
|
I want to know that this program is better than those which are solved using loops.....????
|
|
|
|
|
only way to know it is to do what I told you just above.
This not only depends on the code but on the compiler. A coder can't tell you without benchamrking.
|
|
|
|
|
"Optimized" is not the correct category here. It is simply wrong. 25%6=1, but 25 is not prime.
|
|
|
|
|
Windows 7, VS 2008, MFC, Dialog, c++
I would like to use a control of type Net Address control to provide a method for the user to enter an IP address. Here is the closest I can get:
void CTCP_Client_ConsumerDlg::OnBnClickedButton1()
{
NC_ADDRESS y;
PNC_ADDRESS a = &y;
a->pAddrInfo = 0;
a->PortNumber = 0;
a->PrefixLength = 0;
HRESULT x;
DWORD ip_address = 0;
x = c_net_work_address_edit_box.GetAddress( a );
ip_address = c_net_work_address_edit_box.GetAddress( a );
}
Item: c_net_work_address_edit_box was created by right clicking on the address control and creating a control variable. When I type in: 192.168.2.3 the expected value is not present.
Also: The edit box does not provide the dots between the four parts of the IP address. I have seen that used many places, but want to implement in my code now.
Edit: Answer: Look in section Dialog Editor of the dialog toolbox to find IP Address Control. I don't know what the Network Address Control is for but I lack sufficient knowledge to get it working for my needs.
Thank you for your time
If you work with telemetry, please check this bulletin board: www.irigbb.com
modified 19-Oct-14 18:21pm.
|
|
|
|
|
|
I am not doing well with this. There must be something that I have completely missed and just cannot see. Here is my latest effort.
void CTCP_Client_ConsumerDlg::OnBnClickedButton1()
{
NC_ADDRESS nc_address;
PNC_ADDRESS p_nc_address = &nc_address;
p_nc_address->pAddrInfo = 0;
p_nc_address->PortNumber = 0;
p_nc_address->PrefixLength = 0;
HRESULT hr;
DWORD ip_address = 0;
DWORD &rip_address = ip_address;
hr = c_net_work_address_edit_box.GetAddress( p_nc_address );
ip_address = c_net_work_address_edit_box.GetAddress( p_nc_address );
int count = c_net_work_address_edit_box.GetAddress( rip_address );
}
I dropped a Network Address Control on the MFC dialog, then right clicked and added variable
c_net_work_address_edit_box.
The page you suggested has:
int GetAddress(
BYTE& nField0,
BYTE& nField1,
BYTE& nField2,
BYTE& nField3
);
int GetAddress(
DWORD& dwAddress
);
Which I see is the same as my last attempt. But the compiler complains:
Quote: error C2664: 'CNetAddressCtrl::GetAddress' : cannot convert parameter 1 from 'DWORD' to 'PNC_ADDRESS'
Thank you for your time
If you work with telemetry, please check this bulletin board: www.irigbb.com
|
|
|
|
|
Having thought about this some more, I think there is an easier way. You should really be using the UpdateData [^] function to exchange information between the class variables and the dialog box. You just need to initialise the variable (if necessary) before calling DoModal on the dialog. Then you (i.e. the user) fill in the values in your various boxes and click OK, and the data is transferred into the variables. If you want interim values saved by pressing some other button, then you need to call UpdateData(TRUE) in that button's click handler. You may also need to modify the data exchange routines for some complex controls, so check the documentation for the control you are using.
|
|
|
|
|
I am not understanding the reply.
Quote: Having thought about this some more, I think there is an easier way. You should really be using the UpdateData[^] function to exchange information between the class variables and the dialog box. You just need to initialise the variable (if necessary) before calling DoModal on the dialog.
The new project is an MFC dialog and the net address control has been dropped on to the dialog. Why would I be calling DoModal?
Maybe I'm barking up the wrong tree. I have seen and used many apps where I enter an IP address that display a small window with four fields of up to three digits, each field separated by dots. How is that display created and put in the dialog?
Thank you for your time
If you work with telemetry, please check this bulletin board: www.irigbb.com
|
|
|
|
|
bkelly13 wrote: Why would I be calling DoModal? If it's an MFC dialog then that is effectively how it is run. The dialog is displayed on the screen, the user fills in the data and presses OK. At this point the code in the OnOK override captures the data from the dialog window and saves it in the class object's variables by a call to UpdateData(true) .
I think you would be better with the CIPAddressCtrl Class[^], which is added to the dialog in the same way, as far as I am aware.
|
|
|
|
|
Quote: If it's an MFC dialog then that is effectively how it is run.
When I drop a control onto a dialog, there has never been a need to call DoModal for that control. I tried dropping the Net Address Control onto the dialog and it did not work out well.
EDIT
After spending a couple of hours on this I managed to get the "IP Address Control" to display in the dialog tool box. Its under the Dialog Editor section of the toolbox. Now that I can drop that on to the dialog, my extended question about manually creating the dialog is no longer needed. (That would be the long reply that I am deleting and replacing with this.) Now that the dialog is there, adding a variable and getting the address is no trouble.
So thank you for your patience.
Thank you for your time
If you work with telemetry, please check this bulletin board: www.irigbb.com
modified 19-Oct-14 18:18pm.
|
|
|
|
|
dear Sir/madam,
I am pretty much familiar about working with c. Recently, I am supposed to do a project on serial port communication between two PCs using RS232 protocol with c. I am unfamiliar with the keyword and syntax required to accompany this task..kindly help with a tutorial on it
|
|
|
|
|
|
How do you know the poster is using Microsoft's tool or platform? Plenty of other compilers and platforms, and a C developer is more likely to be working on Unix variant or embedded system these days. I'd skip the "visual".
|
|
|
|
|
I guessed Visual C because this message board contains mostly windows development related posts. If OP needs another platform then modifying the search parameters isn't difficult. RS232 is a topic for which you can find very good tutorials on any platform very easily just by googling. I think OP was simply lazy to try that before posting here.
|
|
|
|
|
Fair enough. I think it's likely OP posted in the wrong group, as neither A, W, or S TL are C libraries.
|
|
|
|
|
The syntax and APIs available depends on your operating system - Windows, OSX, Linux, Net/FreeBSD, Solaris, embedded OSs etc. First, do a search for "Serial port API" and your operating system.
As for how serial communications work, check Wikipedia[^], and pay attention to the hardware signalling and the fact that if you connect two computers, you need a "twisted" cable, and if you connect to a device, you need a "straight" one.
Oh, and this question rightly belong in the "C/C++" discussion forum rather than "ATL/WTL/STL".
|
|
|
|
|
Windows XP, Visual Studio 2008, MFC, Dialog, c++
A button in the main MFC dialog opens a second dialog with options to control the main app. In there is a button that starts disable and after a method is run, is to be enabled. The event handler that initiates that task and enables the button looks like this, so far:
void C_Prog_Control::OnBnClickBtnGetCurrentStates()
{
HWND *button_handle = NULL;
BOOL status = FALSE;
Get_Current_States();
GetDlgItem( IDC_DO_THIS, &button_handle )
status = EnableWindow( button_handle, TRUE );
}
Please forgive, but not ignore, any obvious typos as my work computer is in another room and cannot be connected to the internet. I cannot cut and paste. And now that's worse. I cannot post a message from work and must send the text home and post from there.
I have looked up the GetDlgItem() here: (When I paste in the Microsoft URL it will not display.)
And the EnableWindow function here: (Same for this link).
So the first looks like this on the MS web site:
HWND WINAPI GetDlgItem(
_In_opt_ HWND hDlg,
_In_ int nIDDlgItem
);
and the second looks like this:
BOOL WINAPI EnableWindow(
_In_ HWND hWnd,
_In_ BOOL bEnable
);
The EnableWindow code will not compile. The error is:
… error C2660: ‘CWnd::EnableWindow’ : function does not take 2 arguments.
The compiler does not see, or does not like that I want HWND::EnableWindow rather than CWnd. When I try to use CWnd there are other problems.
Intellisense shows the following signature:
BOOL EnableWindow( BOOL bEnable = 1 )
This differs from the MS page and there is only one signature.
So, given a button with the ID of: IDC_DO_THIS, how can I enable it?
EDIT RESOLVED
While at work, I stopped what I was doing several times and searched for the answer to this several times at work, through the Air Force Firewalls. After giving up there and posting this question from home, I finally found the good idea of searching from home. There in one of the first few pages was a good answer. Tried it, it worked. Just for the record:
void Cvs_2008_get_port_numberDlg::OnBnClickedButton3()
{
CWnd *pfield = GetDlgItem( IDC_BUTTON2 );
pfield->EnableWindow( FALSE );
}
My lesson, try a search at work, but if it does not turn up the answer, search again from home. I presume the Air Force, and all military sites, are attacked so often that they have little choice but to set up things the way they are.
Thank you for your time
If you work with telemetry, please check this bulletin board: www.irigbb.com
modified 8-Oct-14 22:20pm.
|
|
|
|
|
When searching MSDN for a particular Windows function you will generally find two links. One of them wil be in the form FunctionName(parameters) , and the other Classname::FunctionName(parameters) . If you are coding pure Win32 then you take the first form, and if you are using MFC you take the second. There are certain functions that do not have MFC equivalents, but that is generally clear from the context.
|
|
|
|
|
I had not noticed that, but will take the time to look for it now. It will probably help to incorporate those into my searches.
Thank you for taking the time to post that.
Thank you for your time
If you work with telemetry, please check this bulletin board: www.irigbb.com
|
|
|
|
|
Windows 7, Visual Studio 2008, MFC, C++, dialog
Making this short, after failing elsewhere I created a test project. The dialog has one edit box with the default ID of IDC_EDIT1. A button was created and the following code added for that button.
void Cvs_2008_get_port_numberDlg::OnBnClickedButton1()
{
UINT port_number;
BOOL status = FALSE;
int last_error = 0;
port_number = m_box_get_port_number.GetDlgItemInt( IDC_EDIT1, &status );
last_error = GetLastError();
}
Run the code, type in 123 in the edit box, press the button, and port_number gets zero. last_error gets value 1421, Control ID not found.
Some searching indicated that there may be a windows problem: http://www.errorfixes.net/error-1421-windows-7.php[^]
But I find myself skeptical of that.
Do I have a simple error that I can correct? Or is this a deeper problem?
Thank you for your time
If you work with telemetry, please check this bulletin board: www.irigbb.com
|
|
|
|
|
What is m_box_get_port_number in the above code? I would have expected to see something more like:
BOOL status = TRUE;
port_number = GetDlgItemInt( IDC_EDIT1, &status );
if (status = FALSE)
last_error = GetLastError();
Also, where is the button that this handler refers to: on the dialog or the main window?
|
|
|
|
|
I state up front that I am not experienced with GUIs. My tasks include VS MFS dialogs to control how the app runs and show its performance, but the app is a number cruncher and has no significant user interface. Once started there is no user interaction. The problem is probably a beginner's error so do not hesitate to ask or point out the basics.
This test app is an MFC dialog and has only one dialog. I don't know your intent in differentiating between "dialog or main window." It is only a few steps beyond a just created MFC dialog project.
So far in the test project there is one edit box, one check box, and one button. A right click on the edit box produced a drop down and "Add variable" was selected. The name used was m_box_get_port_number. I added the check for status per your code and it is returned with value 0, false. The error code remains 1421.
The one button has the event handler shown in the OP and runs that code.
Is all that of any use?
Thank you for your time
If you work with telemetry, please check this bulletin board: www.irigbb.com
|
|
|
|
|
If I understand what you are saying you have an MFC application that is a dialog; that's fairly standard. Within the dialog are a few controls, one of which is an edit box. You then say, "A right click on the edit box produced a drop down and "Add variable" was selected.". That part I don't understand, an edit box is just that, a box that you type text into which allows simple editing. What I would expect to happen would be that you would type something into the edit box, and then click the button. Your button handler would be something like:
void Cvs_2008_get_port_numberDlg::OnBnClickedButton1()
{
UINT port_number;
BOOL status;
int last_error = 0;
port_number = GetDlgItemInt( IDC_EDIT1, &status );
if (status == FALSE)
last_error = GetLastError();
When you prefix your call to GetDlgItemInt with m_box_get_port_number (the edit box variable name) you are trying to find the item IDC_EDIT1 within the edit control. But the edit control does not have a child window with that ID so the call fails.
|
|
|
|