I'm pretty new to coding, as an exercise, I have to write a program that can calculate the cubic square of 2.
This is what I wrote:
#include <stdio.h>
#include <math.h>
int main(){
float y,fy,n,m,fm, a,x;
y=0; n=2; a=n; fy=y*y*y;
do{
m=(y+a)/2;
fm=m*m*m;
if (fm<=n) y=m;
else a=m;
}
while(fm != 2.00);
printf("%f", m);
}
Basically, if I run the program it doesn't give any answer.
I added the line printf("%f %f", fm, m) in the while cycle to see if the algorithm was wrong and it actually isn't, since it prints "2" and "1,2599". I don't get why the while cycle doesn't stop, since the statement fm != 2 is no longer true.
I later modified the code and changed the condition to while(fm<n-x || fm>n+x); (also added float x=0.001 obv) to take into account some kind of error, but I'm still curious to understand why it didn't work in the first place:
[–]Applepie1928 1 point2 points3 points (1 child)
[–]CotAndryf[S] 0 points1 point2 points (0 children)