Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Need Help With Calculation

Need Help With Calculation

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    WackoWolf
    wrote on last edited by
    #1

    The program works, but the calculation is wrong. The is written in C. Could someone tell me what I am doing wrong? Thank You #include "stdafx.h" #include #using using namespace System; // Function prorotypes void sphere_volume (double); // PI is a constant const double PI=3.14159; int _tmain() { // Integer local to Main to store initial radius value double r; printf("Please Enter A Positive Radius Value\n"); scanf("%lf", &r); // If Else Function To Make Sure The User Types A Positive and Not A Negative Integer. if (r >= 0) sphere_volume (r); else printf("You Must Enter A Positive Integer!!!! Not A Negative Integer!!!\n"); return 0; } // This Is The Fnction That Does The Calculations For The Volume. void sphere_volume (double radius) { double radius_cubed; double exponent=3; double volume; printf("This Is The Test To See If R Was Passed To The Function: %lf\n", radius); radius_cubed=pow(radius, exponent); volume=(4/3)*PI*radius_cubed; printf("The Volume Of The Sphere is %lf\n", volume); } Joseph L. Gelsomino

    R D 2 Replies Last reply
    0
    • W WackoWolf

      The program works, but the calculation is wrong. The is written in C. Could someone tell me what I am doing wrong? Thank You #include "stdafx.h" #include #using using namespace System; // Function prorotypes void sphere_volume (double); // PI is a constant const double PI=3.14159; int _tmain() { // Integer local to Main to store initial radius value double r; printf("Please Enter A Positive Radius Value\n"); scanf("%lf", &r); // If Else Function To Make Sure The User Types A Positive and Not A Negative Integer. if (r >= 0) sphere_volume (r); else printf("You Must Enter A Positive Integer!!!! Not A Negative Integer!!!\n"); return 0; } // This Is The Fnction That Does The Calculations For The Volume. void sphere_volume (double radius) { double radius_cubed; double exponent=3; double volume; printf("This Is The Test To See If R Was Passed To The Function: %lf\n", radius); radius_cubed=pow(radius, exponent); volume=(4/3)*PI*radius_cubed; printf("The Volume Of The Sphere is %lf\n", volume); } Joseph L. Gelsomino

      R Offline
      R Offline
      Roger Allen
      wrote on last edited by
      #2

      Try changing the 4/3 to 4.0/3.0 so the calulation is done as a double not as an integer, which is currently losing you the 0.3333333 If you vote me down, my score will only get lower

      1 Reply Last reply
      0
      • W WackoWolf

        The program works, but the calculation is wrong. The is written in C. Could someone tell me what I am doing wrong? Thank You #include "stdafx.h" #include #using using namespace System; // Function prorotypes void sphere_volume (double); // PI is a constant const double PI=3.14159; int _tmain() { // Integer local to Main to store initial radius value double r; printf("Please Enter A Positive Radius Value\n"); scanf("%lf", &r); // If Else Function To Make Sure The User Types A Positive and Not A Negative Integer. if (r >= 0) sphere_volume (r); else printf("You Must Enter A Positive Integer!!!! Not A Negative Integer!!!\n"); return 0; } // This Is The Fnction That Does The Calculations For The Volume. void sphere_volume (double radius) { double radius_cubed; double exponent=3; double volume; printf("This Is The Test To See If R Was Passed To The Function: %lf\n", radius); radius_cubed=pow(radius, exponent); volume=(4/3)*PI*radius_cubed; printf("The Volume Of The Sphere is %lf\n", volume); } Joseph L. Gelsomino

        D Offline
        D Offline
        David Crow
        wrote on last edited by
        #3

        In addition to Roger's suggestion, I suggest the following to help 'polish' your application:

        WackoWolf wrote:

        // Integer local to Main to store initial radius value double r;

        Comment (int) does not match code (double).

        WackoWolf wrote:

        if (r >= 0)

        Use a double constant (0.0) here.

        WackoWolf wrote:

        printf("You Must Enter A Positive Integer!!!! Not A Negative Integer!!!\n");

        Again referencing integers.

        WackoWolf wrote:

        double exponent=3;

        Use a double constant (3.0) here.

        WackoWolf wrote:

        void sphere_volume (double radius)

        Another suggestion I might make, although it has no impact on the outcome of your program, is to make this function's parameter const.

        void sphere_volume( const double radius );

        This ensures that your code does not accidently change the value of the parameter.


        "One must learn from the bite of the fire to leave it alone." - Native American Proverb

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups