435,412 Members | 2,896 Online + Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,412 IT Pros & Developers. It's quick & easy.

functions and pointers

 P: 8 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 #include #include 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; } Sep 29 '18 #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.

3 Replies

 Expert Mod 5K+ P: 9,197 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. Sep 30 '18 #2

 Expert 100+ P: 2,400 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. Oct 4 '18 #3

 Expert Mod 5K+ P: 9,197 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. Oct 4 '18 #4 