Homework regarding functions.

Hey all...
I have a homework assignment and I am stumped.
Here is the assignment:

The following formula gives the distance between 2 points x1,x2 and y1,y2 in the cartesian plane:
sqrt(pow(x1-x2,2) - pow(y1-y2,2))

given the center and a point on the circle, you can use this formula to find the radius of the circle. Write a program that prompts user to enter the center a point on the circle. The program should then output the circle radius, diameter, circumference and area. your program must have at least the following function:

a)distance:this function takes as its parameters four numbers that represent tow points in the plane and returns the distance between them.

b)radius:this function takes as its parameters four numbers that represent the center and a point on the circle, calls the function distance to find the radius of the circle, and return the circle radius.

c)circumference: this function takes as its parameter a number that represents the radius of the circle and returns the circle circumference.

d)area: this function takes as its parameter a number that represents the radius of the circle and return the circle area

assume the pi = 3.1416

Here is my code. I get an error!!
1>circle.obj : error LNK2019: unresolved external symbol "double __cdecl area(double)" (?area@@YANN@Z) referenced in function _main
1>circle.obj : error LNK2019: unresolved external symbol "double __cdecl circumference(double)" (?circumference@@YANN@Z) referenced in function _main
1>circle.obj : error LNK2019: unresolved external symbol "double __cdecl radius(int,int,int,int)" (?radius@@YANHHHH@Z) referenced in function _main
1>circle.obj : error LNK2019: unresolved external symbol "double __cdecl distance(int,int,int,int)" (?distance@@YANHHHH@Z) referenced in function _main
1>C:\Users\Tracy\Documents\Visual Studio 2010\p359num8\Debug\p359num8.exe : fatal error LNK1120: 4 unresolved externals

Someone point me in the right direction... please

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include<iostream>
#include<cmath>

using namespace std;

double distance (int p, int q, int r, int s);
double radius (int p, int q, int r, int s);
double circumference (double r1);
double area (double r1);

int main ()
{
	int x1, x2, y1, y2;
	double r, d, c, a, p, s, q;

	cout<<"Enter the center of the circle: "<<endl;
	cin>>x1>>y1;
	cout<<"Enter an outer point on the circle: "<<endl;
	cin>>x2>>y2;
	d= distance (x1, y1, x2, y2);
	r= radius (x1, y1, x2, y2);
	c= circumference (r);
	a= area (r);

		cout<< "Distance between points: "<<d<<endl;
		cout<< "Radius of circle: "<<r<<endl;
		cout<< "Circumference of the circle: "<<c<<endl;
		cout<< "Area of circle: "<<a<<endl;

		    double distance (int p, int q, int r, int s);
				double d1;
				d1 = sqrt(pow(r-p,2) + pow (s-q, 2));
				return d1;
			double radius (int p, int q, int r, int s);
				double r1;
				r1= distance (p,q,r,s);
				return r1;
			double circumference (double r1);
				double c1;
				c1= 2*3.1416*r1;
				return r1;
			double area (double r1);
				double a1;
				a1= 3.1416*r1*r1;
				return a1;

		system ("pause");

}
You don't define even one of those functions, you just declare them.
I'm not following.... please explain.
Well, what you are trying to do is define the functions:

1
2
3
4
double distance (int p, int q, int r, int s);
double radius (int p, int q, int r, int s);
double circumference (double r1);
double area (double r1);


you don't do that however. Well, you attempt to, here:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
double distance (int p, int q, int r, int s);
				double d1;
				d1 = sqrt(pow(r-p,2) + pow (s-q, 2));
				return d1;
			double radius (int p, int q, int r, int s);
				double r1;
				r1= distance (p,q,r,s);
				return r1;
			double circumference (double r1);
				double c1;
				c1= 2*3.1416*r1;
				return r1;
			double area (double r1);
				double a1;
				a1= 3.1416*r1*r1;
				return a1;


But function definitions must be done outside of the main function, and they have this syntax:
1
2
3
4
returnvalue functionname ( parameterlist )
{
// code
}


