|
I am trying to get this program to return a square-root value. The main function will call the Square_Root function that will use a do-while loop to calculated the SqRoot from the algorithm:
estimateSquareRoot = ((Number_Sent/estimateSquareRoot) +estimateSquareRoot)/2.0;
This statement will be placed in the loop that contunues to loop as long as
the square-root calculation is not within .00001 of the true value.
estimateSquareRoot*estimateSquareRoot-difference>=.00001
I can not get the loop to work. For example when I enter 9 it return 5, but
it should loop until it return the square-root of 3. If someone can point me
in the right direction please......Thank You
#include <iostream>
#include <math.h>
using namespace std;
double Square_Root (double); //declaring function returning double and passing a double
int main()
{
double Number_Enter, solution; //declaring two variables
cout<< "\n please enter a positive number."; //prompt
cin>>Number_Enter;
solution = Square_Root(Number_Enter);
cout<<"\n the square root of "<<number_enter<<"is" <<="" solution;
return="" 0;
="" }
=""
=""
double="" square_root(double="" number_sent)
{
="" double="" difference;
="" estimatesquareroot="1.0;
" do{
="" +="" estimatesquareroot)="" 2.0;
="" difference="estimateSquareRoot" *="" -="" number_sent;
="" }
while="" (estimatesquareroot*estimatesquareroot-difference="">=.00001);
return (estimateSquareRoot);
}
|
|
|
|
|
I don't know how the function was exiting with a value of 5.
Anyhoo, this line is causing the problem I think:
while (estimateSquareRoot*estimateSquareRoot-difference>=.00001);
Is always equal to the number sent in.
Change it to
while (difference >= .00001);
I'm assuming you know about the function sqrt()
Greba,
My lack of content on my home page should be entertaining.
|
|
|
|
|
You define difference as the current estimate squared minus the original square input by the user. The closer you get, the smaller the difference should be until it is less than the value .00001. In your while statement you are expecting the square of your estimated root minus the difference to get smaller. It won't converge.
For a start, change
while (estimateSquareRoot*estimateSquareRoot-difference>=.00001);
to
while(difference >= .00001);
Also, before you evaluate the difference, replace it by its absolute value. A difference of -.000009 satisfies the algorithm, but will not pass the test in the while statement.
Will Build Nuclear Missile For Food - No Target Too Small
|
|
|
|
|
Hi,
How can I draw the following 2D curve with MSChart?
double X[]= {21.4, 76.56, 32.56, 98.55};
double Y[]= {1.4, 16.56, 22.56, 18.35};
Thanks.
|
|
|
|
|
Hi
I've been trying to find a way to create a basic text editor which saves what you've entered in the text box into a text file. I encountered a few errors and problems though....
Firstly, for the user to specify the name of the file it must be of type string to be used in the file name (ofstream file(variable.c_str());). This won't work with CStrings. How could I go about converting a CString to a string?
Secondly, I try to add the "using namespace std;" statement, but no matter where on the page I add it, I still get about 4 errors....
Here's my Pages code:
<code>
#include <fstream>
#include "stdafx.h"
#include "PyreSoft Text Editor.h"
#include "PyreSoft Text EditorDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
enum { IDD = IDD_ABOUTBOX };
protected:
virtual void DoDataExchange(CDataExchange* pDX);
protected:
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()
CPyreSoftTextEditorDlg::CPyreSoftTextEditorDlg(CWnd* pParent )
: CDialog(CPyreSoftTextEditorDlg::IDD, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CPyreSoftTextEditorDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_FILENAME, m_FileName);
DDX_Control(pDX, IDC_EDIT_BOX, m_TextBoxString);
}
BEGIN_MESSAGE_MAP(CPyreSoftTextEditorDlg, CDialog)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON1, OnSave)
END_MESSAGE_MAP()
BOOL CPyreSoftTextEditorDlg::OnInitDialog()
{
CDialog::OnInitDialog();
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
SetIcon(m_hIcon, TRUE);
SetIcon(m_hIcon, FALSE);
return TRUE;
}
void CPyreSoftTextEditorDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
void CPyreSoftTextEditorDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this);
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
HCURSOR CPyreSoftTextEditorDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CPyreSoftTextEditorDlg::OnSave()
{
CString FileName;
CString MainText;
m_TextBoxString.GetWindowText( MainText );
m_FileName.GetWindowText( FileName );
CString JustChecking = FileName + ".txt";
}
</code>
|
|
|
|
|
|
Blue Pyre wrote:
#include "PyreSoft Text Editor.h"#include "PyreSoft Text EditorDlg.h"
first off
these lines are just begging for trouble
lose the spaces
"there is no spoon" biz stuff about me
|
|
|
|
|
Umm... where did you see that?
Theres no include files on the same line.
Does anyone know how I could cast the CString to a regular string?
Or how I could put the using namespace std somewhere without receiving an error?
Thanks.
|
|
|
|
|
CString objects can be casted down to LPCTSTR strings by using an (LPCTSTR) casting operator in front of the object name. You can't cast any lower than this, so if you need a stream of char -characters, you need to create the char -string manually from the LPCTSTR by using strcpy or similar.
-Antti Keskinen
----------------------------------------------
The definition of impossible is strictly dependant
on what we think is possible.
|
|
|
|
|
Hello.
I am developing an FTP client. Some FTP servers request an identity server. I am not familiar with an Identity Server protocol.
Please post any information or link. Basically I need to know the protocol from beginning to end.
- connect to FTP server
- server wait for Identity response
...
// now what?
...
...
If I am not mistaken, the FTP client needs to create a socket, bind it to the identity port set by the user, and listen for an identity request from the FTP server.
Thanks,
Kuphryn
|
|
|
|
|
This may help.....
http://www.faqs.org/rfcs/rfc2228.html
Rob.
|
|
|
|
|
|
How do i send a simple print job (using c++) of any file on my PC to a printer (among a couple others) connected to my pc?
|
|
|
|
|
Have you looked at....
http://www.codeproject.com/printing/printing_wo_docview.asp?target=print%7Cmfc
and
http://www.codeproject.com/printing/printingmadeeasy.asp?target=print%7Cmfc
|
|
|
|
|
Hello; I'm using Visual C++ 6
1)I need a way to link 225 edit boxes as one group and give each one of them an index and call them individually by their indices instead of their IDs. after that i will take their inputs to fill up a matrix 15*15.
2)the above technique i have made to make a matrix 15*15 of user defined inputs. if anyone has another elegant technique which enables me to fill a matrix 15*15 with the user inputs , i'll be glad to hear from him.
i don't want to use the excel spread sheet active X control !!
thx
Eliyah
|
|
|
|
|
Hello;
1)I need a way to link 225 edit boxes as one group and give each one of them an index and call them individually by their indices instead of their IDs. after that i will take their inputs to fill up a matrix 15*15.
2)the above technique i have made to make a matrix 15*15 of user defined inputs. if anyone has another elegant technique which enables me to fill a matrix 15*15 with the user inputs , i'll be glad to hear from him.
i don't want to use the excel spread sheet active X control !!
thx
Eliyah
|
|
|
|
|
Oriented wrote:
i don't want to use the excel spread sheet active X control !!
How about a grid control, then?
"The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)
|
|
|
|
|
Two possible approaches:
1) Make sure the IDs are in cosecutive order then access them like so
HWND hCtrl = GetDlgItem(ID_EDIT1+nIndex), or something like that.
2) Create an array containing the IDs of the controls then access them like so HWND hCtrl = GetDlgItem(idArray[nIndex]).
These are simple methods that do not require any external controls other than the edit boxes.
INTP
|
|
|
|
|
maybe you think something like this:
for(i = 0; i < 15; i++)
{
for(j = 0; j < 15; j++)
{
nIndex = i*15 + j; // calculate matrix like arrow
M[j][i] = X(ID_EDIT1 + nIndex); // fill up matrix
}
}
rgrds
|
|
|
|
|
HELP! I am having a strange problem with Win32TimerQueue based timers on Windows2000. I am creating a periodic timer of 100ms. On a single processor machine I am seeing a fairly accurate 100ms timer. However, on a DUAL PROCESSOR machine I am getting 110ms and sometimes 150ms. Does anyone know how to resolve this on DUAL PROCESSOR machines?
<br />
#include "stdafx.h"<br />
#include "stdlib.h"<br />
#include "stdio.h"<br />
#include "memory.h"<br />
#include "windows.h"<br />
#include "time.h"<br />
<br />
typedef BOOL ( CALLBACK CreateTimerQueueTimerProc ) ( PHANDLE, HANDLE, WAITORTIMERCALLBACKFUNC, PVOID, DWORD, DWORD, ULONG );<br />
typedef BOOL ( CALLBACK DeleteTimerQueueTimerProc ) ( HANDLE, HANDLE, HANDLE );<br />
<br />
<br />
VOID CALLBACK Win32TimerQueueProc(PVOID lpParameter, BOOLEAN TimerOrWaitFired)<br />
{<br />
SYSTEMTIME myWin32SystemTime;<br />
<br />
::GetSystemTime(&myWin32SystemTime);<br />
<br />
ULONG currentMilliseconds = myWin32SystemTime.wSecond * 1000 + myWin32SystemTime.wMilliseconds;<br />
<br />
SYSTEMTIME* myLastWin32SystemTime = (SYSTEMTIME*)lpParameter;<br />
<br />
ULONG lastMilliseconds = myLastWin32SystemTime->wSecond * 1000 + myLastWin32SystemTime->wMilliseconds;<br />
::printf("%d\n", currentMilliseconds - lastMilliseconds );<br />
<br />
*myLastWin32SystemTime = myWin32SystemTime;<br />
}<br />
<br />
<br />
int main( int argc, char** argv)<br />
{<br />
HMODULE hKernel32 = ::LoadLibrary("kernel32.dll");<br />
<br />
CreateTimerQueueTimerProc* pCreateTimerQueueTimerProc = (CreateTimerQueueTimerProc*) ::GetProcAddress( hKernel32, "CreateTimerQueueTimer" );<br />
<br />
if( !pCreateTimerQueueTimerProc )<br />
{<br />
::printf( "timer code will not function - system must be W2K or later" );<br />
return 1;<br />
}<br />
<br />
DWORD timeDelta = 100;<br />
<br />
SYSTEMTIME myLastWin32SystemTime;<br />
<br />
HANDLE myTimer;<br />
(*pCreateTimerQueueTimerProc)( &myTimer, <br />
NULL, <br />
Win32TimerQueueProc, <br />
&myLastWin32SystemTime, <br />
timeDelta, <br />
timeDelta, <br />
0 );<br />
<br />
::Sleep(50000);<br />
<br />
return 0;<br />
}<br />
<br />
|
|
|
|
|
From MSDN Library: "Callback functions are queued to the thread pool. These threads are subject to scheduling delays, so the timing can vary depending on what else is happening in the application or the system."
If you need a more accurate timer, look at the multimedia timer functions such as timeSetEvent .
Stability. What an interesting concept. -- Chris Maunder
|
|
|
|
|
Mike,
The multimedia timers do exactly the same thing on multiprocessor machines. When I ask for a 100ms MM timer I get about 110 to 150 ms depending. There is another insiduous bug with MM timers on MP machines and that is that after a while, on certain chipsets, the windows clock starts to DRIFT, about 10 seconds per minute! Horrible. I have some code that can demonstrate that one too!
Gavin
|
|
|
|
|
I assume you've already tried calling timeBeginPeriod with a suitable period of time?
I've just noticed how you're measuring the difference. The 'wall clock' time gets updated with a system timer interrupt and only has that resolution, which isn't nearly as accurate as the actual clock time. To accurately measure how long you're waiting, use QueryPerformanceCounter in combination with QueryPerformanceFrequency . You may see a difference.
Stability. What an interesting concept. -- Chris Maunder
|
|
|
|
|
Yes, the timeBeginPeriod is being set with suitable time. Both it and the TimerQueueTimer behave the same way.
IN the code I posted I'm using wall clock, but in my real application I am using both IRIG-B-AM time codes which should give me down to the several microsecond accuracy and the same time delta is exposed. So it is not a wall clock issue, etc.
Thanks,
Gavin
|
|
|
|
|
What is the point of function objects? Are these a leftover from the primitive days of STL? In case you haven't seen these they look like this:
class PrintInt {
public:
void operator() (int elem) const {
cout << elem << ' ';
}
};
My neighbours think I am crazy - but they don't know that I have a trampoline. All they see my head bobbing up and down over the fence every five seconds
|
|
|
|