Compute the positive integer and square root, Computer Engineering

Assignment Help:

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


Related Discussions:- Compute the positive integer and square root

Convert the following decimal numbers into octal, Q. Convert the following ...

Q. Convert the following DECIMAL numbers into OCTAL, double check by converting the result OCTAL to DECIMAL. a) 932 b) 4429.625 c) 19

Paged virtual memory - computer architecture, Paged virtual memory: Mo...

Paged virtual memory: Mostly all implementations of virtual memory divide the virtual address space of an application program into pages; a page is a block that contains conti

Discuss different routing plan adopted in telephone network, Discuss differ...

Discuss different Routing plan adopted in a Telephone network. Hierarchical networks are able of handing heavy traffic where needed, and at similar time use minimal number of t

Convert the decimal number to excess-3 code, Convert the decimal number 430...

Convert the decimal number 430 to Excess-3 code ? Ans. Excess  3  is  a  digital  code  acquired  from  adding  3  to  every  decimal  digit  and  after that converting the res

What are the features common in most of the websites, What are the features...

What are the features common in most of the websites Following general features must be found on most web sites in one form or another (this list is by no means exhaustive):

Multi-layer network architectures, Multi-Layer Network Architectures - Arti...

Multi-Layer Network Architectures - Artificial intelligence: Perceptrons have restricted scope in the type of concepts they may learn - they may just learn linearly separable f

Which of the logic gates are known as universal gates, Which of the logic g...

Which of the logic gates are known as universal gates ? Ans. NAND and NOR are termed as universal gates, since any digital circuit can be realized completely by using either of

What is default look-and-feel of a swing component, The default Look and Fe...

The default Look and Feel of swing components are Java Look-and-Feel.

Define throughput, Define throughput?  Throughput in CPU scheduling is ...

Define throughput?  Throughput in CPU scheduling is the number of processes that are completed per unit time. For long processes, this rate might be one process per hour; for s

Operations of a scientific calculator, A program is to be developed to simu...

A program is to be developed to simulate the operations of a scientific calculator. List the facilities to be provided by this calculator. Analyze this using a DFD 0- level and 1-

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd