Ask Question, Ask an Expert

+61-413 786 465

info@mywordsolution.com

Ask Computer Engineering Expert

Question:

Q1) Write a code that asks the user for a positive integer, computes the square root of that integer, and return the result to the user. The computational error needs to be smaller than 0.01.

1.      Get a positive integer 'X' as the input.

2.      Start from an initial guess, G.

3.      Compute the error = (G^2 - X).

4.      If error < 0.01, report the G as the result and stop. If not, go to 5.

5.      Update G, G = (G + X/G)/2. Go to 3.

Code Block

//This program computes the square root of a given positive integer and is written by Vahid D on 13/1/13

#include
   using namespace std;
   int main()
   {
      double G = 10, err; // G is an initial guess and err is the error
      int X; // input, positive integer
      cout << "Enter a positive integer: \n"; //ask user to enter a number
      cin >> X; // get the input
      
      err = (G*G - X); // compute the error
      while ( err > 0.01) // continue updating G till the error is smaller than 0.01
      {
          G   = (G + X/G)/2;
          err = (G*G - X);
      }
      
      cout << "Square root of " << X << " is: " << G << endl; // report the result
      return 0;
   }

Computer Engineering, Engineering

  • Category:- Computer Engineering
  • Reference No.:- M9522221

Have any Question?


Related Questions in Computer Engineering

Explain that when an unauthorized individual gains access

Explain that when an unauthorized individual gains access to the information an organization trying to protect, that act is categorized as a deliberate act of espionage or trespass.

Er database modeling questionemployees have an id name

ER database modeling question: Employees have an id, name, department and datejoined. A manager who is also an employee is managing a department and can be a manager of several employees in that department. Ignore Depart ...

Relational algebrawrite a relational algebra expression

[Relational Algebra] Write a relational algebra expression that will output first name and last name of computer science major male students who have borrowed books from "University of Iowa Main Library". Relational Sche ...

A cartel is branch of an oligopoly there are still a

A cartel is branch of an oligopoly. There are still a handful of large firms and many smaller firms. For instance, the diamond industry and the petroleum industries are examples are oligopolies. However, the main differe ...

The rate of inflation in year 1 is expected to be 14 year

The rate of inflation in year 1 is expected to be 1.4%, year two is 1.8%, and years three through five is expected to be 2%. Assume the real risk-free rate, r*, is 3% for all maturities. What should the yield to maturity ...

Synchronize threads write a program that launches 1000

Synchronize threads. Write a program that launches 1,000 threads. Each thread adds 1 to a variable sum that initially is 0. Define an Integer wrapper object to hold sum. Create two versions of the program with and withou ...

Assignmentsuppose a particular fa called fin has the

Assignment Suppose a particular FA, called FIN, has the property that it had only one final state that was not the start state. During the night, vandals come and switch the + sign with the - sign and reverse the directi ...

What effect does the teacher have on creating a learning

What effect does the teacher have on creating a learning environment with little to no behavior problems?

What is a domain name in the context of internet what is

What is a domain name in the context of Internet? What is the procedure to get a domain name and link it to an Internet Protocol (IP) address? Use an example.

Question explain why you should always search the free

Question: Explain why you should always search the free space and slack space if you suspect person who deliberately delete file or information on a workstation that you are analyzing. The response must be typed, single ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As