+ 2

Root

double root(double c) { double t; t=c; while( abs(t-c/t) > 0.001 ) t=0.5*(t+c/t); return t; } Someone can help me? The error massage tell me that I have a problem with the abs that it's not declared

10th Dec 2017, 1:00 AM
Avihu
Avihu - avatar
10 Respuestas
+ 2
Great! Thanx!!!
10th Dec 2017, 2:05 AM
Avihu
Avihu - avatar
+ 1
First number that doesn't divide into it evenly returns true.
10th Dec 2017, 1:37 AM
John Wells
John Wells - avatar
+ 1
So what I should write?
10th Dec 2017, 1:51 AM
Avihu
Avihu - avatar
0
You so sharp!
10th Dec 2017, 1:31 AM
Avihu
Avihu - avatar
0
#include <iostream> #include <cmath> using namespace std; double root(double c) { double t; t=c; while( abs(t-c/t) > 0.001 ) t=0.5*(t+c/t); return t; } bool Prime(int b) { int i; if(b==0 || b==1) return false; else for(i=2; i<root(b); i++) { if( b%i == 0 ) return false; else return true; } } int main () { int n; cout<<"Please enter a num "; cin>>n; cout<<n<<endl; cout<<"does your number is Primery? "<<Prime(n)<<endl; return 0; } Now, can you tell me why for every number its return true?
10th Dec 2017, 1:32 AM
Avihu
Avihu - avatar
0
I didn't understand
10th Dec 2017, 1:38 AM
Avihu
Avihu - avatar
0
Ohh, exactly, that the problem
10th Dec 2017, 1:39 AM
Avihu
Avihu - avatar
0
for(i=2; i<root(b); i++) { if( b%i == 0 ) return false; else return true; } b = 9; first loop i= 2, 9%2 is 1 so else path returns true
10th Dec 2017, 1:40 AM
John Wells
John Wells - avatar
0
for(i=2; i<root(b); i++) { if( b%i == 0 ) return false; } return true;
10th Dec 2017, 1:54 AM
John Wells
John Wells - avatar