This is my first time using C++ and I have the homework assignment as follows, with an attempt below it.
I am unsure as how to start constructing the loops for the process in paragraph 2. Despite the instructions they don't make sense to me.
My progress so far:
int
sqroot()
{
cout << setiosflags (ios::fixed|ios::showpoint); //assumed I need for output later
double number;
cout<<"Enter the number to find the square root of: ";
cin>>number;
int sqRoot = 1;
while (sqRoot*sqRoot < number) // sqRoot is too small
sqRoot++; // try the next number
int y=0;
int newRoot =1;
while (y < number)
{
if (newRoot*newRoot > number )
break;
y=newRoot*newRoot;
newRoot++;
}
int decimalInput;
int decimalPosition= 0;
cout<<"Enter the desired decimal position: "<<endl;
cin>>decimalInput;
while (decimalPosition < decimalInput)
decimalPosition++;
return 0;
}
You should not just increment new newRoot by 1 (newRoot++), but by smaller steps. To make it more efficient, you start whit big step (big power of 10, for instance 1000) .Divide this step by 10 just before square of your result will be bigger than input number.
Extra HINT:
There are two nested loops. Outer loops checks for step >= error and inner loops check that result will be small enough.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.