I need a bit of help and some tips on where to go For a programming assignment, I have to write a program that calculates the square root of the number that the user inputs and there are certain requirements.
The main asks for the number and displays it, operates inside a Loop so that the user can repeat the program without closing it
The calculation has to be done in a function called sqRoot which will be called by main using the algorithm:
newValue = 0.5 * (oldValue + (X / oldValue))
- sqRoot will need to find the absolute value of the number with a function named absVal which will then be called by sqRoot
I dont even know where to start with a program like this. But, this is what i have so far:
#include <iostream>
#include <cmath>
#include <cstdlib>
using namespace std;
double sqRoot();
double absVal();
int i = 0;
double X;
int main()
{
sqRoot = sqrt(X);
double X;
// Calculations
cout << "Please enter a number: ";
cin >> X;
while (X <= 0)
{
cout << "*** Error: Invalid Number! *** " << endl;
cout << "Please enter a number: ";
cin >> X;
}
while (X >= 1)
{
cout << "The Square Root is: " << sqRoot << endl;
}
}
double sqRoot ()
{
double newValue;
double oldValue ;
while (abs(newValue - oldValue) > 0.0001)
{
newValue = 0.5 * (oldValue + ( X / oldValue));
oldValue = newValue;
cout << " The square root is: " << newValue << endl;
}
return (newValue);
}
I'm just stuck with what to do next and how to properly write the program. Thank You for the help and tips!