functions and pointers

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ZS369
    New Member
    • Aug 2018
    • 8

    functions and pointers

    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;
    }
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    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.

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      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 @weaknessforcat s 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.

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        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.

        Comment

        Working...