abdullah karabulut
abdullah karabulut

Reputation: 11

pythagoras theorem validation on C

What am I doing wrong here ?

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

int main()
{
    double pythagoras (double x, double y);
    int x[] = {3,3,1, -1, -1, 1, -3, 0, 6};
    int y[] = {4, -4, 1,1,-1,-1,0,4,9};
    {
        double z;
        for(int i = 0; i<sizeof(x)/sizeof(int); i++)
        {
            z = pythagoras(x[i],y[i]);
            z = sqrt(double pow(x,2) + double pow(y,2));
            printf("%3d, %3d, %9.5f\n" , x[i] ,y[i] , z);
        }
        printf("%3d, %3d, %9.5f\n" , 5,12,pythagoras(5,12) );
        return 0;
    }
}

Upvotes: 0

Views: 174

Answers (1)

Satish Pokala
Satish Pokala

Reputation: 21

Try defining your pythagoras function like this...

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

double pythagoras(int,int); //Function Declaration

int main(){
    int x[] = {3,3,1,-1,-1,1,-3,0,6};
    int y[] = {4,-4,1,1,-1,-1,0,4,9};
    double z;
    for(int i=0; i<sizeof(x)/sizeof(int); i++){
        z = pythagoras(x[i],y[i]); // Function Call
        printf("%3d, %3d, %9.5f\n", x[i], y[i], z);
    }
    printf("%3d, %3d, %9.5f\n", 5, 12, pythagoras(5,12)); //Function Call
    return 0;
}

//Function Definition
double pythagoras(int x, int y){
    return sqrt(pow(x,2) + pow(y,2));
}

Upvotes: 1

Related Questions