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. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    9

    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. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    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).
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    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.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Jul 2005
    Posts
    9
    Don't you need a prototype thingy in front of pow(sqr_s, 2.0); ?

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    Huh? No, it returns a double. You do need <math.h> though.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    Jul 2005
    Posts
    9
    so replace <stdio.h> with <math.h> ?

  7. #7
    Work in Progress..... Jaken Veina's Avatar
    Join Date
    Mar 2005
    Location
    Missouri. Go Imos Pizza!
    Posts
    256
    No, add another line that includes math.h.
    Code:
    void function(void)
     {
      function();
     }

  8. #8
    Registered User
    Join Date
    Jul 2005
    Posts
    9
    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. #9
    Registered User Bajanine's Avatar
    Join Date
    Dec 2001
    Location
    The most peaks over 10,000 feet!
    Posts
    396
    Just saying there are still probs. isn't very helpfull. My crystal ball is broken!

    You should include what problems you are having.
    Last edited by Bajanine; 07-04-2005 at 04:11 PM.
    Favorite Quote:

    >For that reason someone invented C++.
    BLASPHEMY! Begone from my C board, you foul lover of objects, before the gods of C cast you into the void as punishment for your weakness! There is no penance for saying such things in my presence. You are henceforth excommunicated. Never return to this house, filthy heretic!



  10. #10
    Registered User Bajanine's Avatar
    Join Date
    Dec 2001
    Location
    The most peaks over 10,000 feet!
    Posts
    396
    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;
    
    }
    Favorite Quote:

    >For that reason someone invented C++.
    BLASPHEMY! Begone from my C board, you foul lover of objects, before the gods of C cast you into the void as punishment for your weakness! There is no penance for saying such things in my presence. You are henceforth excommunicated. Never return to this house, filthy heretic!



  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote 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.
    Hope is the first step on the road to disappointment.

  12. #12
    Registered User Bajanine's Avatar
    Join Date
    Dec 2001
    Location
    The most peaks over 10,000 feet!
    Posts
    396
    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.
    Last edited by Bajanine; 07-04-2005 at 07:20 PM.
    Favorite Quote:

    >For that reason someone invented C++.
    BLASPHEMY! Begone from my C board, you foul lover of objects, before the gods of C cast you into the void as punishment for your weakness! There is no penance for saying such things in my presence. You are henceforth excommunicated. Never return to this house, filthy heretic!



  13. #13
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    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.)
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  15. #15
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    Oh, I didn't know that. Thanks for telling me.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [NEED HELP] I just can't solve this program problem!
    By frodonet in forum C Programming
    Replies: 7
    Last Post: 09-23-2007, 02:44 PM
  2. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  3. Running Program Problem
    By warfang in forum C++ Programming
    Replies: 10
    Last Post: 03-28-2007, 02:02 PM
  4. math problem
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 11-03-2003, 10:58 AM
  5. Console Program Problem
    By Breach23 in forum C++ Programming
    Replies: 3
    Last Post: 10-19-2001, 12:35 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21