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

Show 80 post(s) from this thread on one page
Page 1 of 3 123 Last
• 03-19-2010
matt.s
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```
• 03-19-2010
laserlight
Notice that you called the printresults function without providing any arguments, but defined the function as having three parameters.
• 03-19-2010
matt.s
^^
hey yeah im noticing that now, i'm unsure of how to properly pass them though... :S

• 03-19-2010
rogster001
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..
• 03-19-2010
Epy
Quote:

Originally Posted by matt.s
^^
hey yeah im noticing that now, i'm unsure of how to properly pass them though... :S

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);`
• 03-19-2010
matt.s
^^
thank you, making a little sense now, i'll give it a go and post back! Thanks!
• 03-19-2010
Epy
Quote:

Originally Posted by rogster001
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);`
• 03-19-2010
Feengur
gah... late again...
• 03-19-2010
matt.s
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); }```
• 03-19-2010
rogster001
Quote:

You don't have to include the variable names in the function prototype, just the types:

Code:
void printresults(float, float, float);
• 03-19-2010
laserlight
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.
• 03-19-2010
matt.s
^^
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); }```
• 03-19-2010
laserlight
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.
• 03-19-2010
matt.s
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); }```
• 03-19-2010
laserlight
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.
Show 80 post(s) from this thread on one page
Page 1 of 3 123 Last