Compute the positive integer and square root, Computer Engineering

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;
   }

Posted Date: 2/15/2013 7:31:32 AM | Location : United States







Related Discussions:- Compute the positive integer and square root, Assignment Help, Ask Question on Compute the positive integer and square root, Get Answer, Expert's Help, Compute the positive integer and square root Discussions

Write discussion on Compute the positive integer and square root
Your posts are moderated
Related Questions
Q. Main drawbacks of CD-ROMs? The main drawbacks of CD-ROMs are: It is read only thus can't be updated Access time is longer than that of magnetic disks. Very

what is meant by risk detection in software project management

Suppose you have to develop an error recovery protocol for a link that is unreliable and delay sensitive, which of the following protocol would you choose? (i) Stop & wait.

A subroutine can be terminated unconditionally using EXIT. True.

Specifying the Problem: Now next here furtherly we now use to look at how you mentally constructed your decision tree where deciding what to do at the weekend. But if one way

Explain the transport layer in detail. Transport Layer: The transport layer controls and makes sure the end-to-end integrity of the data message propagated by the network am

Types of Addressing Modes: Each instruction of a computer mentions an operation on certain data. There are many ways of specifying address of the data to be operated on. These

Q. What do you mean by True Complement Method? Explain in detail. Q. Show IEEE format for (12.75)10. Q. What are the various ways to represent Negative Numbers in computer sy

Eequivalences rules: This conveys a meaning that is actually much simpler so than you would think on first inspection.  Hence we can justify this, by using the following ch

What is virtual memory? How address mapping is done in cache memory? Elaborate your answer with examples.