PDA

View Full Version : [SOLVED] Issue with algebra in C



alegomaster
February 22nd, 2012, 01:41 AM
I am working on a practice question for programming and it tells me to implement a specific mathematical function in it. The issue is I am always getting 0 for my output. The code is below:




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

int main ()
{
double A;
double H;
double M;
double t=0;

scanf("%lg", &H);
scanf("%lg", &M);

while (M!=t)
{
A= -6*pow(t,4)+H*pow(t,3)+2*pow(t, 2)+t; //The mathematical Function

if (A<=0)
{
printf("You touched the ground at: %lg", t);
goto end;
}
else
{
t++;
}
}
printf("The ballon didn't touch the ground");
end:
return 0;
}


I am wondering if someone can help me with this code, I am not familiar with using algebra in C

Bachstelze
February 22nd, 2012, 01:56 AM
Do you know about Valgrind?

lisati
February 22nd, 2012, 02:05 AM
What value does t have when the loop starts?

alegomaster
February 22nd, 2012, 02:12 AM
What value does t have when the loop starts?

In my code I assigned it 0, but I removed it here to make the code look cleaner.

lisati
February 22nd, 2012, 02:16 AM
The way the code as posted stands, t is uninitialised, which means that it could contain any old rubbish.

My bad, I took another look.

Bachstelze
February 22nd, 2012, 02:17 AM
In my code I assigned it 0, but I removed it here to make the code look cleaner.

Well, if t is 0, what do you think A will be?


The way the code as posted stands, t is uninitialised, which means that it could contain any old rubbish.

My bad, I took another look.

He edited it, it was uninitialised, though that's not the real problem.

matt_symes
February 22nd, 2012, 02:25 AM
Hi

t = 0 is the problem here. A will always be 0 if t is 0.

Start with t = 1.

Kind regards

alegomaster
February 22nd, 2012, 03:21 AM
Hi

t = 0 is the problem here. A will always be 0 if t is 0.

Start with t = 1.

Kind regards

Thanks, it works great now. I must have spent too much time thinking that the
A= (-6)*pow(t,4)+H*pow(t,3)+2*pow(t, 2)+t; line was incorrect.