Jump to content
Science Forums

Recommended Posts

Posted

Ok, so for this assignment I needed to write a program to find the volume of a sphere using this forumula v = 4/3 π r 3 . I was able to do that but now I ran into another problem. I need to call the function and when it is called it should look something like this:

sphere_volume( r );

 

So here is my code I unnecessarily left a function blank:

#include <stdafx.h>

 

#define PI 3.14159 //define PI as 3.14159

 

int main(void)

{

 

return 0;

}

 

 

int second(void)

{

double volume,radius;

 

printf("Enter A Radius: "); //enter a radius

scanf("%lf", &radius);

 

volume = 4.0/3.0*PI*radius*radius*radius; // find the volume using the formula

printf("The volume of the sphere is %lfn",volume); //state the answer

 

return 0;

}

Posted

Bunch'a comments:

  • Don't put comments on #define lines. Depending on which complier you're using #define will substitute EVERYTHING that comes after the name wherever you use it. The way you have it used here, it's pretty much guaranteed not to work (turn on preprocessor output from your compiler to see why!).
  • If you do use #define for numerical substitutions, ALWAYS put parens around the substitution value or you will get mysterious precedence errors.
  • Best, DON'T USE #define! "const double pi = 3.14159; is much preferred because the compiler can figure out what to do with it (and if you ever want to port your code to a more strongly typed language compiler, you won't have to rewrite!). #define is a textual substitution, and probably one of the silliest things that Dennis Ritchie ever did....
  • The program as is won't do anything because it will call main and then exit, it'll never get to your "second()" function. you need to put a call to "second()" in your main...
  • You can either move most of the stuff in second to main and rename second as sphere_volume( r ), or move some stuff from second() into sphere_volume( r ). I'd recommend the former, but that's just me.
  • Without some parens inside your volume computation, you're likely to get the wrong answer with some compilers (it's likely to do (3.0*PI) before it does (4.0/3.0)...)

The rest is left to you as an exercise....

 

When I read commentary about suggestions for where C should go, I often think back and give thanks that it wasn't developed under the advice of a worldwide crowd, :phones:

Buffy

Posted
Bunch'a comments:

 

[*]The program as is won't do anything because it will call main and then exit, it'll never get to your "second()" function. you need to put a call to "second()" in your main...

 

yea that is the problem, I don't know how to call functions.

and when it is called from the main program it has to look like this: sphere_volume( r );

Posted

Ah, it's difficult because it's too simple. Unlike many languages, C only has functions, there is nothing like a "procedure" as you'd find in Basic or Pascal.

 

Thus, you just use it:


double my_double_returning_function(double d)
{
    return (d * d);
}

main()
{
    double retval;
    // a useless but legal function call   
    my_double_returning_function(2.2);
    // a bit more useful because you can use the return value
    retval = my_double_returning_function(2.2);
}

 

Make sense?

 

Take me out and show me off, put me on the scene, :phones:

Buffy

Posted

Like this? Sorry, I don't understand this very well.

#include <stdafx.h>

#define PI (3.14159)  

int main(void)
{
double sphere_volume(r);

sphere_volume(r);
return 0;
}


int sphere_volume(void)
{
double volume,radius;

printf("Enter A Radius: "); //enter a radius 
scanf("%lf", &radius); 

volume = (4.0/3.0)*PI*radius*radius*radius;  // find the volume using the formula
printf("The volume of the sphere is %lfn",volume); //state the answer

return 0;
}

It keeps giving me an error saying r is undeclared.

Posted

The compiler is right: you haven't declared r anywhere. You'll need to do that.

 

You do have a redundant declaration of your function inside of main: what you have there is perfectly legal (with a small modification to define the type of the argument), and will work if you leave it there. but it's unnecessary since your program is all in one file and has the same scope.

 

What you're really missing though is the point of what they're trying to teach you, which is how to break your programs up into functions that each do a bit of the problem. I'm not going to do your work for you here, but here's the BIG HINT: part of your program should do the input and output and another part should do the computation. If you figure this out then the function definition you've been given will start to make sense...

 

Figure that part out and then I might help you with debugging the rest of it....

 

A time to cast away stones, a time to gather stones together, :phones:

Buffy

Posted

How's this:

// Assignment Problem 3.cpp : Defines the entry point for the console application.
//

#include <stdafx.h>
#include <stdio.h>
#include <math.h>

#define PI (3.14159)  

double volume, r;
double sphere_volume(r);
int radius;


int main(void)
{


r = radius;

printf("Enter A Radius: "); //enter a radius 
scanf("%lf", &r); 

printf("The volume of the sphere is %lfn", volume); //state the answer

return (0);


}

{
double sphere_volume(r);
int volume, r;



volume = ((4.0/3.0)*PI*r*r*r);  ///find the volume
return (volume);
}

 

I'm don't think its right because I keep getting a '{' missing function header

I did try separating the input and stuff.

Posted

syntax is wrong, syntax and general program layout in pseudo code: (usually main is the last function in a program, and not just C/C++, it really doesnt matter, but if you don't declare functions before main, they will not be useable by main... so you would have to have space holders before hand or just throw your function code there, but it's just cleaner that way, you can just take that piece out and that file can become a library or whatever...)

function code before main:

#includes

