Like Tree1Likes
  • 1 Post By Salem

Defines and Header file

This is a discussion on Defines and Header file within the C Programming forums, part of the General Programming Boards category; 1. Write a program the calculates the volume of a sphere. Use a define to set Pi to 3.14 and ...

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    31

    Defines and Header file

    1. Write a program the calculates the volume of a sphere. Use a define to set Pi to 3.14 and a macro for the formula for the sphere. V = 4/3PiR3.
    In main ask for the radius (R). Pass it to a function where you calculate the volume and return it to main and print the volume in main. Use float values. (Save this program as you'll need it later.)

    Can you please help me?

    Code:
     
    Code:
    #include<stdio.h>
    void fun (float);
    main()
    	{
    		float  a;
    		float v;
    		printf("Please enter the radius: ");
    		scanf("%.3f",&a);
    		fun(a);
    		printf("The volume is  =  %.3f", v);
    }
    
    
    void fun (float Radius)
    {
    	float v,pi = 3.14;
    	v = ((4/3)*pi*(Radius * Radius * Radius));
    	printf("%.3f",v);
    	return(v);
    }

  2. #2
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,425
    Can you please help me?
    What sort of help do you need?
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    333
    You've got the basic idea.

    fun calcuates the volume of sphere, given a radius. But it shouldn't be called "fun". It should be called "spherevolume" or somethign similar.

    Then it's one of those functions which returns a single value, like tan or sqrt. So it should be float fun(float r).

    Finally, it depends on the mathematical constant PI. In C we define constants with the #define directive. For instance phi, the golden ratio, is a constant. So you could write
    #define PHI 1.618

    Another constant you use a lot is INT_MAX, the maximum value that can be held in an integer. This is defined for you in limits.h, or you can define a similar constant yourself.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    http://www.malcolmmclean.site11.com/www

  4. #4
    Registered User
    Join Date
    Mar 2013
    Posts
    31
    can you please help me in the program?

  5. #5
    Registered User
    Join Date
    Mar 2013
    Posts
    31
    Code:
    #include<stdio.h>
    #include<limits.h>
    #define pi 3.141592654
    void spherevolume (float a);
    main()
    {
    	float a,v;
    	float answer;
    	printf("Please enter the radius: ");
    	scanf("%f",&a);
    	spherevolume(a);
    	 printf("%f",v);
        getch();
    }
    void spherevolume (float b)
    {
    	float num,Radius,v;
    	num = 1.333333333;
    	Radius = b * b * b;
    	v = (num * pi * Radius);
    	return(v);
    	getch();
    	
    }
    Can you please help me how to return spherevolume to main? please.

  6. #6
    Time-Lord Victorious! The Doctor's Avatar
    Join Date
    Aug 2012
    Location
    Perth, Western Australia
    Posts
    50
    Look up Preprocessor commands. That's what you need to use.

    You want the volume thing to not be a function. you need to use #define for a macro as well.
    Code:
    if (codeWorks( code) && !youCanDoSomethingBetter( code) )
    {
         beHappy();
    } else
    {
         fixCode( code);
    }

  7. #7
    Registered User
    Join Date
    Jun 2011
    Posts
    2,292
    @The Doctor

    Code:
    if (codeWorks( code) && !youCanDoSomethingBetter( code) )
    {
         fix( code);
    } else
    {
         beHappy();
    }
    Fix your signature! You have the statements reversed!

    Code:
    if (codeWorks( code) && !youCanDoSomethingBetter( code) )
    {
         beHappy();
    } else
    {
         fix( code);
    }

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,500
    > Can you please help me how to return spherevolume to main? please.
    How about making it return something?

    > void spherevolume (float a);
    Like say
    float spherevolume (float a);

    Then make sure you assign the result in main rather than ignore it.
    stahta01 likes this.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Registered User
    Join Date
    Mar 2013
    Posts
    31
    Code:
    #include<stdio.h>
    #include<limits.h>
    #define pi 3.141592654
    float spherevolume (float);
    main()
    {
    	float a,v;
    	printf("Please enter the radius: ");
    	scanf("%f",&a);
    	v = spherevolume(a);
    	 printf("\n\nThe volume is : %.3f",v);
        getch();
    }
    float spherevolume (float b)
    {
    	float num,Radius,v;
    	num = 1.333333333;
    	Radius = b * b * b;
    	v = (num * pi * Radius);
    	return(v);
    	getch();
    	
    }
    Thank you so much it 's working now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. namepaces and header file defines
    By manav in forum C++ Programming
    Replies: 6
    Last Post: 12-10-2007, 09:09 AM
  2. Replies: 5
    Last Post: 10-22-2006, 07:35 PM
  3. Replies: 30
    Last Post: 06-19-2006, 12:35 AM
  4. Replies: 4
    Last Post: 12-14-2005, 01:21 PM
  5. Replies: 6
    Last Post: 04-02-2002, 04:46 AM

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