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
The name of a person their address and their contact information like phone number and email address or consider the minimum inf
kozerog [31]

The name of a person their address and their contact information like phone number and email address or consider the minimum information or a desktop publishing should include on a letterhead or personal use is true.

True

<u>Explanation:</u>

Since it is used as personal letterhead, end user should have contact details as minimum, which should carry name, phone number and email address.

Letter head should have more space for end user to type or draft the text. If letter head occupies more space end user has limited to space for typing text or draft a letter.

Mostly letter head should simple which should carry less space for letter head. On letter head normally use one color or simple color and it should more attractive.

5 0
3 years ago
Bayley wants to connect a new external hard drive to his Windows PC. He wants the fastest connection type available because he p
PIT_PIT [208]

Answer:

Thunderbolt 3

Explanation:

Based on the scenario being described it can be said that in order to get the fastest connection possible Bayley needs a Thunderbolt 3 connection. This type of connection has become the standard due to it's theoretical maximum throughput of roughly 40 Gbps. This puts it at roughly double the speed of it's competing connection types, making it the fastest and best option.

8 0
3 years ago
Read 2 more answers
What sort of negative outcomes are possible for this type of risk?
Harlamova29_29 [7]
More details please?
6 0
3 years ago
Read 2 more answers
Select all ways that databases may help you manage and manipulate data and records.
MakcuM [25]
Query data
Arrange data in certain order
Update information in more than one place at the same time
Merge data into a word processing document

Calculate loan payment is something you could possibly make a database do, but more likely you would simply use a spreadsheet. Databases are mostly for for storing, retrieving, sorting and manipulating data.
8 0
4 years ago
Write a program that asks the user to enter a series of single digit numbers with nothing separating them. Read the input as a C
vladimir2022 [97]

Answer:

#include <stdio.h>

#include <string.h>

int main(){

   char number[100];

   printf("Number: ");

   scanf("%s", number);

   int sum = 0;

   for(int i =0;i<strlen(number);i++){

       sum+= number[i] - '0';

   }

           printf("Sum: %d",sum);

   return 0;

}

Explanation:

This declares a c string of 100 characters

   char number[100];

This prompts user for input

   printf("Number: ");

This gets user input

   scanf("%s", number);

This initializes sum to 0

   int sum = 0;

This iterates through the input string

   for(int i =0;i<strlen(number);i++){

This adds individual digits

       sum+= number[i] - '0';

   }

This prints the calculated sum

           printf("Sum: %d",sum);

   return 0;

4 0
3 years ago
Other questions:
  • What is the approximate area of the figure? A. 129 square inches B. 113 square inches C. 110 square inches D. 44 square inches
    15·1 answer
  • How do I sign into raterhub on my phone and my mobile device at the same time to complete phase 2 of my raters exam for sykeshom
    10·1 answer
  • Drugs of addiction act upon a portion of the Brain called the lambic system ? True or false.
    7·1 answer
  • Cual es la herramienta de google que funciona como oficce ?
    11·1 answer
  • __________ are hosted on dedicated computers known as 'web servers'.​
    8·2 answers
  • Write a program to accept 10 different whole number from the user and store them in a
    11·1 answer
  • High level languages are closer to machine language than humans yes or no​
    12·2 answers
  • You are an IT network administrator at XYZ Limited. Your company has critical applications running of its Ubuntu Linux server. C
    6·1 answer
  • Consider the following code.
    7·2 answers
  • Select the correct answer.
    5·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!