1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Ganezh [65]
4 years ago
12

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.)
Computers and Technology
2 answers:
professor190 [17]4 years ago
8 0

Answer:

See explaination

Explanation:

// Include the necessary header files.

#include <iostream>

#include <exception>

#include <cmath>

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;

}

kupik [55]4 years ago
4 0

Answer:

Check the explanation

Explanation:

#include <iostream>

#include <cmath>

#include <math.h> //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);

}

You might be interested in
When operating a forklift, you should always maintain what kind of speed? A. High Speed B. Low Speed C. Medium Speed
timurjin [86]
Low speed unless you want your crates or boxes to fall
5 0
3 years ago
Computer keyboard failures can be attributed to electrical defects or mechanical defects. a repair facility currently has 25 fai
Mariulka [41]
You are going to select 5 keyboards, from a set of 25 defective keyboards. When you select the first keyboard it may be have either electrical deffects or mechanical deffect, this is two possibilities. The same happens with the second, third, fourth and fifth selection. Then  each selection has 2 different possibilities, and the number of possibilities are: 2*2*2*2*2 = 32.

But that is considering that the order matters. This is that it is different that the first has electrical deffects and the others have mechanical defects than the second has mechanical electrical deffects and the other has mechanical deffects.

If you the order is not relevant, then the only different outcomes are:

1) 5 with electrical deffects
2) 4 with electrical deffects and 1 with mechanical deffects
3) 3 with electrical deffects and 2 with mechanical deffects
4) 2 with electrical deffects and 3 with mechanical deffects
5) 1 with electrical deffects and 4 with mechanical deffects
5) 5 with mechanical deffects.

In this case the answer is 6 different ways.

Answer: 6 
5 0
3 years ago
Deborahc1287563 You like Among us too?
gtnhenbr [62]
Yeah I do, lol I used to play it quite a lot although now I have stopped but I still play
6 0
3 years ago
Read 2 more answers
What time does walmart deposit paychecks?
icang [17]
Around 6:00 PM. Maybe.
7 0
4 years ago
There is a flashing yellow light at the intersection you are approaching. What does the flashing yellow light indicate, and what
Ilia_Sergeevich [38]
The flashing yellow light indicates that people should be aware of other cars and turn carefully. To be more safe, you should slightly slow down, make sure all signals that should be on are on, and you should drive very carefully.
6 0
3 years ago
Other questions:
  • One of the most obvious initial changes in windows vista is the ____ interface
    10·1 answer
  • Technician A says that a common type of spontaneous combustion occurs when rags or papers that are soaked in solvents end up in
    14·2 answers
  • The process of providing and denying access to objects is called:
    5·1 answer
  • 4. What are five actions you should do to care for your camera?
    15·1 answer
  • The operation:
    8·1 answer
  • This decision control structure enables the program to execute a statement or block of codes if and only if the Boolean expressi
    7·1 answer
  • URGENT!!! If Pete selects the green square or little green camera icon on his camera dial, what mode is he selecting?
    15·1 answer
  • Designing a mouse to fit well in your hand would demonstrate ..?
    14·2 answers
  • g How safe is to have a LinkedIn account where you have published all the important information about yourself
    13·2 answers
  • Who is better, Tom, Ben, Hank, Angela, or Ginger
    12·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!