Question

Question: I am getting a few errors but I'm not quite sure what I messed up...

Question: I am getting a few errors but I'm not quite sure what I messed up on, if you could take a look and what I should change I would appreciate it. I have included the context of my objective.

Write your own square root function named double my_sqrt_1(double n) using the following pseudocode:

if n < 1 then lower_bound is n, otherwise lower_bound is

      1 + (n – 1) / 2 – (n – 1)2 / 8

upper_bound is (n + 1) / 2

repeat 10 times:

      midpoint = average of upper_bound and lower_bound

      if midpoint2 > n then upper_bound = midpoint

      if midpoint2 < n then lower_bound = midpoint

return midpoint

and then write a main which prints n, sqrt(n), and my_sqrt_1(n) for n =

π times 10 to the kth power for k = -100, -10, -1, 0, 1, 10, and 100. Use this C++11 code (which may not work on Visual Studio):

      for(auto k : {-100, -10, -1, 0, 1, 10, 100}){

                  n = M_PI * pow(10.0, k);

                  //cout goes here

      }

My code:

#include "std_lib_facilities_4.h"

  

double my_sqrt_1(double n)

{

  

double lower_bound;

double upper_bound;

double midpoint;

for (int iterations=0; iterations<=10;)

{

if (n<1)

{

lower_bound = n;

upper_bound= (n+1)/2;

midpoint = (lower_bound+upper_bound)/2;

if (midpoint*midpoint>n)

{

upper_bound= midpoint;

}

if (midpoint*midpoint<n)

{

lower_bound=midpoint;

}

return midpoint;

}

  

if (n>1)

lower_bound = 1+(n-1)/2-(n-1)*(n-1)/8;

upper_bound = (n+1)/2;

midpoint = (lower_bound+upper_bound)/2;

if (midpoint*midpoint>n)

{

upper_bound= midpoint;

}

if (midpoint*midpoint<n)

{

lower_bound=midpoint;

}

return midpoint;

  

iterations= iterations += 1;

  

}

int main();

{

for(auto k: {-100,-10,-1,0,1,10,100})

{

n= M_PI*pow(10.0,k);

  

cout << n <<"\n";

cout << sqrt(n) <<"\n";

cout << my_sqrt_1(n)<< "\n";

}

}

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Modified Code

#include<iostream>

#include <cmath>

#include <iostream>

#include <string>

#include <iomanip>

// declare a constant variable M_PI and its value

double MPI = 3.14159;

using namespace std;

// Method to find the square root

double my_sqrt_1(double n)

{

   double lower_bound,upper_bound,mid_point;

   if (n < 1)

   {

       lower_bound=n;

   }

   else

   {

       lower_bound =(1+(n-1))/((2-pow((n-1),2))/8);

       upper_bound =(n+1)/2;

   }

   for (int i = 0; i < 10; ++i)

   {

       mid_point=(upper_bound+lower_bound)/2;

       if((pow(mid_point,2))>n)

           upper_bound=mid_point;

       if((pow(mid_point,2))<n)

           lower_bound=mid_point;

   }

   return mid_point;

}

// Main method

int main()

{

   double n;

   for(auto k: {-100, -10, -1, 0, 1, 10, 100})

   {

       n = MPI*pow(10.0, k);

       cout << n << " ," << sqrt(n) << " ," << my_sqrt_1(n) << '\n';

   }

}

Add a comment
Know the answer?
Add Answer to:
Question: I am getting a few errors but I'm not quite sure what I messed up...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I have this code, but I need a flowchart that shows how my program works, and...

    I have this code, but I need a flowchart that shows how my program works, and the inputs and outputs??? #include <iostream> #include <cmath> using namespace std; int fact(int n) { int i; int c = 1; for(i=1; i<=n; i++) { if(n==0) c = 1; else c = c*i; } return c; } double ex(int n,double x) { int i; double c = 0.0; for(i=0; i<=n; i++) { c = c + (double)(pow(x,i)/ (double)fact(i)); } return c; } double sinx(int...

  • Am I getting this error because i declared 'n' as an int, and then asking it...

    Am I getting this error because i declared 'n' as an int, and then asking it to make it a double? This is the coude: #include "stdafx.h" #include <iostream> #include <fstream> #include <string> #include <algorithm> #include <vector> using namespace std; void sort(double grades[], int size); char calGrade(double); int main() {    int n;    double avg, sum = 0;;    string in_file, out_file;    cout << "Please enter the name of the input file: ";    cin >> in_file;   ...

  • what is the output for the following code? explain the steps. /*#include <iostream> using namespace std;...

    what is the output for the following code? explain the steps. /*#include <iostream> using namespace std; int f(int &i) { i = 10; return(5 * i); } int main() { int n = 5; f(n); cout << n << "\n"; return 0; } #include <iostream> using namespace std; int sub1(int n) { n--; return n; } int main() { int m = 10; for(int j = 0; j < 10; j++) m -= sub1(j); cout << m << "\n"; return...

  • I am getting a seg fault with my huffman tree. I'm not sure if it's my...

    I am getting a seg fault with my huffman tree. I'm not sure if it's my deconstructors but also getting some memory leaks. Can some one help me find my seg fault? It's in c++, thanks! //#ifndef HUFFMAN_HPP //#define HUFFMAN_HPP #include<queue> #include<vector> #include<algorithm> #include<iostream> #include <string> #include <iostream> using namespace std; class Node { public:     // constructor with left and right NULL nodes     Node(char charTemp, int c) {       ch = charTemp;       count = c;       left...

  • C++ getline errors I am getting getline is undefined error messages. I would like the variables...

    C++ getline errors I am getting getline is undefined error messages. I would like the variables to remain as strings. Below is my code. #include <iostream> #include<string.h> using namespace std; int index = 0; // variable to hold how many customers are entered struct Address //Structure for the address. { int street; int city; int state; int zipcode; }; // Customer structure struct Customer { string firstNm, lastNm; Address busAddr, homeAddr; }; // Functions int displayMenu(); Customer getCustomer(); void showCustomer(Customer);...

  • How do can I update this code (Code A): Code (A) #include using namespace std; int fibonacci(int n) { int a = 0,...

    How do can I update this code (Code A): Code (A) #include using namespace std; int fibonacci(int n) { int a = 0, b = 1, c; if (n <= 1) return n; for (int i = 2; i <= n; i++) { c = a + b; a = b; b = c; } return b; } int fibonacciRecursive(int n) { if (n <= 1) { return n; } return fibonacciRecursive(n-1) + fibonacciRecursive(n-2); } int main() { int n;...

  • can someone assist me and tell me what I'm missing. I managed to get it this...

    can someone assist me and tell me what I'm missing. I managed to get it this far but still missing two parts to complete it. CENGAGE MINDTAP Programming Exercise 6-4 Tasks main.cpp + 1 #include <iostream r with successful Uses piand 2 #include <cnath> output 5,00 out of 10.00 3 using nanespace std; 4 5 const double PI = 3.1419; 2 out of 4 checks passed. Review 6 the results below for more details. 7 int main() 8 ( Checks...

  • 4 Exercise: Arrays and Functions Many of the tasks from the previous exercises can be generalized...

    4 Exercise: Arrays and Functions Many of the tasks from the previous exercises can be generalized to functions, allowing easy reuse. Recall that arrays in C are essentially represented by pointers, so when an array is passed into a function, the function is given access to the original array data (not a copy). This means that arrays are effectively passed by reference in C, and therefore that functions must be careful not to modify the contents of arrays they receive...

  • How many Iterations will this while loop perform? int ico), j(10); cout << "i = "...

    How many Iterations will this while loop perform? int ico), j(10); cout << "i = " << i << endl; cout << "j = " << j << endl; while (i > j) { cout << "j-" « j << endl; j += 2; cout << "i = " << i << endl; } cout << "i = << i << endl; cout << "j = " << j << endl; 5 6 C 8 10 Infinite times Does the...

  • I am trying to write this code which asks "Write a program that ask the user,...

    I am trying to write this code which asks "Write a program that ask the user, the question: What is a^b? //The program generates two signed integers and gives the user four attempts to get the correct answer //a=- , b = + //a= + , b = - " So far this what I wrote. I am not sure how to do this correctly. #include<iostream> #include<cstdlib> #include<ctime> using namespace std; int main() {    srand(time(0));    int guess,a,ans,b, k;...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT