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

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    54

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

    Hey guys,

    I'm new to this language and am having a little problem getting the code below to run, its for a program this asks a user to enter a radius, and then computes the area and volume of a sphere...

    any help would be greatly appreciated. Thanks

    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(void);
    void printresults();
    
    int main (void)
    {
    	explanation();//FUNCTION CALL
    	volume_calc();//FUNCTION CALL
    	printresults();
    
    	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(void)
    {
    	//VARIABLE DECLARATIONS
    	double radius, area, volume;
    	//GET USER INPUT
    	printf ("\n\n\nPlease entert the radius of the sphere==> ");
    	fflush (stdin);
    	scanf ("%f", &radius);
    	;
    
    	//CALCULATIONS
    	volume=(4/3)*PI*(radius,3);
    	area=4*PI*(radius,2);
    	
    	//OUTPUT
    	printresults();
    }
    void printresults(float area, float radius, float volume)
    {
    	printf("You entered a radius of %.3f...", radius);
    	printf("\nthe volume of this sphere is ==> %.3f", volume);
    	printf("\n\nthe area of this sphere is ==> %.3f", area);
    }
    These are the errors that are coming up....

    Code:
    1>LAB7A.obj : error LNK2019: unresolved external symbol "void __cdecl printresults(void)" (?printresults@@YAXXZ) referenced in function _main
    
    1> : fatal error LNK1120: 1 unresolved externals

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Notice that you called the printresults function without providing any arguments, but defined the function as having three parameters.
    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
    Join Date
    Sep 2009
    Posts
    54
    ^^
    hey yeah im noticing that now, i'm unsure of how to properly pass them though... :S

    Thanks for the quick reply!

  4. #4
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    it just means you need to put (float area, float radius, float volume) in your protoype at the top, and when you call the function in your program it needs to have this amount of parameters too, but you only have to write in the names of the variables, not the type also, then in your actual function definition (where the work is done by the function) you need to show the arguments the same as you did in the prototyp, ie with the types also,

    ps the actual variable names can be different there to reflect their exact use in the function itself if you like..

  5. #5
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413
    Quote Originally Posted by matt.s View Post
    ^^
    hey yeah im noticing that now, i'm unsure of how to properly pass them though... :S

    Thanks for the quick reply!
    Note that in volume_calc you are defining area, radius and volume as doubles, but your printresults function takes floats.

    Anyway, to call printresults:
    Code:
    printresults(area, radius, volume);

  6. #6
    Registered User
    Join Date
    Sep 2009
    Posts
    54
    ^^
    thank you, making a little sense now, i'll give it a go and post back! Thanks!

  7. #7
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413
    Quote Originally Posted by rogster001 View Post
    it just means you need to put (float area, float radius, float volume) in your protoype at the top, and when you call the function in your program it needs to have this amount of parameters too, but you only have to write in the names of the variables, not the type also, then in your actual function definition (where the work is done by the function) you need to show the arguments the same as you did in the prototyp, ie with the types also,

    ps the actual variable names can be different there to reflect their exact use in the function itself if you like..
    You don't have to include the variable names in the function prototype, just the types:
    Code:
    void printresults(float, float, float);

  8. #8
    Registered User
    Join Date
    Jan 2008
    Posts
    28
    gah... late again...
    Last edited by Feengur; 03-19-2010 at 10:42 AM. Reason: too late!

  9. #9
    Registered User
    Join Date
    Sep 2009
    Posts
    54
    Okay so I've made the changes, and its compiling now
    but it crashes when it gets to the 'printresults' function in main, saying that..

    Code:
    Run-Time Check Failure #3 - The variable 'radius' is being used without being initialized.
    Heres the changes i've made so far......

    Code:
    /*
    Title: Program to calculate the volume of two rooms
    Author: Matthew Sokolowski 025 586 100 PRG 155
    Date: February 21, 2010
    Description: This program will use the function colume_calc()
    			 to ask the user for l1,w1,h1 as well as l2, h2, w2
    			 and then calculate the total volume.  This information
    			 will then be passed to the screen by a function
    */
    
    //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(void);
    void printresults(double , double , double );
    
    int main (void)
    {
    	double area, radius, volume;
    	explanation();//FUNCTION CALL
    	volume_calc();//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(void)
    {
    	//VARIABLE DECLARATIONS
    	double radius, area, volume;
    	//GET USER INPUT
    	printf ("\n\n\nPlease entert the radius of the sphere==> ");
    	fflush (stdin);
    	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);
    }

  10. #10
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    You don't have to include the variable names in the function prototype, just the types:

    Code:
    void printresults(float, float, float);
    that is not good practice, bad advice

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Epy
    You don't have to include the variable names in the function prototype, just the types:
    That is true, but normally you should include the parameter names in the prototype, to aid in readability.

    Quote Originally Posted by matt.s
    but it crashes when it gets to the 'printresults' function in main, saying that..
    Yes. The problem is that the variables named area, radius, and volume in the main function are not related to the variables named radius, area, and volume in the volume_calc function.

    What you can do is add parameters to the volume_calc function. These parameters will be pointers to the area, radius, and volume variables in the caller, i.e., the main function would pass the addresses of area, radius, and volume to volume_calc.

    By the way, this results in undefined behaviour:
    Code:
    fflush(stdin);
    Refer to this FAQ.
    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

  12. #12
    Registered User
    Join Date
    Sep 2009
    Posts
    54
    ^^
    sorta like this? passing radius, volume and area to the calculate function?

    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(void);
    void printresults(double , double , double );
    
    int main (void)
    {
    	double area, radius, volume;
    	explanation();//FUNCTION CALL
    	(area,radius,volume)volume_calc();//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...");
    
    }
    (area,volume,radius)volume_calc(void)
    {
    	//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);
    }

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    No, that does not make sense. I would expect something like this as the function call:
    Code:
    volume_calc(&area, &radius, &volume);
    You would have to modify volume_calc accordingly to have pointer parameters.
    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

  14. #14
    Registered User
    Join Date
    Sep 2009
    Posts
    54
    sorta like this? sorry im so slow, i feel like such an idiot lol
    but i really appreciate the help, im slowly seeing where i should be going with this...

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

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Getting closer. volume_calc's parameters are still doubles rather than pointers to double. You should also remove those local variables from volume_calc, and since radius would then be a pointer to double, there would be no need to pass its address to scanf.
    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

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