root of a number

1

I would appreciate if someone could check my code, I just need to know why the function always returns r = 0. What my program does is through a series of operations calculate the root of an integer A, having previously entered this number and a tolerance allowed. The problem is that the answer always gives me is 0.00. I can not find the error Thank you. The code is:

    #include<stdio.h>
    #include<math.h>
    float calculo_raiz(float,float);
    int main()
   {
float rta;
float numero;
float tol;
printf("\n\t\t ¿¿¿ AVERIGUA LA RAIZ CUADRADA ???\n\n" );
printf("Usted desea averiguar la raiz del numero:");
scanf("%f",&numero);
printf("\nIngrese la tolerancia que admitira en el resultado:" );
scanf("%f",&tol);
rta=calculo_raiz(numero,tol);
printf("\nLa raiz de %.2f es %.2f\n",numero,rta);

return 0;
   }

  float calculo_raiz(float num,float tolerancia)
   {
float r=0;
float ra=1;
int inc=1;

while(inc>=1)
{
    r=1/2*(ra+(num/ra));

    if((r-ra)<tolerancia)
    {
        return r;
    }
    else
    {
        inc++;
        ra=r;
    }
}
  }
    
asked by micaela 21.08.2017 в 21:57
source

4 answers

2

well, applying the tolerance only had to add parenthesis to the operation in the code that you have, 1/2 change it to 0.5 because that result to give 0, your code works with those changes @

#include<stdio.h>
#include<math.h>
float calculo_raiz(float,float);
int main()
{
float rta;
float numero;
float tol;
printf("\n\t\t ¿¿¿ AVERIGUA LA RAIZ CUADRADA ???\n\n" );
printf("Usted desea averiguar la raiz del numero:");
scanf("%f",&numero);
printf("\nIngrese la tolerancia que admitira en el resultado:" );
scanf("%f",&tol);
rta=calculo_raiz(numero,tol);
printf("\nLa raiz de %.2f es %.2f\n",numero,rta);

return 0;
}

float calculo_raiz(float num,float tolerancia)
{
float r=0;
float ra=1;
int inc=1;

while(inc>=1)
{
r=(0.5*(ra+(num/ra)));

if((r-ra)<tolerancia)
{
    return r;
}
else
{
    inc++;
    ra=r;
}
}
}
    
answered by 21.08.2017 в 22:48
1

The problem is 1/2 since you are dividing integers and the result will return it as a whole:

1 / 2 = 0 .5

Put it with decimals so that you can return a float

1.0 / 2.0 = 0.5

    
answered by 21.08.2017 в 23:04
0

I made some adjustments, I do not understand for you to occupy the tolerance, I also add the function for the square root.

    #include<stdio.h>
    #include<math.h>
    float calculo_raiz(float,float);

    int main(){
        float rta;
        float numero;
        float tol;
        float n;
        printf("\n\t\t ¿¿¿ AVERIGUA LA RAIZ CUADRADA ???\n\n" );
        printf("Usted desea averiguar la raiz del numero:");
        scanf("%f",&numero);
        printf("\nIngrese la tolerancia que admitira en el resultado:" );
        scanf("%f",&tol);
        rta=calculo_raiz(numero,tol);
        printf("\nLa raiz de %.2f es %.2f\n",numero,rta);
        n=sqrt(numero);
        printf("\n Con sqrt La raiz de %.2f es %.2f\n",numero,n);
        return 0;
   }

    float calculo_raiz(float num,float tolerancia){
        float ra,aux; 
        int inc;
        ra=num; 
        float r=0;
        inc=1;
        while(inc){
            aux=num/ra;
            if(ra==aux){
                return ra;
                inc=0;
            }else{
                ra=(0.5)*((num/ra)+ra); 
            }
        }
    }
    
answered by 21.08.2017 в 23:04
0

The problem is that in the part of the calculation where you use (1/2) .

28: r=(1/2)*(ra+(num/ra));

When performing this arithmetic operation, the compiler interprets the operation as a division between integers, resulting in 0 .5 . Therefore, by throwing 0 as a result of that operation and multiplying it by the rest of the expression, the value of r is converted to 0 .

In this Stack Overflow question in English you will see a better explanation of the topic:

  

Why is (1/2) * x different from 0.5 * x?

For the compiler to take it as a floating number, or with a decimal part, you have to change the values by:

  

(1/2) per (1.0/2.0)

     

or

     

(1/2) per 0.5

This topic is explained in the next chapter of the course:

9. Conversion of types.

  

In these cases, when the operands of each binary operation   associated with an operator are of different types, the compiler   convert to a common type. There are rules that govern these   conversions, and although they can change slightly from a compiler to   another, in general they will be more or less like this:

     
  • Any small integer type such as char or short is converted to int or unsigned int . At this point any pair of operands will be int (with or without a sign), long , long long , double , float or long double .
  •   
  • If one operand is long double , the other will be converted to long double .
  •   
  • If one operand is double , the other will be converted to double .
  •   
  • If one operand is float , the other will be converted to float .
  •   
  • If an operand is unsigned long long , the other will be converted to unsigned long long .
  •   
  • If one operand is long long , the other will be converted to long long .
  •   
  • If one operand is of type unsigned long , the other will be converted to unsigned long .
  •   
  • If one operand is long , the other will be converted to long .
  •   
  • If an operand is of type unsigned int , the other will be converted to unsigned int .
  •   
  • At this point both operands are int .
  •   

    This case can be explained with an example of the course citation:

    4. Operators I.

      

    Let's see, for example, the expression 17/7, that is 17 divided between   7. When we learned to divide, before we knew how to draw decimals, we were taught that the result of this operation was 2, and   the rest 3, that is 2 * 7 + 3 = 17.

         

    In C ++, when the expressions that intervene in one of these   operations are whole, the result will also be whole, that is,   if 17 and 7 are stored in whole variables, the result will be whole,   in this case 2.

         

    On the other hand if the expressions are floating point, with decimals,   the result will be floating point, that is, 2.428571. In this case:   7 * 2.428571 = 16.999997, that is, there is no rest, or it is very small.

         

    For that reason, calculating the rest, using the operator%, only has   meaning if the expressions involved are whole, since in case   otherwise as many decimals will be calculated as the precision of the   type used.

         

    Continuing with the example, the expression 17% 7 will result in 3,   which is the rest of the entire division of 17 divided by 7.

    Keeping your code correct:

    #include<stdio.h>
    #include<math.h>
    
    float calculo_raiz(float,float);
    
    int main()
    {
        float rta;
        float numero;
        float tol;
        printf("\n\t\t ¿¿¿ AVERIGUA LA RAIZ CUADRADA ???\n\n" );
        printf("Usted desea averiguar la raiz del numero:");
        scanf("%f",&numero);
        printf("\nIngrese la tolerancia que admitira en el resultado:" );
        scanf("%f",&tol);
        rta=calculo_raiz(numero,tol);
        printf("\nLa raiz de %.2f es %.2f\n",numero,rta);
    
        return 0;
    }
    
    float calculo_raiz(float num,float tolerancia)
    {
        float r=0;
        float ra=1;
        int inc=1;
    
        while(inc>=1)
        {
            r=(1.0/2.0)*(ra+(num/ra));    // ó r=0.5*(ra+(num/ra));
    
            if((r-ra)<tolerancia)
            {
                return r;
            }
            else
            {
                inc++;
                ra=r;
            }
        }
    }
    
        
    answered by 22.04.2018 в 22:34