# Math Equation Program (I can't find the problem with my program!)

This is a discussion on Math Equation Program (I can't find the problem with my program!) within the C Programming forums, part of the General Programming Boards category; k, i can't find the bug to this program Code: #include <stdio.h> int main(void) { /* Variable Declarations */ #define ...

1. ## Math Equation Program (I can't find the problem with my program!)

k, i can't find the bug to this program

Code:
#include <stdio.h>
int main(void)

{

/* Variable Declarations */
#define Pi 3.14159265

int sph_r, cb_s, crcl_r, sqr_s;
int sph_sa, sph_v, cb_sa, cb_v, crcl_a, sqr_a;

/* Sphere */
printf("Enter the radius of a sphere: ");
scanf("%f", & sph_r);

sph_sa = 4 * Pi * sph_r * sph_r;
sph_v = 4/3 * Pi * sph_r * sph_r * sph_r;

printf("%f %f \n", sph_sa, sph_v);

/* Cube */
printf("Enter the length of a side of a cube: ");
scanf("%f", &cb_s);

cb_sa = 6 * cb_s * cb_s;
cb_v = cb_s * cb_s * cb_s;

printf("%f %f \n", cb_sa, cb_v);

/* Circle */
printf("Enter the radius of a circle: ");
scanf("%f", &crcl_r);

crcl_a = Pi * crcl_r * crcl_r;

printf("%f %f \n", crcl_a);

/* Square */
printf("Enter the length of a side of a square: ");
scanf("%f", &sqr_s);

sqr_a = sqr_s^2;

printf("%f %f \n ", sqr_a);
}

2. PI is already defined in <math.h>. This code
Code:
sqr_a = sqr_s^2;
XORs sqr_s with two, which is probably not what you want. Try this:
Code:
sqr_a = pow(sqr_s, 2.0);
All of your variables should be floats or doubles (probably floats, since that's how you're reading them in with scanf).

3. Code:
int main() {
At least you used int main, but put a return 0; at the end.
Code:
printf("%f %f \n ", sqr_a);
printf() expects two floats, but you give it one int.

4. Don't you need a prototype thingy in front of pow(sqr_s, 2.0); ?

5. Huh? No, it returns a double. You do need <math.h> though.

6. so replace <stdio.h> with <math.h> ?

7. No, add another line that includes math.h.

8. its sumwut revised, but there are still probs

Code:
#include <stdio.h>
#include <math.h>
int main(void)

{

/* Variable Declarations */
#define Pi 3.14159265

int sph_r, cb_s, crcl_r, sqr_s;
int sph_sa, sph_v, cb_sa, cb_v, crcl_a, sqr_a;

/* Sphere */
printf("Enter the radius of a sphere: ");
scanf("%f", & sph_r);

sph_sa = 4 * Pi * sph_r * sph_r;
sph_v = 4/3 * Pi * sph_r * sph_r * sph_r;

printf("%f %f \n", sph_sa, sph_v);

/* Cube */
printf("Enter the length of a side of a cube: ");
scanf("%f", &cb_s);

cb_sa = 6 * cb_s * cb_s;
cb_v = cb_s * cb_s * cb_s;

printf("%f %f \n", cb_sa, cb_v);

/* Circle */
printf("Enter the radius of a circle: ");
scanf("%f", &crcl_r);

crcl_a = Pi * crcl_r * crcl_r;

printf("%f %f \n", crcl_a);

/* Square */
printf("Enter the length of a side of a square: ");
scanf("%f", &sqr_s);

sqr_a = sqr_s * sqr_s;

printf("%f %f \n ", sqr_a);

return 0;

}

9. Just saying there are still probs. isn't very helpfull. My crystal ball is broken!

You should include what problems you are having.

10. Code:
#include <stdio.h>
#include <math.h>
#include <stdlib.h>

int main(void)

{

/* Variable Declarations */
#define Pi 3.14159265
char temp[80] ;

double sph_r, cb_s, crcl_r, sqr_s;
double sph_sa, sph_v, cb_sa, cb_v, crcl_a, sqr_a;

/* Sphere */
printf("Enter the radius of a sphere: ");
fgets(temp, 80, stdin);
sph_r = strtod(temp, NULL); // convert a sting to a double.

sph_sa = 4 * Pi * sph_r * sph_r;
sph_v = 4/3 * Pi * sph_r * sph_r * sph_r;

printf("The surface area is: %f, and the volumn is: %f \n", sph_sa, sph_v);

printf("\nPress 'Enter' to continue.");
getchar();

return 0;

}

11. Originally Posted by Bajanine
Just saying there are still probs. isn't very helpfull. My crystal ball is broken!

You should include what problems you are having.
Then you totally disregard your own statements here by reworking their code for them. Yet you wonder why it is people post so vaguely. Good job.

Quzah.

12. Thank you.

If you'll notice at least I waited a half hour or so!

I know that when I had a post count of 4 I did a lot of stupid ........ too. Hell, I still do. I still think the point was made: Ask smart questions.

13. Bajanine, PI is defined in <math.h>, so if you #define PI yourself, you probably need
Code:
#ifdef PI
#undef PI
#endif
(The PI in <math.h> is probably more accurate than your PI, so I'd just use the <math.h> PI.)

14. Actually you never need to #undef if you're just redefining something on your own. A simple redefinition is all you need. The only time you need #undef is if you don't want that define to exist at all any more.

Quzah.

15. Oh, I didn't know that. Thanks for telling me.