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

This is a discussion on Noob Help......Calculating volume and area of a sphere... within the C Programming forums, part of the General Programming Boards category; Okay, i made a couple changes, to try and pass the variables smoother, and it complies but I still get ...

1. 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. 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.

3. ^^^
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. 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.

5. 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. 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?

7. 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. 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. Almost there. Check what you are passing to volume_calc when you call it.

10. 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. 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. 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.

13. ^^^^
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. 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.

15. 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);
}```

Page 2 of 3 First 123 Last
Popular pages Recent additions