423,485 Members | 1,664 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 423,485 IT Pros & Developers. It's quick & easy.

functions and pointers

P: 4
im have to write a program that calculates the square root of a number by using functions and pointers. If the number is less then 0, the function squareRoot should return 0 and print in main that the number cant be calculated. If the number is larger than 0, the function squareRoot should return 1 and print the answer in main


#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int squareRoot(int *number)
{
if (number < 0)
{
return 0;
}
else if (number >= 0)
{
*number = sqrt(*number);
return 1;
}
}


int main()
{
int *num;
printf("Type in a number: ");
scanf("%d", &num);
squareRoot(&num);
printf("Square root = %d\n", num);
return 0;
}
2 Weeks Ago #1

✓ answered by donbock

In squareRoot, comparison of number to 0 should be comparing *number; I suggest replacing else if with else.

In main, you should not print square root value if squareRoot returns an error. Definition of num has wrong type - there should be a compiler error.

I donít know what @weaknessforcats means regarding square root not working for floating point values. Actually, the function prototype for sqrt requires you to use type double. It is your decision whether you want the program to compute square roots of integer or floating point user inputs. If the input is integer then you have to assign that value to a double variable before calling sqrt.

Share this Question
Share on Google+
3 Replies


weaknessforcats
Expert Mod 5K+
P: 9,180
Floating point numbers do not have square roots. Only integers do this.

The relational operators don't work with floating point. When you compare two floats you need also supply a tolerance of error and this is done using special calls.


Please remove floating point and replace with integer.



float type sould never be used uness you have low accuracy scientific results supplied by out-of-date software. the replacement for float is double.


If you don't have the above, you use int. Or long. But not float.
2 Weeks Ago #2

Expert 100+
P: 2,384
In squareRoot, comparison of number to 0 should be comparing *number; I suggest replacing else if with else.

In main, you should not print square root value if squareRoot returns an error. Definition of num has wrong type - there should be a compiler error.

I donít know what @weaknessforcats means regarding square root not working for floating point values. Actually, the function prototype for sqrt requires you to use type double. It is your decision whether you want the program to compute square roots of integer or floating point user inputs. If the input is integer then you have to assign that value to a double variable before calling sqrt.
1 Week Ago #3

weaknessforcats
Expert Mod 5K+
P: 9,180
If an integer, like 25 has an integer, like 5, that can be multiplied by itself, then the 5 is called a square root and the 25 is called a perfect square.

Since floating point numbers cannot be expressed as the ratio of two integers, they are called irrational. These numbers, not being perfect square, do not have a square root. Now you can approximate, apply rounding rules, etc and guess a "square root". But unless the original number is the ratio of two integers, and a perfect square, this guess is not a square root.


In addition both 0 and negative numbers don't have square roots either.


A good example is trying to find the square root of 2.
1 Week Ago #4

Post your reply

Sign in to post your reply or Sign up for a free account.