Thread: help with writing function that multiplys the radius

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    3

    help with writing function that multiplys the radius

    Just like to say hi to everybody and that umm this is basically my first semester of programming its been pretty fun and its always cool learning new things.

    Teach wanted us to write a program that did a couple of things.

    1. prompt the user for the radius in main *did that*
    2. use this formula to find the diameter 2r (2 * radius)
    3. use this formula to find the circumference 2pi r(2*pi*radius)
    4. use this formula to find the circle pi rē (pi * r * r)
    5. use a seperate function for each calculation and print the answers from main.

    What I need help is figuring out how do I go about coding this program. Im stuck at part 2 because I dont know what to do next. here is my code.

    Code:
    /* The purpose of this program is to read in the radius and print out
    the circle's diameter, circumference, and area */
    
    #include <stdio.h>
    #include <conio.h>
    #define PI 3.14159
    
    
    void main()
    
    /* This function asks the user to type in a number for the radius of a circle */
    
    {
    	
    
    	printf("Please enter the radius of a circle: ");
    	scanf ("%d", &radius);
    
    	getch();
    }
    
    /* This function lists the diameter of the number inputed as the radius of the circle */
    
    void function1(float diameter, float radius)
    so for the 2nd function do I start off by writing the formula in ?

  2. #2
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    You'll need three functions like this :
    Code:
    float function(float radius)
    {
        return whatever;
    }
    You have the formulae, so just plonk in it the function as the return value.

    Here's an example:
    Code:
    float circumference(float radius)
    {
        return (2*PI*radius);
    }
    You will also need to declare the functions before main().
    Code:
    #include <stdio.h>
    #include <conio.h>
    #define PI 3.14159
    
    float diameter(float radius);
    float circumference(float radius);
    float area(float radius);
    
    int main()
    {
       ..
       ..
       ..
       return 0;  
    }
    To print the values in main(), just print the return value of the function, eg.
    Code:
    printf("circumference = %f", circumference(radius));
    Oh, and don't use void main(), use int main() and then return 0 at the end.

  3. #3
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    Hey Dog, I felt like saying this:
    http://www.yourethemannowdog.com/
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    3
    thanks for your help. I was doing just the opposite, instead of declaring the functions before main, I was writing them in after main.

    I just have one problem, I keep getting the same 3 syntax errors. What does this mean

    error C2297: '*' : illegal, right operand has type 'int (__cdecl *)(int)'

    it errors on this part of the code

    Code:
    return(PI * radius * radius);

  5. #5
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    Code:
    //Try this
    ..
    return ((radius * radius) * PI);
    If the error persists, then check your function's parameter list, it should be "float function(float radius)"

  6. #6
    Registered User
    Join Date
    Oct 2002
    Posts
    3
    i declared my radius with the other declarations as an int is this ok?

    and if so would it be int radius (float radius) because where im confuse is where I declared the parameters why must there be a float radius next to it, whats the purpose of the float?

    The program acepts a int from the user, so I called that int radius. The int/radius is what is needed to do the math for the program. This is what I have so far, and it keeps giving me that error.

    Code:
    /* The purpose of this program is to read in the radius and print out
    the circle's diameter, circumference, and area */
    
    #include <stdio.h>
    #include <conio.h>
    #define PI 3.14159
    
    
    float diameter (float radius);
    float circum (float radius);
    float area (float radius);
    int radius (int radius);
    
    
    int main()
    
    /* This function asks the user to type in a number for the radius of a circle, and returns the values
    of what that number would be in diameter, circumference and area form. */
    
    {
    	printf("Please enter the radius of a circle: ");
    	scanf ("%d");
    	printf("The Diameter is %f", diameter);
    	printf("The circumference is %f", circum);
    	printf("The area is %f", area);
    	return 0;
    
    	getch();
    }
    
    /* This function lists the diameter of the number inputed as the radius of the circle */
    
    
    
    float diameter()
    
    {
    	return(2 * radius);
    }
    
    /* This function lists the circumference of the number inputed as the radius of the circle */
    
    float circum()
    
    {
    	return (2 * PI * radius);
    
    }
    
    /* This function lists the area of the number inputed as the radius of the circle */
    
    float area()
    
    {
    	return(PI * radius * radius);
    
    }

  7. #7
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    >> and if so would it be int radius (float radius) because where im confuse is where I declared the parameters why must there be a float radius next to it, whats the purpose of the float?

    You didn't define a radius() function in your code. I'm guessing all it does is grab the radius from the user. But since you did that in main already, I'm assuming you don't need that function.
    You need to declare float radius in your main.

    >>scanf ("%d");
    change it to scanf ("%f", &radius);

    >> printf("The Diameter is %f", diameter);
    You didn't call the function or pass in any parameters here.
    Change to
    printf("The Diameter is %f", diameter(radius));

    >> float diameter(){
    You need to pass in your radius.
    float diameter(float radius){

    Make appropriate changes for the rest of your functions. You may want to specify the precision in your output but that's another topic that you can search for on this board.
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Troubleshooting Input Function
    By SiliconHobo in forum C Programming
    Replies: 14
    Last Post: 12-05-2007, 07:18 AM
  3. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Replies: 5
    Last Post: 02-08-2003, 07:42 PM