finding nth root using binary search

Revision en2, by acash, 2020-06-25 08:02:36

I m learning binary search and here i need to find the nth root of a number ,i am not able to figure out the error in the below program please help

#include <iostream>
#include <vector>
#include<algorithm>
#include<iomanip>
using namespace std;

double solve(double x, int n){
    int iter=200;
    double low=0;
    double high=x;
    while(iter--){
        double mid = (low+high)/2;
        //cout<<mid<<endl;
        double val = pow(mid,n);
        cout<<val<<endl;
        if(val<x)low=mid;
        else high=mid;
    }
    return low;
}

int main() {
    // int t--;
  int t;
  cin>>t;
  while(t--){
    double x;
    cin>>x;
    int n;
    cin>>n;

    cout<<fixed<<setprecision(12)<<solve(x,n)<<endl;
  }
  return 0;
}

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en3 English acash 2020-06-25 10:17:35 250
en2 English acash 2020-06-25 08:02:36 4 Tiny change: 'ase help\n~~~~~\n#' -> 'ase help\n\n\n~~~~~\n#'
en1 English acash 2020-06-25 08:02:14 816 Initial revision (published)