# Thread: return value for protype

1. ## return value for protype

i was writing a programm to find the square root of a number.first i thought to find how many iteration it goes.I got stuck int he middle please help me out.Here is my code.Where did i go wrong
Code:
```#include <iostream>
using namespace std;
double epsilon( double , double, double );
int main (void)
{
//int n,i;
int count;
double a[count],subject,epsilon;//,newepsilon;
cout<<"Please input a postive real number:";
cin>>subject;
cout<<"Please input a postive real value for the relative accuracy epsilon:";
cin>>epsilon;
cout<<"count="<<count(subject,a[],epsilon);

double count( double subject,double x[count], double epsilon);
{   x[0]=subject/2.0;
int n=0;
int count=0;
x[n+1]=((x[n] +(subject/x[n]))/2);
newepsilon=((x[n+1]-x[n])/x[n+1]);
while(epsilon > newspilon)
{
count=count+1;
x[n+1]=((x[n] +(subject/x[n]))/2);
newepsilon=((x[n+1]-x[n])/x[n+1]);
}
return count;}

}```

2. At first, a typical structure of a program is like this
Code:
```#include <iostream>

using namespace std;

bool myFunc(int a);

int main()
{

int a = 0;

return 0;
}

bool myFunc(int a)
{
if(a == 9)
return true;
return false;
}```
You have the definition of the function inside the main, which is wrong.

3. i corrected it,but still having troubles.
Code:
```#include <iostream>
using namespace std;
double epsilon( double , double, double );
int main (void)
{
//int n,i;
int ncount,count;
double a[ncount],subject,epsilon;//,newepsilon;
cout<<"Please input a postive real number:";
cin>>subject;
cout<<"Please input a postive real value for the relative accuracy epsilon:";
cin>>epsilon;
ncount=count(subject,a[ncount],epsilon);
cout<<"count="<<ncount;
}
double count( double subject,double x[count], double epsilon);
{   x[0]=subject/2.0;
int n=0;
int count=0;
x[n+1]=((x[n] +(subject/x[n]))/2);
newepsilon=((x[n+1]-x[n])/x[n+1]);
while(epsilon > newspilon)
{
count=count+1;
x[n+1]=((x[n] +(subject/x[n]))/2);
newepsilon=((x[n+1]-x[n])/x[n+1]);
}
return count;}

}```

4. I think you have an extra closing bracket at the end of your code.

Maybe you can improve the readability of your code by applying some rules of indentation. Actually is a must.

5. yes i will try to do it.i have following errors
recently i edited my code as
Code:
```#include <iostream>
using namespace std;
double epsilon( double , double, double );
int main (void)
{
//int n,i;
int ncount,count;
double a[ncount],subject,epsilon;//,newepsilon;
cout<<"Please input a postive real number:";
cin>>subject;
cout<<"Please input a postive real value for the relative accuracy epsilon:";
cin>>epsilon;
ncount= count (subject, a, epsilon);
cout<<"count="<<ncount;
}
double count ( double subject,double x[], double epsilon);
{   x[0]=subject/2.0;
int n=0;
int count=0;
x[n+1]=((x[n] +(subject/x[n]))/2);
newepsilon=((x[n+1]-x[n])/x[n+1]);
while(epsilon > newspilon)
{
count=count+1;
x[n+1]=((x[n] +(subject/x[n]))/2);
newepsilon=((x[n+1]-x[n])/x[n+1]);
}
return count;}```
but still having these errors
Compiling: C:\Users\Documents\1.cpp
C:\Users\Documents\1.cpp: In function 'int main()':
C:\Users\Documents\1.cpp:13: error: 'count' cannot be used as a function
C:\Users\Documents\1.cpp: At global scope:
C:\Users\Documents\1.cpp:17: error: expected unqualified-id before '{' token
Process terminated with status 1 (0 minutes, 1 seconds)
2 errors, 0 warnings

6. The name of your function differs between the prototype and declaration. And you have a semicolon after the declaration that should not be there.

Code:
```double epsilon( double , double, double );

double count( double subject,double x[count], double epsilon);  // <--- no semicolon```

