Hi everyone,
I have written this code which can find the square root of a positive number using newtons method. The code below describes it all. Now, I just need to know how can I show each iteration of the loop? like I wanna out each iteration until the final value is reached.
Thank you in advance

// A program that prints out the square root of a positive value entered by user

#include <iostream> // for cin and cout
#include <cmath> // for math calculations

using namespace std;
int main() // Block of Code
{
  double value; // We use double to avoid overflow
  double value1, value2;
  cout <<"*******************************************" << endl;
  cout <<   "Hey! Welcome to the simple calculator" << endl;
  cout <<"*******************************************" << endl << endl << endl;
  cout << "A Calculator that finds the square root of a number using Newton's method"
       << endl << endl; // using multiple endl just to make the code look better to user
  cout << "Enter a positive number and we will calculate its square root: ";
  cin >> value;
  if (value < 0.0)// negative values are not accepted
    cout << "Sorry, I'm unable to perform this caculation!"
         << endl;
  else
     if (value == 0.0)
        cout << "square root of "
             << value
             << " is 0.00"
             << endl;
     else
       {
         value1 = value/4.0; // initial value
         value2 = (value1 + value/value1)/2;
         while (fabs(value2-value1) > 0.0001)// for float absolute values
           { 
             value1 = value2;
             value2 = (value1 + value/value1)/2;
           }
         cout << "square root of "
              << value
              << " is " << value2 // prints out the square root of the value entered
              << endl;

       }      
 system ("pause"); // inside only the main function and not any other one!
 // because otherwise the system will pause only to show the square root of
 // a positive value
}

Edited 3 Years Ago by Dani: Fixed formatting

Please try to enter your code in the correct code tags... (ends with "[/code]")

So what stops you from just using "cout << current value" in the correct spot?

is this your own code? because I mean there is LOADS of cout and user communication, writing all that surely you understand giving output and how and where to do it?

Edited 3 Years Ago by Dani: Fixed formatting

Hey I figured it out as I got my initial value to display first (value1=value/4.0)
the problem now is that the values repeat each other is that correct for this kind of code? how can I display the whole equation to make it easier for the user to follow through? instead of just showing the final result?

// A program that prints out the square root of a positive value entered by user

#include <iostream> // for cin and cout
#include <cmath> // for math calculations

using namespace std;
int main() // Block of Code
{
  double value; // We use double to avoid overflow
  double value1, value2;
  cout <<"*******************************************" << endl;
  cout <<   "Hey! Welcome to the simple calculator" << endl;
  cout <<"*******************************************" << endl << endl << endl;
  cout << "A Calculator that finds the square root of a number using Newton's method"
       << endl << endl; // using multiple endl just to make the code look better to user
  cout << "Enter a positive number and we will calculate its square root: ";
  cin >> value;
  if (value < 0.0)// negative values are not accepted
    cout << "Sorry, I'm unable to perform this caculation!"
         << endl;
  else
     if (value == 0.0)
        cout << "square root of "
             << value
             << " is 0.00"
             << endl;
     else
       {
         value1 = value/4.0; // initial value
         value2 = (value1 + value/value1)/2;
         cout << value1 << endl;
				 while (fabs(value2-value1) > 0.0001)// for float absolute values
           { 
					 	 
             value1 = value2;
             value2 = (value1 + value/value1)/2;
             cout << value1 << endl;
						 cout << value2 << endl;
					 }
					  
         cout << "square root of "
              << value
              << " is " << value2 // prints out the square root of the value entered
              << endl;
             
       }      
 system ("pause"); // inside only the main function and not any other one!
 // because otherwise the system will pause only to show the square root of
 // a positive value
}

how can I display the whole equation to make it easier for the user to follow through? instead of just showing the final result?

you could display the process by printing out a similar statement to the formula in the loop
or
you can assign the values used in the loop to variables that will be used in displaying the equation in the end

that looks much better... And I agree with zeroliken... ok how about the following:

cout << "Starting calculation" << endl << endl;

while (fabs(value2-value1) > 0.0001)// for float absolute values

           { 

             cout << "Starting loop" << endl;

             cout << "Starting value: " << value1;

             value1 = value2;

             cout << "Overwritten to: " << value1;

             value2 = (value1 + value/value1)/2;

	     cout << value2 << endl;

}

Build further on this... try to always make your output descriptive and complete, then everyone can know what is going on

Also remember to make it easy to read... add in endl commands and spaces... get everything to display easy to read... just makes it easier in the future

Edited 4 Years Ago by Eagletalon: n/a

Thank you guys so much for your help! here is what I came up with to make it look nice - I think- but the problem is it shows 2 values after "overwritten to" can I make each value used as a starting value then overwritten to a different value and use that value after as a starting value? I seem to be confusing but what I mean is for example x0 is my starting value that was overwritten to produce x1 now I want x1 to be used as a starting value and x2 is the new produced value. I hope that made sense. This code is meant to be for high school students is there a way where I can make it more understandable by showing the formula for each iteration? thank you again. Here's the code

