Thread: Help with solving Order of Ops Question

  1. #1
    Registered User Psyzen's Avatar
    Join Date
    Jan 2012
    Location
    Canada
    Posts
    5

    Question Help with solving Order of Ops Question

    I'm stuck on a question in my assignment that's asking me to create a program to calculate the Area-cubed of a cylinder knowing that
    Pi = 3.14159 and Radius = 2.2
    the formula is 4/3 Pi R^3
    and this is the code I have written down:

    Code:
    #include<stdio.h.>
    #include<math.h>
    main()
    {
    float Pi = 3.14159, num1 = pow(2.2,3), area;
    area = 4 / 3 * num1;
    printf("%f", area);
    getch();
    }
    I'm getting different answers if I instead make the 4 / 3 into 1.25, so I was wondering how do I signify the operations in the order it should be in, if it should be according to PEMDAS(which i believe it should be)?

    Thanks in Advance!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Be wary of integer division here, e.g., change 4 / 3 to 4.0 / 3.0
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User Psyzen's Avatar
    Join Date
    Jan 2012
    Location
    Canada
    Posts
    5
    Quote Originally Posted by laserlight View Post
    Be wary of integer division here, e.g., change 4 / 3 to 4.0 / 3.0
    I changed the ints to floating point values and I'm getting a number like 14.121337 where it should be somewhere closer to 33-34

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Oh, but of course, mathematically, 4/3 is not equal to 1.25.

    Plus, I note that you computed 2.2 to the power of 3, but did not multiply by pi.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User Psyzen's Avatar
    Join Date
    Jan 2012
    Location
    Canada
    Posts
    5
    Quote Originally Posted by laserlight View Post
    Oh, but of course, mathematically, 4/3 is not equal to 1.25.

    Plus, I note that you computed 2.2 to the power of 3, but did not multiply by pi.
    Of course it doesn't! haha thanks for the help.

  6. #6
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Looks more like the formula of volume of sphere. If you are working with a cylinder where's the height parameter?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help solving this C programming question
    By C-user in forum C Programming
    Replies: 23
    Last Post: 10-20-2011, 02:39 AM
  2. Replies: 3
    Last Post: 10-18-2011, 12:07 PM
  3. Help solving a question
    By everyone0 in forum C Programming
    Replies: 9
    Last Post: 05-06-2010, 11:09 AM
  4. Help solving this loop question
    By everyone0 in forum C Programming
    Replies: 2
    Last Post: 05-05-2010, 11:33 AM
  5. Order of operations question
    By nicomp in forum C++ Programming
    Replies: 9
    Last Post: 09-20-2006, 07:18 PM