Question

Using C++ Write a template function that accepts an integer parameter and returns its integer square...

Using C++

Write a template function that accepts an integer parameter and returns its integer square root.
The function should return -1, if the argument passed is not integer.
Demonstrate the function with a suitable driver program .

Homework Answers

Answer #1

The answer to this question is as follows:

The code is as follows:

#include<bits/stdc++.h>
using namespace std;
int Sqrt(int num)
{
  
   if (num == 0 || num == 1)
   return num;
int x=num;
int y=(x+1)/2;
while(y<x)
{
x=y;
y=(x+num/x)/2;
}
return x;
}


int main()
{
   int x = 20;
   cout << Sqrt(x) << endl;
   return 0;
}

The input and output are provided in the screenshot below:

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write a method called countChar which accepts a string parameter and a character parameter and returns...
Write a method called countChar which accepts a string parameter and a character parameter and returns an integer, that is: int countChar (String s, char c) This method should count the number of occurrences of the character c within the string s, and return the count to the caller. Also write a main method that tests countChar. All of the print statements and user interaction belong in the main method, not in countChar.
Write a program containing a function, reverseDigit, that takes an integer as a parameter and returns...
Write a program containing a function, reverseDigit, that takes an integer as a parameter and returns the number with its digits reversed, then printout the return result. For example, the value of reverseDigit(12345) is 54321; the value of reverseDigit(5600) is 65; the value of reverseDigit(7008) is 8007; and the value of reverseDigit(-532) is -235.    Modify the following program to make a correct output. /* // Name: Your Name // ID: Your ID // Purpose Statement: ~~~ */ #include <iostream>...
Write a function called odd_rms that returns orms, which is the square root of the mean...
Write a function called odd_rms that returns orms, which is the square root of the mean of the squares of the first nn positive odd integers, where nn is a positive integer and is the only input argument. For example, if nn is 3, your function needs to compute and return the square root of the average of the numbers 1, 9, and 25. You may use built-in functions including, for example, sum and sqrt, except for the built-in function...
Write a template function maxn() that takes as its arguments an array of items of type...
Write a template function maxn() that takes as its arguments an array of items of type T and an integer representing the number of elements in the array and that returns the largest item in the array. The number of elements should take the default value of 10. The program should include a specialization that takes an array of strings as an argument and returns the longest string. (If there is a tie, the function should return the first one...
Write a function that accepts an int array and the array’s size as arguments. The function...
Write a function that accepts an int array and the array’s size as arguments. The function should create a new array that is twice the size of the argument array. The function should copy the contents of the argument array to the new array, and initialize the unused elements of the second array with 0. The function should return a pointer to the new array. Demonstrate the function by using it in a main program that reads an integer N...
Write a function called odd_rms that returns orms, which is the square root of the mean...
Write a function called odd_rms that returns orms, which is the square root of the mean of the squares of the first nn positive odd integers, where nn is a positive integer and is the only input argument. For example, if nn is 3, your function needs to compute and return the square root of the average of the numbers 1, 9, and 25. You may use built-in functions including, for example, sum and sqrt, except for the built-in function...
javascript 1.Write a function delay that accepts two arguments, a callback and the wait time in...
javascript 1.Write a function delay that accepts two arguments, a callback and the wait time in milliseconds. Delay should return a function that, when invoked waits for the specified amount of time before executing. HINT - research setTimeout(); 2.Create a function saveOutput that accepts a function (that will accept one argument), and a string (that will act as a password). saveOutput will then return a function that behaves exactly like the passed-in function, except for when the password string is...
Write a recursive method repeatNTimes(String s, int n) that accepts a String and an integer as...
Write a recursive method repeatNTimes(String s, int n) that accepts a String and an integer as two parameters and returns a string that is concatenated together n times. (For example, repeatNTimes ("hello", 3) returns "hellohellohello") Write a driver program that calls this method from the Main program. Program should ask for input and be stored in the string and should return n amount of times.
C++ only: Please implement a function that accepts a string parameter as well as two character...
C++ only: Please implement a function that accepts a string parameter as well as two character arguments and a boolean parameter. Your function should return the number of times it finds the character arguments inside the string parameter. When both of the passed character arguments are found in the string parameter, set the boolean argument to true. Otherwise, set that boolean argument to false. Return -1 if the string argument is the empty string. The declaration for this function will...
Write a function in C++ that takes an integer argument D and returns the index and...
Write a function in C++ that takes an integer argument D and returns the index and value of the first D-digit Lucas number. Turn in a print-out of your code, as well as the index and value your function returns when D = 15. Example) Let D = 3. The first 3-digit Lucas Number is L10 = 123, so your function should return something like firstDDigitLucas(3) = (10, 123).
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT