Home > Back-end >  Math.h function problem in c in code :: blocks
Math.h function problem in c in code :: blocks

Time:12-04

I wrote this code in C and it shows me this error. I don't know what it is or how can I solve it.

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

int main()
{
    int z;
    scanf("%d", &z);
    double x1 , x2 , x3 , x4 , y1 , y2 , y3 , y4;
    for(int i = 0;i<=z;i  )
        {
    scanf("%lf %lf", &x1 , &y1);
    scanf("%lf %lf", &x2 , &y2);
    scanf("%lf %lf", &x3 , &y3);
    scanf("%lf %lf", &x4 , &y4);
    double tule_parekhat1 = sqrt(pow(y2-y1, 2)   (pow(x2-x1), 2));
    double tule_parekhat2 = sqrt(pow(y3-y2, 2)   (pow(x3-x2), 2));
    double tule_parekhat3 = sqrt(pow(y4-y1, 2)   (pow(x4-x1), 2));
    double tule_parekhat4 = sqrt(pow(y4-y3, 2)   (pow(x4-x3), 2));

    }

}

I get the error (line 15, error : too few arguments to function 'pow')

I don't know what it is.

CodePudding user response:

You have wrong usage of parentheses.

double tule_parekhat1 = sqrt(pow(y2-y1, 2)   (pow(x2-x1), 2));

in the (pow(x2-x1), 2)) part, it should be

double tule_parekhat1 = sqrt(pow(y2-y1, 2)   pow(x2-x1, 2));

function pow sees only x2-x1

CodePudding user response:

Your code contains:

double tule_parekhat1 = sqrt(pow(y2-y1, 2)   (pow(x2-x1), 2));

with 2 call of pow function. In first call you correctly write pow(y2-y1, 2), actually calling the function with 2 parameters. But in second one, you write:

(pow(x2-x1), 2)

Here you only call pow with the single x2-x1 parameter and then use the comma (,) operator to discard that value and only retain the second one. This is incorrect and the compiler correctly raises an error.

You should write pow(x2-x1, 2) as you did for first call.


BTW, and IMHO you are abusing the pow function here. It is intended to be used for complex operation where both the operands are floating point values. Here the idiomatic way would be:

double tule_parekhat1 = sqrt((y2 - y1) * (y2 - y1)   (x2 - x1) * (x2 - x1));
...

using only addition and product operators.

  •  Tags:  
  • c
  • Related