Thread: Using power

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    41

    Using power

    What did i do wrong here?

    I am using GNU emacs to compile

    Code:
    #include <math.h>
    #include <stdio.h>
    
    int main ()
    {
      double time; /* declare time as a power */;
      double g; /* declare g as a decimal */;
      double pi; /* declare pi as a decmal */;
      double length; /* declare length as a double value */
    
    time = 60;
    g = (32.260);
    pi = (3.1416);
    length = g * (time/pi);
    
    printf ("time/pi raised to the 2nd power is %f\n", pow(time/pi, 2));
    printf("\nThe length to complete one swing in one second is: %f, inches");
    
      return 0;
    }
    Last edited by Ken Fitlike; 09-16-2006 at 04:06 AM. Reason: code tags added

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    printf("\nThe length to complete one swing in one second is: %f, inches");
    Your closing double quote is misplaced.
    Code:
    printf("\nThe length to complete one swing in one second is: %f", inches);
    You might not want to have a variable called time because that's the name of a function in <time.h>.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Oct 2004
    Posts
    151
    Quote Originally Posted by wonderpoop
    What did i do wrong here?
    You didn't use [code][/code] tags, duh.
    System: Debian Sid and FreeBSD 7.0. Both with GCC 4.3.

    Useful resources:
    comp.lang.c FAQ | C++ FQA Lite

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >What did i do wrong here?
    >printf("\nThe length to complete one swing in one second is: %f, inches");
    length is missing from your argument list. Try:
    Code:
    printf("\nThe length to complete one swing in one second is: %f inches", length);

  5. #5
    Mistake is a way to learn
    Join Date
    Sep 2006
    Location
    Malaysia
    Posts
    11
    Next time, you might want to shorten your code by initialize all of the variable value. Like this:

    Code:
    #include <math.h>
    #include <stdio.h>
    
    int main ()
    {
      double time = 60 , g =32.260, pi = 3.1416 ,length;
    
    length = g * (time/pi);
    
    printf ("time/pi raised to the 2nd power is %f\n", pow(time/pi, 2));
    printf("\nThe length to complete one swing in one second is: %f inches",length);
    
      return 0;
    }
    That's just my suggestion btw.. A nice programming habits u know..

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    41
    thank you every1 so much. I am very new at this so ya i will ask dumb questions such as what is this error?

    "fig264a.c", line 9.84: 1506-186 (S) String literal must be ended before the end of line.

  7. #7
    Registered Luser risby's Avatar
    Join Date
    Jun 2006
    Posts
    72
    Quote Originally Posted by wonderpoop
    What did i do wrong here?
    Code:
      double length; /* declare length as a double value */
    
      g = (32.260);
    This is not a syntax error but is something to watch out for. Comments should be there so that when someone else, or yourself in six months time, come to read your code their understanding of the code is advanced by reading the comment.

    Declaring a variable and then commenting that you just declared a variable helps no-one.

    Assigning a value like 32.26 to a variable named g without explaining why in a comment also helps no-one.
    ===
    Don't grumble about what you can't have;
    be grateful you don't get what you deserve.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    41
    Quote Originally Posted by dwks
    Code:
    printf("\nThe length to complete one swing in one second is: %f, inches");
    Your closing double quote is misplaced.
    Code:
    printf("\nThe length to complete one swing in one second is: %f", inches);
    You might not want to have a variable called time because that's the name of a function in <time.h>.
    thankyou for that suggestion, I will be sure to ask my professor about it on monday

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    41
    #include <math.h>
    #include <stdio.h>

    int main ()
    {
    double time; /* time is 32.260ft/sec^2 */;
    double g;
    double pi;
    double length; /* length is how far the pendulum of a grandfather clock swings in one sec */;

    time = 120;
    g = 32.260;
    pi = 3.1416;
    length = g * pow(time/pi, 2);

    printf("\nThe length to complete one swing in one second is: %f", inches", length);

    return 0;
    }

    this is what i redid, i'm not sure if i should use pow as printf or as i did above. I still get that error though:

    "fig264a.c", line 17.84: 1506-186 (S) String literal must be ended before the end of line.
    "fig264a.c", line 17.73: 1506-276 (S) Syntax error: possible missing ')'?

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Press EDIT. Add code tags. Also, get an editor that has syntax hilighting, and you'd see where your problem is. Start counting " marks.


    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Especially on this line.
    Code:
    printf("\nThe length to complete one swing in one second is: %f", inches", length);
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    41
    I got it :`)

    it was the -lm
    The more i tried to find out the messier i made it. Thanks!!

    PS. this is my new college references. You guys and gals are great at tutoring without giving the answers, but suggestions.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem With My Box
    By HaVoX in forum Tech Board
    Replies: 9
    Last Post: 10-15-2005, 07:38 AM
  2. The destructive power of a nuclear bomb
    By InvariantLoop in forum A Brief History of Cprogramming.com
    Replies: 30
    Last Post: 03-24-2005, 02:46 AM
  3. Power supplies and demanding processors
    By Liger86 in forum Tech Board
    Replies: 12
    Last Post: 03-17-2005, 11:56 AM
  4. power recursion
    By datainjector in forum C Programming
    Replies: 2
    Last Post: 11-06-2002, 04:56 PM