help with square root function

hi,i am having a bit of problem with my code.it's a basic square root function.when i complie,i get a error for expected , or ; before '{' in line 2.

1
2
3
4
5
6
7
8
9
10
  int sqrt (t)
{
 for (int c=0;c<t ;c++)
 {
    if
     ( (t-(c*c)) <0)
return c-1 ;
  }
}



any help would be great.thanks.
On line 1 there is no type given for t. Is it a string or a char or something else?

this is the whole code.....

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include<iostream>
using namespace std;

int a,b,t ;

int sqrt (t)
 {

 for (int c=0;c<t ;c++)

 {
    if
     ( (t-(c*c)) <0)

return c-1 ;
  }
}


int main()

{
cout<< "enter the number."<<endl;

cin>>a;
b = sqrt (a) ;
cout << b << endl;
return 0;
}


i get an error on the 7th line which says there must be ',' or a ';' before the curly bracket.could you tell me why?
On line 6 there is no type given for t. Deja vu?
I don't think that's exactly how you find square roots.

It should be something more like

x0 ≈ √S
xn + 1 = ½( xn + S / xn )

Sqrt of 242

x0 ≈ √242 ≈ 15 ( I know 15[sup]2[sup] = 225 and that's fairly close ).
x1 = ½( 225 + 242 / 225 ) = 113.03777777777777777777777777778
x2 = ½( 113.03777777777777777777777777778 + 242 / 113.03777777777777777777777777778 ) = 57.589327483655633536695915055167

x3 = 30.895747489234675838506572221216
x4 = 19.364270330981247023481101692238
x5 = 15.930756876085621105208833550763
x6 = 15.560748886614439108021132012641
x7 = 15.556349808097393100216074071313
x8 = 15.556349186104057971474815560019
x9 = 15.556349186104045536818575966312


As you see...the more you do it the more accurate it gets.

When I use Windows calculator I get
15.556349186104045536818575966307

Lets compare

15.556349186104045536818575966307
15.556349186104045536818575966312


307
312

The last 3 digits are slightly off..
I'll run it one more time

x10 = 15.556349186104045536818575966307

Now they look the exact same.
I could run it more times but that's a pretty accurate number there..

So basically to make a sqrt function you must make a rough estimate to the sqrt ( it doesnt matter if you're wrong ) Technically you could use the same number as it...

I'll show

x1 = 1/2( 242 + 242/242 ) = 121.5
x2 = 1/2( 121.5 + 242/121.5 ) = 61.745884773662551440329218106996
x3 = 32.832587154147202874137650025223
x4 = 20.101656519442766890214243994976
x5 = 16.070232674624509619963012834186
x6 = 15.564565502728724782068665768247
x7 = 15.556351354743409904082972225744
x8 = 15.556349186104196696836289542334
x9 = 15.556349186104045536818575967041
x10 = 15.556349186104045536818575966307

Lets compare all 3

Calc: 15.556349186104045536818575966307
Estimate 15 after 10 runs: 15.556349186104045536818575966307
Estimate 242 after 10 runs: 15.556349186104045536818575966307

They all look the same to me.

*edit sorry took so long to post took me a while to do the math lol
Last edited on
thanks for your help guys...

@ chervil : didn't i define t in line 4,is anything else required?

@giblit : thanks man,i know what i am doing is not neat.i learnt these things but have no grab on the subject and was just trying to get my hands dirty..now that you have replied,i shall be taking your way :P

anyways,please tell me why i got that error.what's wrong and didn't i define the t type as int?


*edit: got it.so,i should have defined t in parentheses instead of globally.could anybody tell me why.thanks chervil for pointing it out. :)

*pps : @giblit : could you tell me which iteration method is that.[ n+1)th = 1/2(nth term + term/nth term)].i don't remember.thanks.
Last edited on
i should have defined t in parentheses instead of globally.could anybody tell me why

Because a function parameter is not the same thing as a global variable. Just because you define a function with a parameter that has the same name as a global variable, doesn't mean the compiler will magically give it the same type as the global variable.

When you define an entity, you need to explicitly declare its type.
that would mean for x1 n = 0
since 0 + 1 = 1 and it's xn + 1

Also your problem was when you create a function parameter you have to give it a type

example:

void function( int param );

What you're trying to do is
void function param );
with no type

The compiler wont know what param is.
is it an int , char , string , ect...?

As for the method I suggested it would look something like

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>

double sqrt( const double S , const int PRECISION )
{
	double x = S; //starting value is x0 which I will make equal to the value will use for xn + 1
	double prev_x = x; //this will be our xn and not xn+1
	
	for( int n = 1; n < PRECISION; ++n )
	{
		prev_x = x;
		x = .5 * ( prev_x + S / prev_x );
	}
	
	return( x );
}

int main()
{
	std::cout << "Sqrt of 4 precision 10: " << sqrt( 4 , 10 ) << std::endl;
	std::cout << "Sqrt 224 precision 10: " << sqrt( 224 , 10 ) << std::endl;
	std::cout << "Sqrt 121 precision 10: " << sqrt( 121 , 10 ) << std::endl;
	std::cout << "Sqrt 12.34 precision 10: " << sqrt( 12.34 , 10 ) << std::endl;
	return( 0 );
}
Sqrt of 4 precision 10: 2
Sqrt 224 precision 10: 14.9666
Sqrt 121 precision 10: 11
Sqrt 12.34 precision 10: 3.51283


Please don't copy and paste though...If you do not understand let me know
and I will see if I can help to make you understand.
thanks for your response.i thought as much.i guess what i want to know is how would defining t in those two ways be different or "why a function parameter can't be defined globally'.need to look up these things.thanks for the info guys. :)

@ giblit : i don't see what else i would need seeing you have provided all the info.thanks for this.let me try it out and i shall get back if i don't make it work.also,i actually wanted to know the name of the iteration method.(some specific name like secent method,bisection etc....is it one of them. :P)
Last edited on
why a function parameter can't be defined globally

That's like asking why a cat can't be a dog.

If an entity is defined globally, then it's a global variable, not a function parameter.

Where you define an entity is (one part of) how you specify what type of entity is it.
Last edited on
thanks giblit.

@ mikeyboy : thanks man but i think i am not able put my question properly and anyways it's not important now.wrt your analogy i would say that i need the points which differentiates a cat from a dog(from genetics pov,the cat/dog question is not so bad,is it?) i.e. i just need more info to make sense of this internal functionality/syntax/convention of the language for which i need to do some reading. or maybe i am just overanalysing :p. anyhow,it's not important but thanks bearing with me.:)
Last edited on
Topic archived. No new replies allowed.