Question

Write a function isqrt that computes the integer square root of a nonnegative inte- ger. Write a unit test that adequately exercises your function. The function prototype should be int isqrt (int n, int isr) Your function should return 0 if successful and -1 otherwise. Lack of success means that n is negative or s is a NULL pointer. In all other cases, isqrt should write the result to the location pointed by isr This is a non-trivial problem at this stage of your programming careers. Lets solve it one step at a time. First, lets more precisely define what you need to do. The integer square root of a nonnegative integer n is the largest integer s such that s2 S n. For example, the integer square root of all integers between 25 and 35 (both included) is 5, but the integer square root of 24 is 4 and the integer square root of 36 is 6. The largest integer that is not greater than is called the floor of z and is often written d. With this notation, our problem asks you to compute LVnj Our strategy is quite simple: We run through all the perfect squares 0 0 0, 1.1-1. 2 2 4, 3. 3 9, 4.4-16, 5 5 25,I could not figure how to do it and tried times and times and still could finish under gcc. so please help in c language.thanks

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The following C code gives you the required function along with main function but you will require only isqrt function

#include<stdio.h>
int main()
{
int n;
printf("Enter a positive number: ");
scanf("%d",&n);
int res, *ptr;
ptr = &res;
int d = isqrt(n, ptr);
if(d==-1)
printf("Error");
else
printf("Res = %d", *ptr);
return 0;
}//end of main function

int isqrt(int n, int *isr)
{
if(n<0 || isr == NULL)
return -1;
int i,res, sum = 0;

for(i=1;sum<=n; i+=2)
{
sum +=i;
res = i/2;
}
* isr = res;
return 0;
}

Output:

Enter a positive number: 34 6 42 Press any key to continue.

Enter a positive number: 36 Res-6 Process returned e (exe) execution time: 3.961 s Press any key to continue. 938

Hope it helps, do give your response.

Add a comment
Know the answer?
Add Answer to:
I could not figure how to do it and tried times and times and still could...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Consider the following program that reads a number of nonnegative integers into an array and prints...

    Consider the following program that reads a number of nonnegative integers into an array and prints the contents of the array.   Complete the missing parts, add new function prototypes and function definitions, and test the program several times. Add the following to the program: Write a void function that prints the list of nonnegative integers in reverse. Write a void function that prints all the numbers stored in the list that are greater than 10. It will also print the...

  • Your code must approximate the square root of an integer between 0 and 2^31-1 Using integer...

    Your code must approximate the square root of an integer between 0 and 2^31-1 Using integer maths is sufficient (no floating point or fractions are required); your code will return the truncated (integer portion) of the square root. Your code must be in an assembly language subroutine which is called by a C function for testing. Be sure to use registers according to the ARM calling convention. Base your software on the following pseudocode: Approximate square root with bisection method...

  • In ASCII C programming write a function that determines if an integer is prime. A prime...

    In ASCII C programming write a function that determines if an integer is prime. A prime number is one that is not divisible by any number other than one and itself. int isPrime(long num); Return 1 if num is prime, 0 if not. As above, make this a pure function contained in its own file isPrime.c and test it with a separate tester program. To test if an integer is prime you could implement a loop that generates trial divisors...

  • if it is possible help me with the code in C language and I could learn...

    if it is possible help me with the code in C language and I could learn it, thanks ! Write a function that computes the integer mean of an array of integers. For example, the integer mean of -1, 4, 2 is (-1 + 4 + 2)/3 = 5/3 = 1, where/des integer division. The function should implement the following specification:/* Computes the integer mean of an array and stores it * where mn references. Returns -1 for erroneous input...

  • when i run it is still be "false" i don't know why. could you help me...

    when i run it is still be "false" i don't know why. could you help me fix it and tell me which part is wrong??? specific answer is better thanks!!! Problem 6: (4 pts): Write a function that takes as an input parameter a string. It should return a Boolean value indicating whether the string is a palindrome or not. INPUT: “mom"->true “palindrome”->false “amanaplanpanama"->true 3 #include <iostream> #include <string.h> I using namespace std; string function(string str); int main() { function("palindrome");...

  • Tried numerous times but I think I'm doing this problem wrong. Will give thumb's up for...

    Tried numerous times but I think I'm doing this problem wrong. Will give thumb's up for help. JAVA starter code: import java.util.Scanner; public class ArraySorter { public static void main(String[] args) { Scanner sc = new Scanner(System.in);    // Read in k, which represents the maximum // distance between a number's current position // and sorted position int k = Integer.parseInt(sc.nextLine());    // Read in the list of numbers int[] numbers; String input = sc.nextLine(); if (input.equals("")) { numbers =...

  • I do not know how to code this. I have tried several times. Instructions CountByAnything.java +...

    I do not know how to code this. I have tried several times. Instructions CountByAnything.java + Modify the CountByFives application so that the user enters the value to count by. Start each new line after 10 values have been displayed. 1 public class CountByAnything 2 { // Modify the code below 4 public static void main(String args[]) { Grading final int START; final int STOP = 500; final int NUMBER_PER_LINE = 10; for(int i = START; i <= STOP; i...

  • in c++ language 1.Re-write the following for loop statement by using a while loop statement: int...

    in c++ language 1.Re-write the following for loop statement by using a while loop statement: int sum = 0; for(int i=0;i<=1000;i++){                 sum+=i; } 1 (b). Following is the main () function of a program. The program asks a user to enter 150 integers and print the largest integer user entered. int main() {    int score, highest;             //missing portion of the program             return 0;    } 1(c). Find the missing portion of the following code, so the...

  • in C++ and also each function has its own main function so please do the main...

    in C++ and also each function has its own main function so please do the main function as well. Previously when i asked the question the person only did the function and didn't do the main function so please help me do it. 1-1. Write a function that returns the sum of all elements in an int array. The parameters of the function are the array and the number of elements in the array. The function should return 0 if...

  • Exercise 4 – Passing an argument to a new thread and reporting on execution times================...

    Exercise 4 – Passing an argument to a new thread and reporting on execution times===================================================================== Here are some miscellaneous items on threads and resource usage times. This is a very short exercise to learn how to pass an argument to a thread’s start function. Write a program called threadArgA.c that simply does the following: Pass a simple integer to a thread’s start function at thread creation time. ii) The thread function will assign this argument as variable int i (just...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT