Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++: Loop keeps running infintely. So, here\'s my code: #include <iostream> usin

ID: 670915 • Letter: C

Question

C++: Loop keeps running infintely.


So, here's my code:

#include <iostream>

using std::cin;

using std::endl;

using std::cout;

#include <cmath>

#include <iomanip>

using std::setw;

using std::setiosflags;

using std::ios;

using std::fixed;

using std::setprecision;

int main()

{

  

//====================== Declaration Section =======================//

int value;

int decimal;

int sqrootS; //meaning the smallest sqrt

int sqrootL; //meaning the largest sqrt

double error;

double firstApproximation;

double decimalPosition;

double delta;

double approx;

double actualsqroot;

//====================== End of Declaration =========================//

  

  

  

  

//==================Part1:

  

cout << "Please, enter a value that will find the square root: ";

cin >> value;

cout << endl;

  

cout << "Please, enter your desired amount of decimal places (NO MORE THAN 15): ";

cin >> decimal;

cout << endl;

  

  

sqrootL = 1;

while (value > sqrootL * sqrootL)

sqrootL++;

  

sqrootS = 10000;

while (value < sqrootS * sqrootS)

sqrootS--;

  

firstApproximation = ((sqrootL + sqrootS)/2.0); //Because we need the average of the two sqrts.

  

decimalPosition = 1;

  

delta = 1/pow(10,decimalPosition); //Equation was given.

  

//==================Part2:

  

//* Computing the expected output information:

  

cout << "Find the square root of " << value << " to " << decimal << " decimal places." << endl;

cout << " approx" << endl;

cout << "delta" << " " << "sq root" << " " << "error" << endl;

cout << "-----" << " " << "-------" << " " << "-----" << endl;

cout << " ";

cout << fixed << setprecision(decimal) << firstApproximation << endl;

  

//==================Part3:

  

   for (int decimalPosition = 1; decimalPosition <= decimal; decimalPosition++)

   {

while (decimalPosition <= decimal)

{

while (firstApproximation * firstApproximation <= value)

{

firstApproximation = firstApproximation + delta;

  

error = (firstApproximation * firstApproximation) - value;

  

cout << setw(5) << setprecision(decimal) << delta << " " << setw(7) << firstApproximation << " " << setw(9) << error << endl;

  

}

//=======

  

while (firstApproximation * firstApproximation >= value)

{

firstApproximation = firstApproximation - delta;

  

error = (firstApproximation * firstApproximation) - value;

  

cout << setw(5) << setprecision(decimal) << delta << " " << setw(7) << firstApproximation << " " << setw(9) << error << endl;

}

  

cout << endl;

  

}

}

  

//========= Last output part:

  

cout << "The square root of " << value << " to " << decimal << " decimal places is " << firstApproximation << "." << endl;

  

approx = firstApproximation * firstApproximation;

cout << "The approximation " << firstApproximation << " squared is " << approx << endl;

  

actualsqroot = sqrt(value);

cout << "The actual square root of " << value << " to 5 " << fixed << setprecision(5) << " decimal places is " << sqrt(value) << "." << endl;

  

  

return 0;

}

I don't seem to know how to fix this. What should I add/remove?

Thanks,

Explanation / Answer

#include <iostream>
using std::cin;
using std::endl;
using std::cout;

#include <cmath>

#include <iomanip>
using std::setw;
using std::setiosflags;
using std::ios;
using std::fixed;
using std::setprecision;

int main()
{
  
//====================== Declaration Section =======================//
int value;
int decimal;
int sqrootS; //meaning the smallest sqrt
int sqrootL; //meaning the largest sqrt
double error;
double firstApproximation;
double decimalPosition;
double delta;
double approx;
double actualsqroot;
//====================== End of Declaration =========================//
  
  
  
  
//==================Part1:
  
cout << "Please, enter a value that will find the square root: ";
cin >> value;
cout << endl;
  
cout << "Please, enter your desired amount of decimal places (NO MORE THAN 15): ";
cin >> decimal;
cout << endl;
  
  
sqrootL = 1;
while (value > sqrootL * sqrootL)
sqrootL++;
  
sqrootS = 10000;
while (value < sqrootS * sqrootS)
sqrootS--;

  
firstApproximation = ((sqrootL + sqrootS)/2.0); //Because we need the average of the two sqrts.
  
decimalPosition = 1;
  
delta = 1/pow(10,decimalPosition); //Equation was given.
  
//==================Part2:

  
//* Computing the expected output information:
  
cout << "Find the square root of " << value << " to " << decimal << " decimal places." << endl;
cout << " approx" << endl;
cout << "delta" << " " << "sq root" << " " << "error" << endl;
cout << "-----" << " " << "-------" << " " << "-----" << endl;
cout << " ";
cout << fixed << setprecision(decimal) << firstApproximation << endl;
  
//==================Part3:
  
for (int decimalPosition = 1; decimalPosition <= decimal; decimalPosition++)
{
  

while (firstApproximation * firstApproximation <= value)
{
firstApproximation = firstApproximation + delta;
  
error = (firstApproximation * firstApproximation) - value;
  
cout << setw(5) << setprecision(decimal) << delta << " " << setw(7) << firstApproximation << " " << setw(9) << error << endl;
  
}
//=======
  
while (firstApproximation * firstApproximation >= value)
{
firstApproximation = firstApproximation - delta;
  
error = (firstApproximation * firstApproximation) - value;
  
cout << setw(5) << setprecision(decimal) << delta << " " << setw(7) << firstApproximation << " " << setw(9) << error << endl;
}
  
cout << endl;
  
  
}
  
//========= Last output part:
  
cout << "The square root of " << value << " to " << decimal << " decimal places is " << firstApproximation << "." << endl;
  
approx = firstApproximation * firstApproximation;
cout << "The approximation " << firstApproximation << " squared is " << approx << endl;
  
actualsqroot = sqrt(value);
cout << "The actual square root of " << value << " to 5 " << fixed << setprecision(5) << " decimal places is " << sqrt(value) << "." << endl;
  
  
return 0;
}

Removed an unnecessary while loop inside for loop.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote