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]
3 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]3 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]3 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
Which of the following software program provides for email communication
olga2289 [7]
If "Outlook" is one of the options then thats the answer.
6 0
3 years ago
Read 2 more answers
application that will perform a lot of overwrites and deletes of data and requires the latest information to be available anytim
o-na [289]

Answer:

RDS.

Explanation:

A corporation has implemented a software that conducts the number of information overwriting and deleting, which allows the newest details to be accessible whenever the information is examined. As a Software Engineer, they suggest RDS database technologies.

It is an AWS supported controlled SQL DB service that also reinforces the array type DB for the purpose to contain and maintain the information.

3 0
3 years ago
Henry is designing a document for his architecture project. In which order should Henry preform the different tasks that are req
nadezda [96]

It is

Select an appropriate layout

Decide which current format...

Import or compose...

Proofread...

Check the print preview..

4 0
3 years ago
Frames control what displays on the Stage, while keyframes help to set up
pogonyaev

Answer: A keyframe is a location on a timeline which marks the beginning or end of a transition. So for example, you have a movie and it transitions to another scene, keyframes tell it when and where to start the transition then when and where to stop the transition.

3 0
2 years ago
Which of the following statements invokes the GetDiscount function, passing it the contents of two Decimal variables named decSa
ryzh [129]

Answer:

c. decDiscount = GetDiscount(decSales, decRate)                                                                                                                          

Explanation:

Option a. is incorrect because it is using Call word which is not a valid way to invoke a function.

Similarly option b. is also incorrect because it uses Call word to invoke function GetDiscount() which is not a valid way to call a function and also it is passing it the contents of three variables decSales, decRate and decDiscount and as mentioned in the question only two parameters are to be passed to GetDiscount() function.

Option c. is correct as it invokes the function GetDiscount() and passes it the contents of two variables decSales and decRate and assigns this to a variable decDiscount. For example if the GetDiscount() method has to calculate the discount using decSales and decRate then the resultant value of this computation is assigned to decDiscount. So whatever this function returns or computers is assigned to and stored in decDiscount variable. So this is a valid way to invoke a method.

3 0
2 years ago
Other questions:
  • ACCOUNTING
    13·2 answers
  • A circuit has a resistance of 300 ohm and a current of 0.024 A. What is the voltage in the circuit?
    12·1 answer
  • What would you recommend for data segregation if using cloud software
    12·2 answers
  • Jenny is the project manager and her company. She needs to break her current project into parts that her employees can work on.
    10·1 answer
  • The invention of the transistor was important to the development of computers because
    14·1 answer
  • Henry conducted a survey on an ad done by his company. In the survey, he asked people to evaluate the ad and state whether they
    8·1 answer
  • Which type of financial institution typically has membership requirements?
    14·1 answer
  • What is pseudo code?
    11·2 answers
  • Pls help I will mark you the brainliest
    9·2 answers
  • You are making a game! The player tries to shoot an object and can hit or miss it. The player starts with 100 points, with a hit
    13·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!