Hi. I'm obviously new to C++. I thank you in advance for your help. I know this is a very simple program but I can't figure out what I'm doing wrong. A user will input a number in order to find the square root of the floating point number. I have to keep track of the iterations and print out the square root and the number of iterations it took. I'm also trying to implement a pass by reference. Could someone please point me in the right direction?
#include <iostream>
#include <cmath>
#include <stdio.h> /* printf */
usingnamespace std;
constdouble EPSILON = 1e-6;
void squareroot(double, int& );
int main ( )
{
double A;
int cycles = 0;
cout << "Let's compute the square root of any number you choose!" << endl;
cout << "Make sure the number you choose is a positive number!" << endl;
cout << "Enter your number: ";
cin >> A;
double answer = squareroot(A, cycles);
cout << endl << "It took " << cycles << " times." << endl;
return 0;
}
double squareroot(double A, int &c)
{
// put you code here including that c = iter or just change 'iter' to 'c' everywhere.
return xnew;
}
Thanks kemort. The comment you have for line 31, was that referring to my code for the lines that I had at 27 - 39? If so, I received the errors below. If not, then I used the wrong part of my code. :(
wishonp2x.cpp: In function ‘int main()’:
wishonp2x.cpp:23:32: error: void value not ignored as it ought to be
wishonp2x.cpp: In function ‘double sqrt(double, int&)’:
wishonp2x.cpp:30:29: error: new declaration ‘double sqrt(double, int&)’
wishonp2x.cpp:9:6: error: ambiguates old declaration ‘void sqrt(double, int&)’
I've changed my code and the first part of Main runs (data input and data validation) but I can't get what I thought was the function to run. I understand the concept of passing by reference...I just can't completely understand the logic yet.
// This program will compute the square root of a given floating point number.
using namespace std;
void sqrt(double, int&);
int main ( )
{
double A;
int cycles = 0;
cout << "Let's compute the square root of any number you choose!" << en$
cout << "Make sure the number you choose is a positive number!" << endl;
cout << "Enter your number: " << endl;
cin >> A;
while (A <= 0)
{
cout << "Invalid Input, must be greater than 0" << endl;
cout << "Enter your number: " << endl;
cin >> A;
return A;
}
{
const double EPSILON = 1e-10;
double sqrt(double A, int &c);
double xnew = A;
double xold;
int c = 0;
while (fabs(xnew - xold) > EPSILON && c < 100);
{
++c;
xold = xnew;
xnew = (xold + A / xold) / 2.0;
}
cout << endl << "The sqare root is: ";
cout << fixed << showpoint << setprecision << xnew << endl;
cout << endl << "It took " << c << " times." << endl;
Wishon, the layout of your code needs to be very similar to mine. You have two SEPARATE functions - main() and squareroot(....)
Show us the code that produced the errors and please use the code tags. You also need to tidy up your formatting - proper indents, remove unnecessary blank lines etc. at the moment it's far too messy.
Hi kemort. After I compile the code and run it, I enter the number I wish to find a square root for and then nothing. I don't think it's caught in an infinite loop but I don't know. I enter '8' below and it ends line and I can enter another number or anything. Any ideas? I appreciate your help!
Let's compute the square root of any number you choose!
Make sure the nmber you choose is a positive number!
Enter your number: 8
what are you doing with a ';' in the wrong place somewhere between lines 30 an 46 inclusive. Look VERY carefully, line by line, and you'll see why you have an infinite loop.
oh yeah...I forgot to paste those. haha. It took a bit but it finally compiled. I had to do some research about the errors I was getting and things finally fell into place. But I appreciate your input and direction. It helped tremendously.