and not the one in your code.
1
2
3
4
5
6
7
double distance;
			double d1 (int p, int q, int r, int s)
			{
				double d1;
				d1 = sqrt(pow(r-p,2) + pow (s-q, 2));
				return d1; 
			}	


Like that?
Uhhh... not really. But you'll get one more shot before I spit it out :O
1
2
3
4
5
6
7
// function definition for area
double area (double base, double height)
{
	double value;
	value = base * height;
	return value;
}


That's the code the instructor gave as an example. I'm not looking for the easy way out, and don't mean to bug you.
And the code was placed outside of main
Uh yeah, compare your instructors code:
1
2
3
4
5
6
double area (double base, double height)
{
	double value;
	value = base * height;
	return value;
}


to your code

1
2
3
4
5
6
7
double distance;
			double d1 (int p, int q, int r, int s)
			{
				double d1;
				d1 = sqrt(pow(r-p,2) + pow (s-q, 2));
				return d1; 
			}	


You can spot the differences, can't you?
Yeah. the p, q, r, and s are int when they should be double
I'm afraid that's not it at all... are you sure you did comprehend the whole idea of functions and datatypes?
I know there may be an error calling by reference when it should be called by value.... am I getting warmer?
No, let's do this line by line:

double area (double base, double height)
This marks the beginning of the function. double is the return type of the function. area is the name of the function. The part in the brackets is the parameterlist, in this case the function takes 2 double arguments.
{
Marks the beginning of the function block.
double value;
declares a local variable of the type value
value = base * height;
Sets value to base*height (the names of the passed arguments).
return value;
Jumps back to where the function was called from, and sets the return value to value.
}
marks the end of the function block

Yours was almost right. I'll correct it for you, if you understood my explanation you should get it then:

1
2
3
4
5
6
7
double distance; //this doesn't belong here
			double d1 (int p, int q, int r, int s) //this should be double distance (int p, int q, int r, int s)
			{
				double d1;
				d1 = sqrt(pow(r-p,2) + pow (s-q, 2));
				return d1; 
			}	
I updated the code... and it almost works..
Please point me in the right direction!
The math is off. Or my brain is off.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include<iostream>
#include<cmath>

using namespace std;

double distance (int p, int q, int r, int s);
double radius (int p, int q, int r, int s);
double circumference (double r1);
double area (double r1);

int main ()
{
	int x1, x2, y1, y2;
	double r, d, c, a, p, s, q;


	cout<<"Enter the center of the circle: "<<endl;
	cin>>x1>>y1;
	cout<<"Enter an outer point on the circle: "<<endl;
	cin>>x2>>y2;
	d= distance (x1, y1, x2, y2);
	r= radius (x1, y1, x2, y2);
	c= circumference (r);
	a= area (r);
	p=0;
	q=0;
	s=0;


		cout<< "Distance between points: "<<d<<endl;
		cout<< "Radius of circle: "<<r<<endl;
		cout<< "Circumference of the circle: "<<c<<endl;
		cout<< "Area of circle: "<<a<<endl;
system ("pause");}
		   double distance (int p, int q, int r, int s) //this should be double distance (int p, int q, int r, int s)
			{
				double d1;
				d1 = sqrt(pow((double)r-p,2) + pow((double)s-q, 2));
				return d1; 
			}
			double radius (int p, int q, int r, int s)
			{
				double r1;
				r1= distance (p,q,r,s);
				return r1;
			}
			double circumference (double r1)
			{
				double c1;
				c1= 2*3.1416*r1;
				return r1;
			}
			double area (double r1)
			{
				double a1;
				a1= 3.1416*r1*r1;
				return a1;
			}

		
closed account (D80DSL3A)
I have noticed that the pow() sometimes chokes when fed integer arguments.
Try modifying line 38 to:
 
d1 = sqrt(pow( (double)(r-p), 2.0) + pow( (double)(s-q), 2.0));


Alternately, you could skip using the pow() since finding a square is trivial.
 
d1 = sqrt( static_cast<double>( (r-p)*(r-p) + (s-q)*(s-q) ) );


Note that you really don't need the function radius(). All it does is call distance().
The program could be simplified in other ways as well. Hint: You have more variables being used than are needed for the overall task.
Topic archived. No new replies allowed.