// A program that prints out the square root of a positive value entered by user
 
#include <iostream> // for cin and cout
#include <cmath> // for math calculations
 
using namespace std;
int main() // Block of Code
{
  double value; // We use double to avoid overflow
  double value1, value2;
  cout <<"*******************************************" << endl;
  cout <<   "Hey! Welcome to the simple calculator" << endl;
  cout <<"*******************************************" << endl << endl << endl;
  cout << "A Calculator that finds the square root of a number using Newton's method"
       << endl << endl; // using multiple endl just to make the code look better to user
  cout << "Enter a positive number and we will calculate its square root: ";
  cin >> value;
  if (value < 0.0)// negative values are not accepted
    cout << "Sorry, I'm unable to perform this caculation!"
         << endl;
  else
     if (value == 0.0)
        cout << "square root of "
             << value
             << " is 0.00"
             << endl;
     else
       {
         value1 = value/4.0; // initial value
         value2 = (value1 + value/value1)/2;
         cout << value1 << endl;
				 while (fabs(value2-value1) > 0.0001)// for float absolute values
           { 
 
             value1 = value2;
             value2 = (value1 + value/value1)/2;
             cout << "Starting loop" << endl;
 
             cout << "Starting value: " << value1;
 
             value1 = value2;
 
             cout << "  Overwritten to: " << value2;
 
             value2 = (value1 + value/value1)/2;
 
	     cout << "      " << value2 << endl;
 
					 }
 
         cout << "square root of "
              << value
              << " is " << value2 // prints out the square root of the value entered
              << endl;
 
       }      
 system ("pause"); // inside only the main function and not any other one!
 // because otherwise the system will pause only to show the square root of
 // a positive value
}

Thank you guys I think I'm getting to what I need. I will post my last version with a question if I had one or jut so you guys can evaluate it.

save the value to a another(new) variable then use it a the formula

EDIT: I'm glad that your solving it on your own then

Edited 4 Years Ago by zeroliken: n/a

yeah the structure you can work on yourself... depends on what you want it to look like...

remember that as long as it makes sense you can put it into more than 1 line, you can change around, what ever you want... kind of the programmers right ;)

So this is the last version. This program as I mentioned previously is made for high school students. I would very much appreciate it if you guys gave me some input on how to make it look as great, simple and clear as possible after evaluating it from a high school student point of view. Thanks again.

// A program that prints out the square root of a positive value entered by user

#include <iostream> // for cin and cout
#include <cmath> // for math calculations

using namespace std;
int main() // Block of Code
{
  double value; // We use double to avoid overflow
  double value1, value2;
  cout <<"*******************************************" << endl;
  cout <<   "Hey! Welcome to the simple calculator" << endl;
  cout <<"*******************************************" << endl << endl << endl;
  cout << "A Calculator that finds the square root of a number using Newton's method"
       << endl << endl; // using multiple endl just to make the code look better to user
  cout << "Enter a positive number and we will calculate its square root: ";
  cin >> value;
  if (value < 0.0)// negative values are not accepted
    cout << "Sorry, I'm unable to perform this caculation!"
         << endl;
  else
     if (value == 0.0)
        cout << "square root of "
             << value
             << " is 0.00"
             << endl;
     else
       {
         value1 = value/4.0; // initial value
         value2 = (value1 + value/value1)/2;
         cout << "First Guess = " << value1 << endl;
                 while (fabs(value2-value1) > 0.0001)// for float absolute values
           { 

             value1 = value2;

             cout << value2 <<"+("<<value<<"/"<<value2<<")/2" << endl;

             cout << "Starting value: " << value2;

             value1 = value2;
                         value2 = (value1 + value/value1)/2;

                         cout << "  Overwritten to: " << value2 << endl;


                     }

         cout << "square root of "
              << value
              << " is " << value2 // prints out the square root of the value entered
              << endl;

       }      
 system ("pause"); // inside only the main function and not any other one!
 // because otherwise the system will pause only to show the square root of
 // a positive value
}

Edited 3 Years Ago by Dani: Fixed formatting

oops sorry!

// A program that prints out the square root of a positive value entered by user
 
#include <iostream> // for cin and cout
#include <cmath> // for math calculations
 
