Thread: Noob Help......Calculating volume and area of a sphere...

  1. #31
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Sorry, but I think that you really have to pause and read a tutorial on pointers. You need to understand how to work with pointers before you can proceed. At the moment, it looks like you have been making changes to your code without the faintest clue of why I have been suggesting what I have been suggesting.

    EDIT:
    Oh, I did not notice that in your post #23, you changed your correct line of code to:
    Code:
    double *area, *radius, *volume;
    which is not correct. If area, radius and volume are pointers, what are they supposed to point to? Again, this means that you need to understand the concept of a pointer and how to use pointers effectively.
    Last edited by laserlight; 03-19-2010 at 01:54 PM.
    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

  2. #32
    Registered User
    Join Date
    Sep 2009
    Posts
    54
    ^^^
    AHH! okay makes perfect sense, I corrected that and now the program compiles!!

    im not getting the desired output though lol

    if i enter a radius of 25, i should get a volume of 65449.847 and an area of 7853.982, instead i'm getting a volume of 9.425 and an area of 25.133 :S

    is it because im not returning anything from the calc?

    Code:
    //INCLUDE SECTION
    #include <stdio.h> //def's 4 printf, scanf, getchar, puts, gets, fflush
    #include <conio.h> //def's for getch
    #include <stdlib.h> //def's 4 system
    #define PI 3.14159
    
    
    //FUNCTION PROTOTYPES
    
    void explanation(void);
    void volume_calc(double *area, double *radius,double *volume);
    void printresults(double area, double radius, double volume);
    
    int main (void)
    {
    	double area, radius, volume;
    	explanation();//FUNCTION CALL
    	volume_calc(&area, &radius, &volume);//FUNCTION CALL
    	printresults(area, radius, volume);
    
    	printf("\n\t\t\tPRESS ANY KEY TO EXIT");
    	_getch();
    	return 0;
    }
    
    //FUNCTION DEFINITIONS
    void explanation(void)
    {
    	system ("cls");
    	puts ("This program will ask you to enter the RADIUS of a sphere...");
    	puts ("and then calculate the volume and surface area...");
    
    }
    void volume_calc( double *area, double *radius, double *volume)
    {
    	//VARIABLE DECLARATIONS
    	
    	//GET USER INPUT
    	printf ("\n\n\nPlease entert the radius of the sphere==> ");
    	scanf ("%f", &*radius);
    	
    
    	//CALCULATIONS
    	*volume=((4/3)*PI*(radius,3));
    	*area=(4*PI*(radius,2));
    	
    	
    	//OUTPUT
    }	
    void printresults(double area,double radius,double volume)
    
    {
    	printf("You entered a radius of %.3f...", radius);
    	printf("\nthe volume of this sphere is ==> %.3f", volume);
    	printf("\n\ntthe area of this sphere is ==> %.3f", area);
    }

  3. #33
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    It's mainly because you have 4/3 when computing your volume. 4/3 is assumed by the compiler to be an integer division and the result is 1. What you probably want is a floating point result so just do 4.0/3.0 instead of 4/3.

  4. #34
    Registered User
    Join Date
    Sep 2009
    Posts
    54
    ^^^
    yeah just noticed that, gave it a quick fix the numbers are a bit better, but still way off.....

    for some reason the radius is coming up as -92556900000000000000.000 when i display it at the end....can't seem to figure this one out.. hmmmmm

  5. #35
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by matt.s View Post
    ^^^
    yeah just noticed that, gave it a quick fix the numbers are a bit better, but still way off.....

    for some reason the radius is coming up as -92556900000000000000.000 when i display it at the end....can't seem to figure this one out.. hmmmmm
    What exactly is (radius,2) and (radius,3)?

  6. #36
    Registered User
    Join Date
    Sep 2009
    Posts
    54
    (radius,2) = radius squared
    (radius,3) = radius cubed

    or at least it does according to my teacher, who could very well be wrong lol

  7. #37
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    ummm... NO!

    Just use radius*radius for radius^2 and radius*radius*radius. it's not like you are raising to the 100th power.

    If you wanted to do that you would write x = pow(radius,100); pow is a function defined in math.h

  8. #38
    Registered User
    Join Date
    Sep 2009
    Posts
    54
    ^^^^^
    ahhh i see what your saying their, my teacher has clearly lost the plot i think lol :P

    ahhhhhh its still giving me the wrong output! how can such a simple little program be so confusing!?! :S

    Code:
    //INCLUDE SECTION
    #include <stdio.h> //def's 4 printf, scanf, getchar, puts, gets, fflush
    #include <conio.h> //def's for getch
    #include <stdlib.h> //def's 4 system
    #define PI 3.14159
    
    
    //FUNCTION PROTOTYPES
    
    void explanation(void);
    void volume_calc(double *area, double *radius,double *volume);
    void printresults(double area, double radius, double volume);
    
    int main (void)
    {
    	double area, radius, volume;
    	explanation();//FUNCTION CALL
    	volume_calc(&area, &radius, &volume);//FUNCTION CALL
    	printresults(area, radius, volume);
    
    	printf("\n\t\t\tPRESS ANY KEY TO EXIT");
    	_getch();
    	return 0;
    }
    
    //FUNCTION DEFINITIONS
    void explanation(void)
    {
    	system ("cls");
    	puts ("This program will ask you to enter the RADIUS of a sphere...");
    	puts ("and then calculate the volume and surface area...");
    
    }
    void volume_calc( double *area, double *radius, double *volume)
    {
    	//VARIABLE DECLARATIONS
    	
    	//GET USER INPUT
    	printf ("\n\n\nPlease entert the radius of the sphere==> ");
    	scanf ("%f", &*radius);
    	
    
    	//CALCULATIONS
    	*volume=((4.0/3.0)*PI*(*radius*(*radius)*(*radius)));
    	*area=(4.0*PI*(*radius*(*radius)));
    	
    	
    	
    	//OUTPUT
    }	
    void printresults(double area,double radius,double volume)
    
    {
    	printf("You entered a radius of %9.3f...", radius);
    	printf("\nthe volume of this sphere is ==> %.3f", volume);
    	printf("\n\ntthe area of this sphere is ==> %.3f", area);
    }

  9. #39
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    What's the input and output now?

  10. #40
    Registered User
    Join Date
    Sep 2009
    Posts
    54
    Input: 25

    Output:
    You entered a radius of -925596047186160310000000000000000000000000.000...
    The Volume of the sphere is: -33164118186955090000000000000000000000000000.000.. ...
    The Area of the sphere is: 10765953004987781000000000000000000000000000.000.. ..

  11. #41
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Try printing %lf

  12. #42
    Registered User
    Join Date
    Sep 2009
    Posts
    54
    ^^
    same output

    kinda whack no? :S

  13. #43
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    scanf ("%lf", radius); <--- correct

    You undo and do an indirect, which means you put neither. Also the %lf again for double.

  14. #44
    Registered User
    Join Date
    Sep 2009
    Posts
    54
    Ohhhh i was doing the %lf in the wrong place! and your right, its working perfect now! Thank you so much, greatly appreciated!!!!

  15. #45
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Nice catch! I didn't see that coming.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What is wrong with my code?
    By dakarn in forum C Programming
    Replies: 6
    Last Post: 10-14-2008, 05:16 AM
  2. iam a noob and i need help please
    By joker_tony in forum C Programming
    Replies: 8
    Last Post: 02-22-2008, 01:58 AM
  3. What am I doing wrong? Help please...
    By SprinterSteve in forum C Programming
    Replies: 9
    Last Post: 04-17-2003, 09:35 PM
  4. Need help with switch
    By 3kgt in forum C Programming
    Replies: 2
    Last Post: 02-26-2003, 12:43 PM