please help me

Write your question here.
The following pseudocode computes the square root of positive numbers using newton’s method.
Convert it into a c++ program.

1. input number x
2. If x<=0
a. Print “please enter positive number >0”
b. End program
3. Guess = 0.0
4. newGuess=1.0
5. while newGuess is not equal to Guess
a. guess=newGuess;
b. newGuess=(x/Guess + Guess)/2;
6. print guess


#include <iostream>
using namespace std;

int main (){


double x;
cout<<"please enter positive number";
cin>>x;
if (x<=0)
cout<<"please enter positive number>0"<<endl;
cin>>x;

double Guess = 0.0 ,newGuess=1.0;

if(newGuess!=Guess) {
Guess==newGuess;
else (x%Guess + Guess%2);

cout<<Guess;
}


return 0;
closed account (48T7M4Gy)
Does it work?
closed account (48T7M4Gy)
You haven't implemented lines 2b and 5 of the pseudocode
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
using namespace std;

int main (){
double x;   // Declared, not defined. You don't know what it is equal to.
cout<<"please enter positive number"; 
cin>>x;
if (x<=0)                    // Isn't something missing here?
cout<<"please enter positive number>0"<<endl;
cin>>x;
                                               
double Guess = 0.0 ,newGuess=1.0;

if(newGuess!=Guess) {          // This and 
Guess==newGuess;              // makes no sense. 
else (x%Guess + Guess%2); //  What's this?

cout<<Guess;
}

return 0;      // Does this belong outside the closing bracket? 
Last edited on
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
#include <iostream>
using namespace std;

int main (){
double x;   
bool checkPositive = false;
while(checkPositive == false){
cout<<"please enter positive number"; 
cin>>x;
if(x > 0){
checkPositive = true;
}
}

                                               
double Guess = 0.0 ,newGuess=1.0;

while(newGuess!=Guess){
Guess==newGuess; 
newGuess=(x/Guess + Guess)/2;
}

cout<<Guess;
return 0;  
}
Last edited on
Topic archived. No new replies allowed.