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
Write short notes on the Multihomed host. A host computer that connects to multiple networks is termed as multi-homed host. Multihoming is occasionally used to raise reliabilit

Explain technical reasons which made Microsoft withdraw its support for VBA in Mac? Ans) The reasons which made Microsoft drop its carry to VBA are as follows, Microsoft visual

A certain memory has a capacity of 4K × 8 (i)  How many data input and data output lines does it have? (ii) How many address lines does it have? (iii) What is its capacity in bytes

In order to support IA-32 Itanium can switch in 32-bit mode with special jump escape instructions. IA-32 instructions have been mapped to Itanium's functional units. But as Itanium

The scope of the experts system is very limited. It cannot work outside the field it is being used. The users knowledge is required to adjust to new situation. To reduce

Q. Show Basic concepts of permutation? Let us look at the basic concepts of permutation with respect to interconnection network.  Let us say the network has set of n input node

Biscuits move on a conveyer through an oven where they are cooked.   Two sensors, H and C, measure the temperature of the oven, H = 1 if the oven is too hot and C = 1 if the ove

What are the two primary models of Supply Chain Management? The Two Primary models of Supply Chain Management are:- 1.  Porter's Value Chain Model 2. Supply Chain Model

Q. Functions for MPI Environment? Int MPI_Finalize (void) It ends the MPI environment. Any MPI function cannot be called after MPI_Finalize. Each MPI process belongs to on

What are Sewing Kits? Sewing Kits are modules which contain a not used mix of gates, any other cells or flip-flops considered potentially helpful for an unforeseen metal fix. W