Hi guys!
I have an assignment for my C++ programming course. The deadline is 2009/11/1 ! (I know it's too late, but plzzzz!!) I would really appreciate it if you could help me.
The problem is:
Write a program which calculates and prints the area and circumference of a circle with 2 functions.
THANKS A LOT !

Recommended Answers

All 16 Replies

You should use one function for calculating the area of a circle, and another function for calculating the circumference.

The equation for calculating the area of a circle is

A = (pi) * r^2

Where A is obviously the area and r is the radius.

Area of a circle

The equation for calculating the circumference of a circle is

C = (pi) * D

Where C is the circumference and D is the diameter.

Circumference of a circle

That should help you get started.

Before we 'should' help you, you should post some code before hand, so we can see that you put effort into your assignment.

Kontained gave you a good start btw.

Thank you restrictment and Kontained. But I really don't have the time.

Is that our fault? If you look under the stickies, you will see a topic labeled 'homework help' or something of that nature, which explains that the OP must show some type of effort in completing the project. Otherwise we shouldn't help you, as it could be classified as cheating.
Thread: http://www.daniweb.com/forums/announcement8-2.html

If you do not have anything to show, then it sounds to me that you just waited until the last minute, and put no dedication into your work.

Please post some type of code here, and I would be happy to help. I already have the program up and running, which I made in about 10 minutes..so it really is not that difficult.

Thanks a lot restrictment! I'm a newbie, so I didn't know the rules.
Here's the program I've written. Please tell me what's wrong with it.
Thanks in advance.

#include <iostream.h>
#include <conio.h>

void main()
{
	clrscr();
	float area (int r)
	float circ (int r)
	area=3.14 * r * r;
	circ=3.14 * 2 * r ;
	cout<<"Enter Radius: "
	cin>> r;
	int a=area(r)
	int c=circ(r)
	cout<<"\n Area= "<<a;
	cout<<"\n Circumference= "<<c;
	getch;
}

Hmm, ok. That looks like you put a bit of effort in, however it does not look like you really tried.

It's good enough, so please compare your code with mine, and examine the differences.

#include <iostream>
#include<iomanip>
using namespace std;
void circ(float circum);
void are(float area2);
float circum, circumference, radius, area2, area;
int main()
{
	cout << setiosflags(ios::fixed) << setprecision(2); //sets decimal places
	cout << "This program displays the area and circumfrence of a circle using two functions.\n\n";
	cout << "Please enter the radius of your circle: ";
	cin >> radius; 
	circ(circumference);
	are(area2);
	cout << endl;
	cout << "The circumference of your circle is " << circumference << ".\nThe area of your circle is " << area << endl << endl;
}
void circ(float circum)
{
	circumference = 3.14*(radius*2);
}
void are(float area2)
{
	area = 3.14 * (radius*radius);
}

*please do not completely copy the code and hand it in. First look at how it works, so you would know how to do it again. Otherwise you will be lost for a while.*

Thank you very much restricted.
But what is "end1"? And by the way, these codes seem a little complex to me ! (maybe our professor hasn't taught us the codes you have used).
Thanks again.

It's endl (the l is an L). This is just used to go to the next line. (same thing as \n)

As far as the complexity of the code is concerned, I believe all of it you should have learned before learning functions. Are you sure the assignment is to use functions? (Your example code does not contain any functions.)

Yes, the assignment is to use functions.
That's right ! I had some problems with declaring functions.
Anyways, when trying to run your program, it encounters 2 errors and 3 warnings.
Errors:
line #3: Delcaration syntax error
line #16: Function should return a value
Lets skip the warnings for now!
Thank you very much restricted.

Circumference and area are both out of scope in the main() function, and you need to pass in radius to the function. They should probably return floats

Hmm, that is odd. It works perfectly for me. I only get 2 warnings which say possible loss of data. (Because of the decimal in 3.14.) But it does not effect anything in visual studios express.

What application do you use to compile the c++?

I missed it, but yours compiles because you declared them all as global variables. It works, but is not good practice for the purposes of clean coding: you should declare circumference, radius, etc within main and do the appropriate passing and returning.

I missed it, but yours compiles because you declared them all as global variables. It works, but is not good practice for the purposes of clean coding: you should declare circumference, radius, etc within main and do the appropriate passing and returning.

Really? Could you show me what you mean by adding it into the code. I just learned functions yesterday using examples, and I must have used the wrong one for reference.

Thanks.

Don't do:

float circum, circumference, radius, area2, area;
int main()
{

Do:

int main()
{
float circum, circumference, radius, area2, area;

and for your function:
(we could go wild and use doubles, but I'll leave it as float)

float circ(float radius)
{
        return 3.14*2*radius;
}

-OR-

float circ(float radius)
{
        float circumference;
        circumference = 3.14*2*radius;
        return circumference;
}

So the function takes a float (since that's what you declared your radius as in the main function, but could just as easily have been an int) and returns a float.
So back in main you need circum = circ(radius);

Ah, the return value. I did not know about that yet. Thanks.

Edit:
Could you please tell me what is wrong with this code...

#include <iostream>
#include<iomanip>
using namespace std;
float circ(float circum);
float are(float area2);
int main()
{
	float circumference, radius, area;
	cout << setiosflags(ios::fixed) << setprecision(2); //sets decimal places
	cout << "This program displays the area and circumfrence of a circle using two functions.\n\n";
	cout << "Please enter the radius of your circle: ";
	cin >> radius; 
	circ(radius);
	are(radius);
	cout << endl;
	cout << "The circumference of your circle is " << circumference << ".\nThe area of your circle is " << area << endl << endl;
}
float circ(float radius)
{
	float circumference;
	circumference = 3.14*(radius*2);
	return circumference;
}
float are(float radius)
{
	float area;
	area = 3.14 * (radius*radius);
	return area;
}

You are calling your functions with the proper parameter, but the return value is disappearing

So back in main you need something = circ(radius); This function has an output, you need to hook it up to something.

Also note that the circumference variable in the main function is completely unrelated to the circumference you declared in your function. Take a look at your text's treatment of the scope of variables.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.