using namespace std;
int main() // Block of Code
{
  double value; // We use double to avoid overflow
  double value1, value2;
  cout <<"*******************************************" << endl;
  cout <<   "Hey! Welcome to the simple calculator" << endl;
  cout <<"*******************************************" << endl << endl << endl;
  cout << "A Calculator that finds the square root of a number using Newton's method"
       << endl << endl; // using multiple endl just to make the code look better to user
  cout << "Enter a positive number and we will calculate its square root: ";
  cin >> value;
  if (value < 0.0)// negative values are not accepted
    cout << "Sorry, I'm unable to perform this caculation!"
         << endl;
  else
     if (value == 0.0)
        cout << "square root of "
             << value
             << " is 0.00"
             << endl;
     else
       {
         value1 = value/4.0; // initial value
         value2 = (value1 + value/value1)/2;
         cout << "First Guess = " << value1 << endl;
				 while (fabs(value2-value1) > 0.0001)// for float absolute values
           { 
 
             value1 = value2;
             
             cout << value2 <<"+("<<value<<"/"<<value2<<")/2" << endl;
 
             cout << "Starting value: " << value2;
 
             value1 = value2;
 						 value2 = (value1 + value/value1)/2;
             
						 cout << "  Overwritten to: " << value2 << endl;
 
             
					 }
 
         cout << "square root of "
              << value
              << " is " << value2 // prints out the square root of the value entered
              << endl;
 
       }      
 system ("pause"); // inside only the main function and not any other one!
 // because otherwise the system will pause only to show the square root of
 // a positive value
}

ok first thing... in the case of an user inputting a negative value... what happens?

in the case of an user entering letters... what happens?

unfortunately in software development you HAVE to handle the possibility that your user is an idiot... (no offense intended)

try doing something like a while loop around your initial input that breaks out when the input in valid

another basic check you can perform to block to much working is to see if the input is 1... IIRC square root of 1 = 1 so no need to calculate

thanks for ur input @eagletalon. you pointed out a very good point. How can I do that? I have entered a letter and it still gave me a numeric square root!

Hey guys,
so I was thinking about adding another else - if statement where it basically says else if y==(not a number) cout "error" but how can I write that so the compiler understands what I need?

Okay so I figured that part out! I have another issue tho. My teacher uses command prompt, and I don't want the program to close every time he enters a letter or a negative value. I want the program to re-launch - you can tell I'm not a great programmer - so its easier and faster to keep entering many values without the program shutting down. Thanks in advance

Here is my final version

// A program that prints out the square root of a positive value entered by user
 
#include <iostream> // for cin and cout
#include <cmath> // for math calculations
#include <string> // to make it accept digits only 

using namespace std;
int main() // Block of Code
{
  double value; // We use double to avoid overflow
  double value1, value2;
  cout <<"*******************************************" << endl;
  cout <<   "Hey! Welcome to the simple calculator" << endl;
  cout <<"*******************************************" << endl << endl << endl;
  cout << "A Calculator that finds the square root of a number using Newton's method"
       << endl << endl; // using multiple endl just to make the code look better to user
  cout << "Enter a positive number and we will calculate its square root: ";
  cin >> value;
  if (value < 0.0)// negative values are not accepted
    cout << "Sorry, I'm unable to perform this caculation!"
         << endl;
  else
     if (value == 0.0)
        cout << "square root of "
             << value
             << " is 0.00"
             << endl;
  else
	 		 if (cin.fail()) {
			 		cin.clear();
			 		string junk;
			 		cin >> junk;
			 		cout << "Sorry, I can only compute digits!" << endl;
					}
	  
		 else
       {
         value1 = value/4.0; // initial value
         value2 = (value1 + value/value1)/2;
         cout << "First Guess = " << value1 << endl << endl;
				 while (fabs(value2-value1) > 0.0001)// for float absolute values
           { 
 
             value1 = value2;
             
             cout << value2 <<"+("<<value<<"/"<<value2<<")/2" << endl << endl;
 
             cout << "Starting value: " << value2;
 
             value1 = value2;
 						 value2 = (value1 + value/value1)/2;
             
						 cout << "  Overwritten to: " << value2 << endl << endl;
 
             
					 }
 
         cout << "square root of "
              << value
              << " is " << value2 // prints out the square root of the value entered
              << endl;
 
       }      
 system ("pause"); // inside only the main function and not any other one!
 // because otherwise the system will pause only to show the square root of
 // a positive value
}

Haha bloody well done... sorry I dont normally log in from home so I didnt get your updates...

If you think this is a stable version then I am highly impressed that you made it work by yourself... its a quality you need to develop, because I believe more than 90% of programming is solving problems (hence the forum was born)

I have 1 other suggestion for you though... if you believe your application is solid, have someone test it with the intention of "breaking" it... errors are absolutely unwanted in any application, warnings and confirmation requests is a good thing though, keep that in mind...

well I probably would have not made it this far without your help @Eagletalon so thank you very much for your help. The teacher was actually impressed and thats all I wanted - a full mark -.

Hahaha then I guess my work here is done ;)

just remember to mark the thread as solved... and good luck further :D

This question has already been answered. Start a new discussion instead.