For the function
isqroot()
to calculate the square root using Babylonian method with one degree of precision and return it in a struct.
I'm unable to return the value to the struct and when I compile it is returning garbage value.
Here is my code:
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
struct rootStruct {
int rootInt;
int rootFraction;
};
typedef struct rootStruct RootStruct;
RootStruct* isqroot (int n) {
/*We are using n itself as initial approximation*/
RootStruct* root=(RootStruct* )malloc(sizeof(RootStruct));
float x = n;
float y = 1;
float e = 0.1; /* e decides the accuracy level*/
while(x - y > e) {
x = (x + y)/2;
y = n/x;
}
root->rootInt = (int)x/2;
root->rootFraction = (int)(x-root->rootInt)*100;
return root;
}
int main(){
RootStruct* roo+t=(RootStruct* )malloc(sizeof(RootStruct));
printf("the sqrt is %d\n%d\n",root->rootInt,root->rootFraction);
return 0;
}
What is wrong with this code?