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
Shift Microoperations Shift microoperation can be used for serial transfer of the data. They are used generally with arithmetic, logic and other data-processing operations. The


(a) Explain the following terms: Information System and Information Technology. (b) Describe the main components of a computer. Illustrate your answer by a block diagram. (c)

You have a file having sporting goods that are sold online. Every item record contains the item id, item name, item description, item category, item price, and the units in stock.

Q. Describe Miscellaneous and Privileged Instructions? These instructions don't fit in any of above categories. I/O instructions: start I/O, stop I/O, and test I/O. Characteris

Illustrate the Full form of OOA OOA views the world as objects consist of data structures and events that trigger operations and behaviours, for object behaviour changes. The b

Now when a site structure is set up for storing pages and assets for Compass site, you'll create the first page-a home page for the site. As you build this page, you will add text,

Q. Illustrate Basic strategy used in RAID? Basic strategy used in RAID is to replace large capacity disk drive with multiple smaller capacity disks. Data on these disks is dist

What are the Requirements to design Sequential circuit Ans . Design Requirements of Sequential circuit: (i) The circuit specifications translated into a state diagram

In using COBOL on PC we have only flat files and the programs can access only limited storage, while in VS COBOL II on M/F the programs can access up to 16MB or 2GB depending on th