Thread: Defines and Header file

  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,472
    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
    505
    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.
    https://github.com/MalcolmMcLean


  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
    4,513
    @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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > 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.
    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.

  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, 10: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, 02:21 PM
  5. Replies: 6
    Last Post: 04-02-2002, 05:46 AM