 1 - 20 of 183 Posts

·
##### Registered
Joined
·
4,732 Posts
Discussion Starter
A continuance from my Java tirade.

I am having issues with my C++.

It's another "search the web for your answers" class. WTF am I going to school for? /*
Newton-Raphson Method
convert from supplied Java format to C++ format
*/

#include <iostream>
#include <cmath>

using namespace std;

class Lab1
{
public:
double getValue();
double getInitialGuess( double value );
double approximateSqrt( double value );
};

/*
getValue method asks user for value greater than zero.
method also checks to verify value is greater than zero.
*/
double Lab1::getValue()
{
double value;
cout<<"Enter value (>0): "<<endl;
cin>>value;
while( value <= 0 )
{
cout<<"Invalid value "<<value<<" <= 0 -- try again"<<endl;
cout<<"Enter value (>0): "<<endl;
cin>>value;
}
return value;
} // end getValue method

/*
getInitialGuess method calculates value for
approximateSqrt method.
*/
double Lab1::getInitialGuess( double value )
{
return ( value / 2.0 );
} // end getInitialGuess method

/*
approximateSqrt method approximates the sqare root
of the entered value.
*/
double Lab1::approximateSqrt( double value )
{
const double epsilon = 0.00001;
double x = getInitialGuess( value );
printf( "sqrt( %1.6f ) = %1.6f\n", value, x );
while ( abs( value - x * x ) > epsilon )
{
double tx = x;
x = 0.5 * ( x + ( value / x ));
printf( "sqrt( %1.6f ) = 0.5 * ( %1.6f + ( %1.6f / %1.6f ) ) = %1.6f\n", value, tx, value, tx, x );
}
return x;
} // end approximateSqrt method

int main()
{
cout<<"Enter value whos square root will be computed"<<endl;
double x = getValue();
double xSqrt = approximateSqrt( x );
printf("\n sqrt( %1.6f ) = %1.6f \n", x, xSqrt);
return 0;
} // end main

Problem is, I have no experience with C++. I don't think/know if the way I declared my methods is correct. TA has had two classes so far which were over the meanings of ++, --, && and || :shock: Stuff I actually knew from two quarters ago.

This is supposed to be due for lab on Monday.

The formulas are correct. The TA gave us the Java code. We just have to convert it into C++.

Errors I am getting:

#### Apathetic Frimbo

·
##### Registered
Joined
·
672 Posts
I'm not sure why you created a class called Lab1, but try the thing below. If that doesn't work and you don't need the class Lab1, I can tell you how to fix it.

You need to create a Lab1 in your main.

Lab1 lab1;
cout<<"Enter value whos square root will be computed"<<endl;
double x = lab1.getValue();
double xSqrt = lab1.approximateSqrt(x);
printf("\n sqrt( %1.6f ) = %1.6f \n", x, xSqrt);
return 0;

·
##### Registered
Joined
·
4,732 Posts
Discussion Starter
I created the class because that is what I could decipher from my programming book.

And of course... your design works. What is the other way if you don't mind?

#### agm_ultimatex

·
##### Banned
Joined
·
13,838 Posts
you can just use functions and call them from the main() function, rather than use a class and its methods. I don't guarentee the following will work, as I havent tried it myself yet.

/*
Newton-Raphson Method
convert from supplied Java format to C++ format
*/

#include <iostream>
#include <cmath>

using namespace std;

//function declarations
double getValue();
double getInitialGuess( double value );
double approximateSqrt( double value );

/*
getValue function asks user for value greater than zero.
function also checks to verify value is greater than zero.
*/
double getValue()
{
double value;
cout<<"Enter value (>0): "<<endl;
cin>>value;
while( value <= 0 )
{
cout<<"Invalid value "<<value<<" <= 0 -- try again"<<endl;
cout<<"Enter value (>0): "<<endl;
cin>>value;
}
return value;
} // end getValue function
/*
getInitialGuess method calculates value for
approximateSqrt function.
*/
double getInitialGuess( double value )
{
return ( value / 2.0 );
} // end getInitialGuess method

/*
approximateSqrt function approximates the sqare root
of the entered value.
*/
double approximateSqrt( double value )
{
const double epsilon = 0.00001;
double x = getInitialGuess( value );
printf( "sqrt( %1.6f ) = %1.6f\n", value, x );
while ( abs( value - x * x ) > epsilon )
{
double tx = x;
x = 0.5 * ( x + ( value / x ));
printf( "sqrt( %1.6f ) = 0.5 * ( %1.6f + ( %1.6f / %1.6f ) ) = %1.6f\n", value, tx, value, tx, x );
}
return x;
} // end approximateSqrt function
int main()
{
cout<<"Enter value whos square root will be computed"<<endl;
double x = getValue();
double xSqrt = approximateSqrt( x );
printf("\n sqrt( %1.6f ) = %1.6f \n", x, xSqrt);
return 0;
} // end main

#### Apathetic Frimbo

·
##### Registered
Joined
·
672 Posts
Shoot I was still typing...

Try that. What's different from java is that if you want to call a method like in main, you need to create the method above your main (or whatever method you called it from). or you could put the name of the method, double getValue();, with a semi colon below your include statements. This allows you to use the method no matter the order you wrote them in.

You can move cout<<"Enter value whos square root will be computed"<<endl; up in your getValue() method.

/*
Newton-Raphson Method
convert from supplied Java format to C++ format
*/

#include <iostream>
#include <cmath>
//double getValue();
using namespace std;

