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

Real-Time Software Programs that monitor/analyze/ control real world events as they take place are known as real-time software. Elements of real-time software involve a data

Q. What is a good resolution? In fact it relies on your hardware. Hence it's the maximum your hardware can allow you.  But one odd-looking resolution that has become popular is

How can I printout the formulas in an Excel spreadsheet - rather than the results? Ans) The trick is to change the way Excel shows the worksheet before you choose to print. Ch

Explain the term- Hacking    Use of passwords and ids to prevent illegal access to files. Also locking the computer itself or locking computer room can help here. Encryption s

Integrating Virtual Memory, TLBs, and Caches - computer architecture:   There are 3 types of misses: 1. a cache miss 2. TLB miss 3. a page fault 2 techniqu

Granularity Granularity refers to the quantity of computation done in concurrent relative to the size of whole program. In concurrent computing, granularity is a qualitative me

Consider the following expression. Assume that complement inputs are available. F(A,B,C,D) = ∑m (0,1,3,5,9,12,14) + ∑d (4,10,15) 1. Find minimal expression for SOP. Draw gate ne

Explain Tri-state logic inverter with the help of a circuit diagram. Give its Truth Table. Ans: Tri-state Logic Inverter: The functional diagram of Tri-state Logic Inve

Q. Explain about Microcomputers? Microcomputers: A microcomputer's CPU is a microprocessor. They are characteristically used as single usercomputer though present day microco