7. thanks alot matticus.could u please look at this error
Code:
```#include <iostream>
using namespace std;
double count( double , double, double );
int main (void)
{
//int n,i;
int ncount,count;
double a[ncount],subject,epsilon;//,newepsilon;
cout<<"Please input a postive real number:";
cin>>subject;
cout<<"Please input a postive real value for the relative accuracy epsilon:";
cin>>epsilon;
ncount= count (subject, a, epsilon);
cout<<"count="<<ncount;
}
double count ( double subject,double x[], double epsilon)
{   x[0]=subject/2.0;
int n=0;
int count=0;
double newepsilon;
x[n+1]=((x[n] +(subject/x[n]))/2);
newepsilon=((x[n+1]-x[n])/x[n+1]);
while(epsilon > newepsilon)
{
count=count+1;
x[n+1]=((x[n] +(subject/x[n]))/2);
newepsilon=((x[n+1]-x[n])/x[n+1]);
}
return count;}```
Compiling: C:\Users\Documents\1.cpp
C:\Users\Documents\1.cpp: In function 'int main()':
C:\Users\Documents\1.cpp:13: error: 'count' cannot be used as a function
Process terminated with status 1 (0 minutes, 0 seconds)
1 errors, 0 warnings

8. I would suggest you to give different name to variable count from the name of the function.

Originally Posted by Matticus

EDIT :
But wait, in main count is not really a variable...So replace the line
Code:
`int ncount,count;`
with this
Code:
`int ncount;`
Also you are not giving a value for the length of the array.

Also the second argument in the prototype should be double* , because you pass an array as argument....

Then cope with run time erros

9. plz check out the edit.i hope i did the indentation for better reading.ya the whole question was not to define the length of the array at first .user inputs the value for the square root and epsilon.We have to find how many times it will iterate until newepsilon value is less than epsilon value.

Code:
```#include <iostream>
using namespace std;
int zahl( double , double, double );
int main (void)
{

int ncount;
double a[ncount];
double subject,epsilon;//,newepsilon;
cout<<"Please input a postive real number:";
cin>>subject;
cout<<"Please input a postive real value for the relative accuracy epsilon:";
cin>>epsilon;
ncount = zahl (subject, a, epsilon);
cout<<"count="<<ncount;
}
int zahl ( double subject,double x[], double epsilon)
{
x[0]=subject/2.0;
int n=0;
int count=0;
double newepsilon;
x[n+1]=((x[n] +(subject/x[n]))/2);
newepsilon=((x[n+1]-x[n])/x[n+1]);
while(epsilon > newepsilon)
{
count=count+1;
x[n+1]=((x[n] +(subject/x[n]))/2);
newepsilon=((x[n+1]-x[n])/x[n+1]);
}
return count;
}```

Compiling: C:\Users\Documents\1.cpp
C:\Users\Documents\1.cpp: In function 'int main()':
C:\Users\Documents\1.cpp:14: error: cannot convert 'double*' to 'double' for argument '2' to 'int zahl(double, double, double)'
Process terminated with status 1 (0 minutes, 1 seconds)
1 errors, 0 warnings

10. Indentation improved

Maybe you did not see my edit before.It said -> Also the second argument in the prototype should be double* , because you pass an array as argument....

But if ncout is unknown to your program , how is your code suppose to set the size of the array?
First you have to give value to the ncount and then create the array

11. do u mean double * new epsilon; is that do with pointers.then can i do newepsilon calculations.i have studied earlier that pointer holds the address of the pointing variable.so will it hold the calculated double value?
This way my idea to tackle it.
* create double a[ncount]; (which i dont know the value.)
**do the calculations and find out how many times it iterate.
***then put the value to the array.(then i though that could my create desired length of array).
correct me..plz

12. Before main you have
Code:
`int zahl( double , double, double );`
After main you have
Code:
`int zahl ( double subject,double x[], double epsilon)`
Does the seem same to you?The second argument needs to be an array...So or have as second argument double* x or double x[]

13. guys thanks.but it s crashing now.either way x[] or *x
Code:
```#include <iostream>
using namespace std;
int zahl( double , double x[], double );
int main (void)
{

int ncount;
double a[ncount];
double subject,epsilon;//,newepsilon;
cout<<"Please input a postive real number:";
cin>>subject;
cout<<"Please input a postive real value for the relative accuracy epsilon:";
cin>>epsilon;
ncount = zahl (subject, a, epsilon);
cout<<"count="<<ncount;
}
int zahl ( double subject,double x[], double epsilon)
{
x[0]=subject/2.0;
int n=0;
int count=0;
double newepsilon;
x[n+1]=((x[n] +(subject/x[n]))/2);
newepsilon=((x[n+1]-x[n])/x[n+1]);
while(epsilon > newepsilon)
{
count=count+1;
x[n+1]=((x[n] +(subject/x[n]))/2);
newepsilon=((x[n+1]-x[n])/x[n+1]);
}
return count;
}```

14. I said to you that ncount must take a value before playing the role of the size of the array!

15. thanks for ur patience std10093&matticus.atleast its started nw.I will post the complete solution in some time.Thank you guys u guys are awesome.