Ask Question
19 September, 05:40

Write a function that accepts an integer parameter and returns its integer square root (if it exists). The function should throw an exception if it is passed an integer that is not a perfect square. Do not handle the exception in the function, but instead create the exception handler in main. Make the exception a new exception class which your program creates. Demonstrate the function with a driver program that passes the function the numbers 0 through 25, then prints whether or not the number is a perfect square. (A "perfect square" is a whole number whose square root is also a whole number.)

+4
Answers (2)
  1. 19 September, 05:44
    0
    See explaination

    Explanation:

    / / Include the necessary header files.

    #include

    #include

    #include

    using namespace std;

    / / Definition of the function.

    int square_root (int n)

    {

    / / Check whether n is less than 0.

    if (n < 0)

    / / Throw exception.

    throw domain_error ("Supplied integer is negative!");

    / / check whether n is equal to 0;

    else if (n = = 0)

    / / Throw 0.

    throw 0;

    / / Declare a variable and assign value.

    double squ_root = sqrt (n);

    / / / / Declare a variable and assign value.

    int squ_root_int = (int) squ_root;

    / / compare values

    / / check whether the values are equal or not.

    if (squ_root! = squ_root_int)

    / / Throw exception.

    throw invalid_argument ("Supplied integer is not a perfect square!");

    / / return the value.

    return squ_root_int;

    }

    / / Declare the main function.

    int main ()

    {

    / / declare variables.

    int n;

    int squ_root;

    / / Prompt the user to enter the number.

    cout << "Enter a number: ";

    cin >> n;

    / / start the try block.

    try

    {

    / / call to the function.

    squ_root = square_root (n);

    / / Display the statement on console.

    cout << "Square root is " << squ_root << endl;

    }

    / / start the catch block.

    catch (domain_error e)

    {

    / / Display the statement on console.

    cout << "Error: " << e. what () << endl;

    return - 1;

    }

    / / Start the catch block

    catch (int y)

    {

    / / check whether y is equal to zero or not.

    if (y = = 0)

    {

    / / Display the statement on console.

    cout << "Error: Supplied integer is zero!" << endl;

    return - 2;

    }

    }

    / / Start the catch block

    catch (invalid_argument e)

    {

    / / Display the sstatement.

    cout << "Error: " << e. what () << endl;

    return - 3;

    }

    / / Return the value.

    return 0;

    }
  2. 19 September, 06:01
    0
    Check the explanation

    Explanation:

    #include

    #include

    #include / /Not sure about including math. h

    using namespace std;

    int sqrt4 (const int m)

    {

    int i=0;

    const int q=0;

    while ((i*i) < = m)

    {

    if (i*i==m)

    {

    q++;

    return i;

    }

    else

    {

    i++;

    }

    }

    if (q==0)

    {

    throw "not a perfect square!";

    }

    }

    int M1 (int x) {

    cout << "Number: " << x << endl;

    x = sqrt (x);

    cout << "Number's square root: " << x << endl;

    return x;

    }

    void main () {

    int y = 4; / / Program currently uses 4 in calculations

    M1 (y);

    }
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “Write a function that accepts an integer parameter and returns its integer square root (if it exists). The function should throw an ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers