Results 1 to 8 of 8

Thread: Issue with algebra in C

  1. #1
    Join Date
    Feb 2011
    Location
    Great White North
    Beans
    128
    Distro
    Kubuntu 11.10 Oneiric Ocelot

    Issue with algebra in C

    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:
    Code:
    
    #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
    Last edited by alegomaster; February 22nd, 2012 at 02:12 AM.
    VHDL and C FTW

    Designing a custom CPU based on MIPS

  2. #2
    Join Date
    Nov 2005
    Location
    Sendai, Japan
    Beans
    11,296
    Distro
    Kubuntu

    Re: Issue with algebra in C

    Do you know about Valgrind?
    「明後日の夕方には帰ってるからね。」


  3. #3
    Join Date
    Jun 2007
    Location
    Paraparaumu, New Zealand
    Beans
    Hidden!

    Re: Issue with algebra in C

    What value does t have when the loop starts?
    Forum DOs and DON'Ts
    Please use CODE tags
    Including your email address in a post is not recommended
    My Blog

  4. #4
    Join Date
    Feb 2011
    Location
    Great White North
    Beans
    128
    Distro
    Kubuntu 11.10 Oneiric Ocelot

    Re: Issue with algebra in C

    Quote Originally Posted by lisati View Post
    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.
    VHDL and C FTW

    Designing a custom CPU based on MIPS

  5. #5
    Join Date
    Jun 2007
    Location
    Paraparaumu, New Zealand
    Beans
    Hidden!

    Re: Issue with algebra in C

    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.
    Forum DOs and DON'Ts
    Please use CODE tags
    Including your email address in a post is not recommended
    My Blog

  6. #6
    Join Date
    Nov 2005
    Location
    Sendai, Japan
    Beans
    11,296
    Distro
    Kubuntu

    Re: Issue with algebra in C

    Quote Originally Posted by alegomaster View Post
    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?

    Quote Originally Posted by lisati View Post
    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.
    「明後日の夕方には帰ってるからね。」


  7. #7
    Join Date
    May 2010
    Location
    uk
    Beans
    9,249
    Distro
    Xubuntu 14.04 Trusty Tahr

    Re: Issue with algebra in C

    Hi

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

    Start with t = 1.

    Kind regards
    If you believe everything you read, you better not read. ~ Japanese Proverb

    If you don't read the newspaper, you're uninformed. If you read the newspaper, you're mis-informed. - Mark Twain

    Thinking about becoming an Ubuntu Member?

  8. #8
    Join Date
    Feb 2011
    Location
    Great White North
    Beans
    128
    Distro
    Kubuntu 11.10 Oneiric Ocelot

    Re: Issue with algebra in C

    Quote Originally Posted by matt_symes View Post
    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.
    VHDL and C FTW

    Designing a custom CPU based on MIPS

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •