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
A videogame designer would be most interested in the _____ elements of beowulf.
adelina 88 [10]
I think he would be interested in a. epic elements of beowulf.
5 0
3 years ago
Read 2 more answers
Which of these is the most likely result if the internet download speed in your home is 25 Mbps?
sashaice [31]

Answer:

Streaming movies may stop to buffer

Explanation:

microphones have a set record limit, and games downloaded wont generate errors unless something in the code is wrong, printers don't really need to connect to internet to work except for some, and streaming movies buffer because as you are watching them it downloads the next few minutes.

3 0
3 years ago
What is the next line? &gt;&gt;&gt; tupleB = (5, 7, 5, 10, 2, 7) &gt;&gt;&gt; tupleB.count(7) 1 0 5 2
dybincka [34]

Answer:

The right answer is option 4: 2

Explanation:

Lists are used in Python to store elements of same or different data types.

Different functions are used in Python on List. One of them is count.

Count is used to count how many times a specific value occurs in a list.

The syntax for count is:

listname.count(value)

In the given code,

The output will be 2

Hence,

The right answer is option 4: 2

3 0
3 years ago
Is the following statement true or false information literacy is a subset of health literacy
BARSIC [14]

the answer is false because information literacy is a whole other type. you don't need it for the structure of health literacy.                              

4 0
3 years ago
Which of the following is the MOST important consideration when planning your budget?
nata0808 [166]
<span>Budget for fixed expenses before flexible expenses.</span>
5 0
3 years ago
Read 2 more answers
Other questions:
  • Which one of these students is having a positive impact?
    8·2 answers
  • What are advantages and disadvantages of developing a more robust Internet of Things and continuing to add more smart nodes?
    10·1 answer
  • Which nmap switch is used to detect target OS and services?<br> a -S<br> b -p<br> c -F<br> d -A
    8·1 answer
  • In general, font size for software-generated presentation slides should be no smaller than __________ points.
    8·1 answer
  • Write a function in Java that implements the following logic: Given a string and an int n, return a string made of n repetitions
    15·1 answer
  • What is a flowchart​
    9·1 answer
  • Please help with my Python code - Functions
    6·1 answer
  • Assert statements are a tool programmers employ to help them debug their code more efficiently.
    6·1 answer
  • Which of the following is an example of phishing attack? csp
    8·1 answer
  • Write a Python program to find whether a given number (accept from the user) is positive
    11·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!