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

Please program in C++ etc. Each number in the sequence is computed using the pre

ID: 3716249 • Letter: P

Question

Please program in C++

etc. Each number in the sequence is computed using the previous number and the initial number is a. The sequence will converge, meaning that after a while the absolute difference between two consecutive numbers is as small as possible.You could write a do-while loop that with each iteration computes two values as long as fabs(x2-x1) > 1e-10.

Write a program that computes the 3rd root of a number. There should be a loop where numbers are read, then compute the cube root, and print it using 20 decimal places, together with the value given by pow(a,1.0/3.0).

Explanation / Answer


Note : Provided two programs using pow fuction and without pow fuction..........at the beginning give testcases that you want and number to find the cube root.





//////////////////////////////Cpp using Pow Fuction///////////////////////
#include <iostream>
#include <cmath>
using namespace std;
  
int main()
{
int testcases;
cout << "Enter the No.of TestCases :" << std::endl;
cin >> testcases;
testcases = testcases-1;
do{
double number,result;
cout << "Enter the Number that you to find the Cube Root :" << std::endl;
cin >> number;
result = pow(number, 1.0/3.0);
printf("Cubic root of %.20lf is %.20lf ",
number,result);
}while(testcases-- );
return 0;
}






//////////////////////////////Cpp for Custom Method without using Pow Fuction///////////////////////


#include <bits/stdc++.h>
using namespace std;

double diff(double n,double mid)
{
if (n > (mid*mid*mid))
return (n-(mid*mid*mid));
else
return ((mid*mid*mid) - n);
}

double cubicRoot(double n)
{

double start = 0, end = n;
// Set precision
double e = 0.0000001;
while (true)
{
double mid = (start + end)/2;
double error = diff(n, mid);

if (error <= e)
return mid;
if ((mid*mid*mid) > n)
end = mid;

else
start = mid;
}
}

int main()
{
int testcases;
cout << "Enter the No.of TestCases :" << std::endl;
cin >> testcases;
testcases = testcases-1;
do{
double n;
cout << "Enter the Number that you to find the Cube Root :" << std::endl;
cin >> n;
printf("Cubic root of %.20lf is %.20lf ",
n, cubicRoot(n));
}while(testcases-- );
return 0;
}

Sample Output :
Enter the No.of TestCases : 3

Enter the Number that you to find the Cube Root : 27

Cubic root of 27.00000000000000000000 is 3.00000000000000000000

Enter the Number that you to find the Cube Root : 8

Cubic root of 8.00000000000000000000 is 2.00000000000000000000

Enter the Number that you to find the Cube Root : 45

Cubic root of 45.00000000000000000000 is 3.55689330449006257950



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