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

  1. #16
    Registered User
    Join Date
    Sep 2009
    Posts
    54
    Okay, i made a couple changes, to try and pass the variables smoother, and it complies but I still get an error about it not being initilized correctly...is this starting to look a little better at least?

    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;
    	double a,r,v;
    	area=a;
    	radius=r;
    	volume=v;
    	explanation();//FUNCTION CALL
    	volume_calc(a,r,v);//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 a, double r,double v)
    {
    	//VARIABLE DECLARATIONS
    	double radius, area, volume;
    	//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);
    }

  2. #17
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What I mean is that this:
    Code:
    void volume_calc(double area, double radius,double volume);
    should be:
    Code:
    void volume_calc(double *area, double *radius, double *volume);
    Assigning to the local variables in volume_calc has no effect on the local variables in main. You need to assign to the local variables defined in main from within volume_calc, by having volume_calc operate on them via pointers.

    By the way, you should also compile with a high warning level. The compiler should have been complaining to you that you were trying to pass pointers where they were not expected.
    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. #18
    Registered User
    Join Date
    Sep 2009
    Posts
    54
    ^^^
    AHHHH! i see!

    what does this refer to?
    Code:
    error C2440: '=' : cannot convert from 'double' to 'double *'
    in regards to...
    Code:
    	volume=((4/3)*PI*(radius,3));
    	area=(4*PI*(radius,2));

  4. #19
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by matt.s
    what does this refer to?
    Now that the parameters are pointers, you need to dereference them, i.e., *radius, *volume and *area.
    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. #20
    Registered User
    Join Date
    Sep 2009
    Posts
    54
    clearly im missing something here because i still can't get it to run, perhaps i need to consider dropping this course lol

    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);
    }

  6. #21
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Your volume_calc function's prototype does not match its definition, and when you call it in the main function, you did not pass pointers.

    By the way, have you actually been taught how to use pointers?
    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

  7. #22
    Registered User
    Join Date
    Sep 2009
    Posts
    54
    unfortunatly my teacher is quite useless, and basically shows up to class late, throws on a video he found on youtube, and 15 mins later the class is over.

    This is the first assignment we've had, and have barely even touched on functions so far, so this is a bit of a stretch for me right now.

  8. #23
    Registered User
    Join Date
    Sep 2009
    Posts
    54
    any better?
    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);
    double 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...");
    
    }
    double 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));
    	return (area,volume);
    	
    	//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);
    }

  9. #24
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Almost there. Check what you are passing to volume_calc when you call it.
    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

  10. #25
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    As laserlight pointed out, you are doing good, just double check the way you using pointers. Remember a pointer is AN ADDRESS (i.e. &(<type>) of a variable). (*ptr) is the value contained in the address pointed to by ptr.

    Example:

    Code:
    int i_am_an_integer_value;
    i_am_an_integer_value = 3;
    int *i_am_a_pointer_to_an_int;
    /* pointer (which is an address) is assigned address of int */
    i_am_a_pointer_to_an_int = &i_am_an_integer_value;
    /* value contained at address pointed to by the pointer is an int */
    /* so *i_am_a_pointer_to_an_int is of type int and has the value 3 */

  11. #26
    Registered User
    Join Date
    Sep 2009
    Posts
    54
    am i just going around in circles now?

    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);
    double 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...");
    
    }
    double 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));
    	return (area,volume);
    	
    	//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);
    }
    i keep getting

    Code:
    Run-Time Check Failure #3 - The variable 'volume' is being used without being initialized.

  12. #27
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by matt.s
    am i just going around in circles now?
    Yes. Using your code in post #23, replace this:
    Code:
    volume_calc(area,radius, volume);
    with:
    Code:
    volume_calc(&area, &radius, &volume);
    Now, search the Web for a tutorial on pointers in C to better understand what you have just been guided to do.
    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

  13. #28
    Registered User
    Join Date
    Sep 2009
    Posts
    54
    ^^^^
    THANK YOU! ahh i see now, i wasnt pointing the right way, so the compiler didnt know!

    i'm still coming up with these errors...
    Code:
    1>c:\users\matthew\desktop\assignment.cpp(27) : error C2664: 'volume_calc' : cannot convert parameter 1 from 'double **' to 'double *'
    
    1>        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
    
    1>c:\users\matthew\desktop\assignment.cpp(55) : error C2440: 'return' : cannot convert from 'double *' to 'double'

  14. #29
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You can make the return type of volume_calc be void. By the way, you are compiling as C++ instead of C; try using say, .c as the file extension.
    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

  15. #30
    Registered User
    Join Date
    Sep 2009
    Posts
    54
    Ahhh okay..
    I renamed it as a ".c" and its running a little smoother i think, but im still coming up with errors :S

    Code:
    1>c:\users\matthew\desktop\assignment.c(28) : warning C4047: 'function' : 'double *' differs in levels of indirection from 'double **'
    1>c:\users\matthew\desktop\assignment.c(28) : warning C4024: 'volume_calc' : different types for formal and actual parameter 1
    1>c:\users\matthew\desktop\assignment.c(28) : warning C4047: 'function' : 'double *' differs in levels of indirection from 'double **'
    1>c:\users\matthew\desktop\assignment.c(28) : warning C4024: 'volume_calc' : different types for formal and actual parameter 2
    1>c:\users\matthew\desktop\assignment.c(28) : warning C4047: 'function' : 'double *' differs in levels of indirection from 'double **'
    1>c:\users\matthew\desktop\assignment.c(28) : warning C4024: 'volume_calc' : different types for formal and actual parameter 3
    1>c:\users\matthew\desktop\assignment.c(56) : error C2059: syntax error : ')'
    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));
    	return ();
    	
    	//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);
    }

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