double sphere_volume(radius)
{
   variable definitions;
   function code;
   return;
}

int main()
{
   main variable definitions here;
   input here;
   volume = sphere_volume(radius);
   output volume;
   return;
}

function code after main:

#includes
double sphere_volume(radius);

int main()
{ 
   main code;
}

double sphere_volume(radius)
{
   sphere_volume code here;
{

Posted

// Assignment Problem 3.cpp : Defines the entry point for the console application.
//

#include <stdafx.h>
#include <stdio.h>
#include <math.h>

#define PI (3.14159)  

double r;


double sphere_volume(r)
{


int volume;
double r;



volume = (4.0/3.0)*PI*r*r*r;  // find the volume

return(0);
}

int main()

{
int volume;
double r;


printf("Enter A Radius: "); //enter a radius 
scanf("%lf", &r); 
r = volume;
volume =  sphere_volume(r);
printf("The volume of the sphere is %lfn", volume); //state the answer


return (0);


}



 

It has a problem with 'r'

Posted

yeah don't define it globally :phones: and don't redefine it in your function

 

should look more like

double sphere_volume(double r)
{
   return (4.0/3.0)*PI*r*r*r;
}

 

"double" signifies return type, "sphere_volume" is the name of the function "(double r)" the variable declaration and by that also definition of the kind of variable that should be passed to it

Posted

I got it working:

// Assignment Problem 3.cpp : Defines the entry point for the console application.
//

#include <stdafx.h>
#include <stdio.h>
#include <math.h>

#define PI (3.14159)  



double sphere_volume(double r)
{


double volume;


volume = (4.0/3.0)*PI*r*r*r;  // find the volume

return(volume);
}

int main()

{
double volume;
double r;


printf("Enter A Radius: "); //enter a radius 
scanf("%lf", &r); 

volume = sphere_volume(r);
printf("The volume of the sphere is %lfn", volume); //state the answer


return (0);


}



 

Thanks again.

Posted

Much better!

 

But you're having some problems with a couple of issues:

 

Declaring vs. Defining functions: When you say:

 

int foo(int i);

 

You're *declaring* a function, which is basically just giving the compiler a hint about the return values and arguments for your function. In C this is pointless unless you are linking multiple modules together and one module is calling foo() that's *defined* in another module.

 

To *define* a function you say:

 

int foo(int i) { return i; }

 

which differs from a declaration in that the semi-colon has been replaced by a statement block which *defines* what the function is going to do.

 

You definitely need to define all the functions you call somewhere, but they can be in another module (file) or in a library.

 

Structure of a source file Ignoring the fact that comments can occur anywhere, at the top level you can only have 3 things in a source file:

  • Preprocessor directives: these are any line that begins with a # and basically get taken away by the time your code actually hits the *compiler*. As the name implies, the preprocessor translates them first. These are usually either references to header files (that *declare* functions (and sometime variables)) for libraries that your program references, or #defines that you've already learned about.
  • Declarations of variables or functions (see above)
  • Definitions of functions

 

Now you'll notice in the lower part of your code you have a block of code bracketed by {'s. This is not a function definition, and so the compiler is complaining that "'{' missing function header". You'll need that to fix your bug.

 

You're getting closer though!

 

Out of intense complexities intense simplicities emerge , :phones:

Buffy

Posted

And then there is DCE, which should not be ignored.

 

DCE = Disciplined Code Esthetics, and refers to the fact that formatting and white space in your source code carry information.

 

Not to the compiler, of course. They carry meta-information to YOU and any other human who reads the source code. This meta-information gives you subliminal clues as to the structure and architecture of the source code, without you having to figure it out explicitly every time you return to the code. This will aid you massively in debugging your code and saving lots of time. DCE includes structured comments, indenting, variable naming conventions, and intentional white space.

 

Applying some DCE to your code might make it look something like:

 

[font="Courier New"]//----------------------------------
// Calculate the volume of a sphere with a given radius.

double sphere_volume(double r)
{
   double volume;
   volume = (4.0/3.0)*PI*r*r*r;     // volume of sphere
   return(volume);
}

//===================================

int main()
{
   double volume;
   double r;

   // Enter radius
   printf("Enter A Radius: "); 
   scanf("%lf", &r);
   // Get volume 
   volume = sphere_volume(r);
   // Print output
   printf("The volume of the sphere is %lfn", volume);

   return (0);[/font]
}

Posted
And then there is DCE, which should not be ignored.

 

There's two sides to everything though. On the one side, utilizing DCE in a teamwork environment insures that someone else can pick up where you left off. As a sole coder the lack of DCE helps to insure job security since no one else can figure out what the hell is going on in your code :phones:

Posted
...As a sole coder the lack of DCE helps to insure job security since no one else can figure out what the hell is going on in your code :phones:
Not any more.

Coders found "encrypting" their source code at Singer-Link as far back as 1983 were fired.

The illegible code they had produced was slowly and expensively replaced.

Most all companies now realize that software is expensive, especially its maintenance after it is "delivered". Life goes on. Bugs are found. Interfaces change. Enhancements are needed. And more often than not, the original author is not around or not available. So the original code MUST be written as a "corporate asset", with maintenance in mind from the beginning.

Companies cannot afford programmers who encrypt their software in any way.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...