/*
getValue method asks user for value greater than zero.
method also checks to verify value is greater than zero.
*/
double getValue()
{
double value;
cout<<"Enter value (>0): "<<endl;
cin>>value;
while( value <= 0 )
{
cout<<"Invalid value "<<value<<" <= 0 -- try again"<<endl;
cout<<"Enter value (>0): "<<endl;
cin>>value;
}
return value;
} // end getValue method

/*
getInitialGuess method calculates value for
approximateSqrt method.
*/
double getInitialGuess( double value )
{
return ( value / 2.0 );
} // end getInitialGuess method

/*
approximateSqrt method approximates the sqare root
of the entered value.
*/
double approximateSqrt( double value )
{
const double epsilon = 0.00001;
double x = getInitialGuess( value );
printf( "sqrt( %1.6f ) = %1.6f\n", value, x );
while ( abs( value - x * x ) > epsilon )
{
double tx = x;
x = 0.5 * ( x + ( value / x ));
printf( "sqrt( %1.6f ) = 0.5 * ( %1.6f + ( %1.6f / %1.6f ) ) = %1.6f\n", value, tx, value, tx, x );
}
return x;
} // end approximateSqrt method

int main()
{
cout<<"Enter value whos square root will be computed"<<endl;
double x = getValue();
double xSqrt = approximateSqrt( x );
printf("\n sqrt( %1.6f ) = %1.6f \n", x, xSqrt);
return 0;
} // end main

#### Apathetic Frimbo

·
##### Registered
Joined
·
672 Posts
Yes and no. Since the functions are above main and his call to getInitialGuess() is after the method getInititialGuess doesn't matter. However if you put the method getInitialGuess() after approximateSqrt() yes you would need to declare the functions like you did.

#### agm_ultimatex

·
##### Banned
Joined
·
13,838 Posts
Yes and no. Since the functions are above main and his call to getInitialGuess() is after the method getInititialGuess doesn't matter. However if you put the method getInitialGuess() after approximateSqrt() yes you would need to declare the functions like you did.
[/quote]

ah okay, probably just one of those practices i learned. I am by no c++ expert. Once i get my desktop up and running, ill continue finishing the book i have from class, and working on a java applet. I wanna try to build a pong game using the SDL or something too.

·
##### Registered
Joined
·
4,732 Posts
Discussion Starter
Ah... I got it now.

Problem is my "C++" book I picked up some time ago is really a C+ book with some C++ functions. :/

All the simplifying stuff isn't covered in the book (using namespace std; etc)

I got rid of the Class Lab1 stuff and it worked.

Then I read more comments and decided to try the other suggestion so I commented out the
/*
double getValue();
double getInitialGuess( double value );
double approximateSqrt( double value );
*/

and it still works, so I don't know WTH I was doing wrong the first half dozen tries. As far as layout... We're supposed to copy it directly from Java, so I'll leave the cout where it is. In my own code, I would probably do something like you suggested.

#### bearda

·
##### Registered
Joined
·
2,270 Posts
Im sure some will come once he gets to *shudder* pointers.
[/quote]

It ain't real C or C++ unless it's got pointers....

#### agm_ultimatex

·
##### Banned
Joined
·
13,838 Posts
Im sure some will come once he gets to *shudder* pointers.
[/quote]

It ain't real C or C++ unless it's got pointers....
[/quote]

With C, C strings.

·
##### Registered
Joined
·
4,732 Posts
Discussion Starter
Quit it you guys... you're scaring me. #### agm_ultimatex

·
##### Banned
Joined
·
13,838 Posts
Quit it you guys... you're scaring me. [/quote]
hehe, all in good fun. Don't worry, its not so bad. BTW googling is really heart of finding things out with programming. When I went for the co op interview that im working at, I was asked "If you get stumped on a part of a program what do you do?" I simply answered that I will usually try to figure it out, but if it's causing too much trouble, I will for say in class ask my teacher, another student, or search on the internet. I said in the instance of the work place as either one of them, or google it. They said they usually resort to google when something strange comes up.

·
##### Registered
Joined
·
4,732 Posts
Discussion Starter
Quit it you guys... you're scaring me. [/quote]
hehe, all in good fun. Don't worry, its not so bad. BTW googling is really heart of finding things out with programming. When I went for the co op interview that im working at, I was asked "If you get stumped on a part of a program what do you do?" I simply answered that I will usually try to figure it out, but if it's causing too much trouble, I will for say in class ask my teacher, another student, or search on the internet. I said in the instance of the work place as either one of them, or google it. They said they usually resort to google when something strange comes up.
[/quote]

Yeah that was my TA's answer to "learning" CS 241 class. :/

Glad I paid my college \$1000 for that "lesson". /sarcasm

In CS 240 the TA actually TAUGHT us the proper way to code and if we had a question he would go into as much detail as he could without giving away a program.

Like I said... I haven't even been shown how to create/declare the methods. Well, Lab1 is done... time for Project 1. :?

IMHO, Trial and error is NOT a good way to learn a new program language.

#### bearda

·
##### Registered
Joined
·
2,270 Posts
IMHO, Trial and error is NOT a good way to learn a new program language.
[/quote]

It's not all that bad. Try learning 8051 assembly on an embedded system with a software-controlled power supply. Lot of smoke involved on that project....

·
##### Registered
Joined
·
4,732 Posts
Discussion Starter
LOL... we just started learning pointers.

·
Joined
·
13,838 Posts