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
_________ is critical to Amazon's success.<br><br> SCM<br><br> JIT<br><br> ERP<br><br> VMI
Minchanka [31]

Answer: SCM

Explanation:

 SCM stand for supply chain management system and it is amazon's competitive strategy. The amazon SCM is the strategic of being the retailer according to the customer's choice.

Inventory system for multi-tier is the major component for the amazon's SCM. The main key aspect for the amazon supply chain management is that  it is responsible for development in the trade market over years.The amazon SCM are basically responsible for analysis all the major growth and productivity ij the market by using the supply chain management process.

8 0
2 years ago
A bag of cookies holds 40 cookies. The calorie information on the bag claims that there are 10 servings in the bag and that a se
laila [671]

Answer:

import java.util.Scanner;

public class ANot {

   public static void main(String[] args) {

   Scanner in = new Scanner (System.in);

       System.out.println("How many cookies did you eat today");

       int numOfCookies = in.nextInt();

       double numCalories = (numOfCookies*300)/4;

       System.out.println("The total number of calories you consumed in "+numOfCookies+" cookies is " +

               " "+numCalories);

   }

}

Explanation:

This code is implemented in Java.

  1. We know from the question that 4 cookies contain 300 calories
  2. Therefore number of calories consumed = (number of cookies eaten*300)/4
  3. To implement this in java we used the scanner class to prompt user for the input
  4. save the input to a variable and write mathematical expression for the number of calories consumed
  5. Then output the result

4 0
3 years ago
Are you concerned that strangers may have access to the personal information you share on social media platforms? Why or why not
GuDViN [60]
It makes me a little uneasy at times, yes, but I know that they cannot find me unless I share way too much.
Hope this helps~!
~{Oh Mrs. Believer}
4 0
3 years ago
Dani needs to present a mathematical formula to his audience. How should he start the process of adding an equation to a slide?
bonufazy [111]

The principles of creating equations are the same in PowerPoint 2007 and later. Assuming Danny is using PowerPoint 2010, he will click on the insert tab then go ahead and choose the Equation in the symbols group. 

Dani will be able to click on the equation option and use the Equation Tools Design tab or click on the drop down arrow to view more equations






3 0
2 years ago
Write a program to calculate the total salary of the employees in an office. The program will first prompt the user to enter the
raketka [301]

Answer:

import java.util.Scanner;

public class Salary

{

public static void main(String[] args) {

   

    int numberOfEmployees;

    double payRate, numberOfHours, totalPay = 0;

       Scanner input1 = new Scanner(System.in);

       Scanner input2 = new Scanner(System.in);

       System.out.print("Enter the number of employees: ");

       numberOfEmployees = input1.nextInt();

       

       for(int i=1; i<=numberOfEmployees; i++) {

           System.out.print("Enter the pay rate for employee " + i + ": ");

           payRate = input2.nextDouble();

           System.out.print("Enter the number of hours worked: ");

           numberOfHours = input2.nextDouble();

           

           totalPay += (payRate * numberOfHours);

       }

       

       System.out.println("Total payment is: "+ totalPay);

}

}

Explanation:

- Declare the variables

- Ask the user the for the number of employees

- Inside the for loop, ask the user for pay rate and hours worked for each employee

- Calculate the total pay

- <u>Outside of the loop</u>, print the total pay

6 0
3 years ago
Read 2 more answers
Other questions:
  • You just turned on a four port Ethernet switch (it hasn’t learned any addresses yet) and connected a host to each port. You send
    8·1 answer
  • 7. What is the school campus’s setting<br> the school is sanford
    6·2 answers
  • Drag each storage device to its category.
    7·1 answer
  • In order for Dr. Reynolds to send a CPOE from her office computer system to the computer system at the local hospital, a/an ____
    5·1 answer
  • A browser is used for creating Web pages. true or false?
    5·2 answers
  • Encrypting text allows us to encrypt and decrypt the text using a special key.
    9·1 answer
  • Please help thank u!!!!!
    13·2 answers
  • Write a program named as reverse.c that reads a message, then prints the reversal of the message. The output of the program shou
    7·1 answer
  • You were recently hired by a small start-up company. The company is in a small office and has several remote employees. You have
    15·1 answer
  • Which of the following is NOT a function of a Web Browser?
    9·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!