1,105,556 Community Members

Help starting a program about a circle

Member Avatar
coolbreeze
Newbie Poster
15 posts since Oct 2007
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

ill show the problem in the book first then ill tell you what i know.
___________________________________________________________________________
Then following formula gives the distance between two points (x1,y1) and (x2,y2) in the Cartesian plane: √ (x2-x1)^2 + (y2-y1)^2 its the distance formula..hard to type on here

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 the user to enter the center and a point on the circle. The program should then output the circles radius, diameter, circumference, and area. The program must have at least the following functions:

distance - this function takes as its parameters four numbers that represent two points in the plane and returns the distance between them.

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 returns the circles radius.

circumference: this function takes as its parameter a number that represents the radius of the circle and returns the circle's circumference (if r is the radius, the circumference is 2*pie*r)

area: this function takes as its parameter a number that represents the radius of the circle and returns the circles area. (if r is the radius, the area is (pie)(r)^2 ...thats pie times r squared.

assume that pie=3.1416
____________________________________________________________________________

I'm just confused on how to set this all up. so i just typed in all the code i have an idea about and maybe one of you guys can help set this thing up for me and give your thoughts on how to do this.. Any information is appreciated!!! Thanks guys.

//I have to prompt to enter a center and a point on the circle so maybe something like this:
cout<<"Enter a center point: ";
cin>>x1, y1;

cout<<"Enter a point on the circle: ";
cin>>x2, y2;

//my functions will be something like this: maybe im not sure



int distance (int x1, int y1, int x2, int y2)
{ 
  int dx = x2 - x1; 
  int dy = y2 - y1; 
  float dsquared = dx*dx + dy*dy; 
  float result = sqrt (dsquared); 
  cout<<"The distance of the circle is "<<result<<"."<<endl;
} 

int radius (int x1, int y1, int x2, int y2) 
{ 
  float radius = distance (x1, y1, x2, y2); 
  float result = area (radius); 
  cout<<"The radius of the circle is "<<result<<"."<<endl;
} 

int circumference(float radius)
{
float circumference = 3.1416 * (radius * 2);
cout<<"The circumference of the circle is " <<circumference<< "."<<endl;

//ok im not sure on this one because he says the function "area" should be made void and 
//the return value should come from a reference parameter.

void area(float radius)
{
   float area = 3.1416 * radius * radius;
   return area;
}
Member Avatar
yagiD
Light Poster
35 posts since Dec 2007
Reputation Points: 0 [?]
Q&As Helped to Solve: 7 [?]
Skill Endorsements: 0 [?]
 
0
 

This may not be your complete code. As far as I can see, you may need

- function prototypes before calling them
- to be careful with types

Furthermore since c++ is there for oop, change whole thing into oop approach (i.e. to a circle class)

Member Avatar
coolbreeze
Newbie Poster
15 posts since Oct 2007
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

our professor wanted to challenge us to not declare the circle class or believe me haha, i definately would. yea it wasn't all of my code. I was having a hard time starting it, but believe it or not, I looked at what i wrote and started a very very rough draft from it. Here is what the compiler says first, then ill put my whole code up:

I think the problems i have in my mind are the declarations on what to have right beside the name in the functions... for example int circumference().. the other problems im having ill say something above the code... thanks once again!!!!!

line 8: Error: Multiple declaration for y1.
line 71: Error: Too many arguments in call to "area()".
line 71: Error: A value of type void is not allowed.
line 84: Error: "area(float)" cannot return a value.
line 89: Error: The operand "*diameter" cannot be assigned to.
line 90: Error: Cannot return int(*)(float) from a function that should return int.
6 Error(s) detected.

#include <iostream>

using namespace std;

//declare variables (I'm not really sure why it says I declared y1 twice)
int x1;
int x2;
int y1;
int y2;

//Declare functions
int distance();
int radius();
int circumference();
void area();

int main()
{

//Get a center point
cout<<"Enter a center point: ";
cin>>x1, y1;

//Get another point on the circle
cout<<"Enter a point on the circle: ";
cin>>x2, y2;

//Call the functions
int distance();
void area();
int radius();
int circumference();
int diameter();

//distance return
cout<<"The distance of the circle is: ";
int distance();

//area return
cout<<"The area of the circle is: ";
void area();

//radius return
cout<<"The radius of the circle is: ";
int radius();

//circumference return
cout<<"The circumference of the circle is: ";
int circumference();

//diamter
cout<<"The diameter of the circle is: ";
int diameter();

}

int distance (int x1, int y1, int x2, int y2)
{
  int dx = x2 - x1;
  int dy = y2 - y1;
  float dsquared = dx*dx + dy*dy;
  float result = sqrt (dsquared);
  return result;
}

int radius (int x1, int y1, int x2, int y2)
{
  float radius = distance (x1, y1, x2, y2);
  float result = area (radius);
  return result;
}

//The (float radius) on this one and the void area one I'm not sure about. The parameters
//confuse me, sorry, i'm a newbie. haha
int circumference(float radius)
{
float circumference = 3.1416 * (radius * 2);
return circumference;
}

//this one im having a hard time because our professor said the function area should be 
//made void and the return value should come from a reference parameter(which is before 
//main I think, correct me if im wrong please)
void area(float radius)
{
   float area = 3.1416 * radius * radius;
   return area;
}

//see area, circumference for confusion
int diameter(float radius)
{
 diameter = 2 * radius;
 return diameter;
}
Member Avatar
JRM
Practically a Master Poster
618 posts since Nov 2006
Reputation Points: 107 [?]
Q&As Helped to Solve: 75 [?]
Skill Endorsements: 0 [?]
 
0
 

take a look at the <math> library
pow can be used to square the numbers.

Define Pi as a global , then just use the variable.

The area function returnd a float, so it's NOT void,but :

float area(float radius)
Member Avatar
Colin Mac
Posting Whiz
327 posts since Sep 2006
Reputation Points: 53 [?]
Q&As Helped to Solve: 22 [?]
Skill Endorsements: 0 [?]
 
0
 

You're calling your functions wrongly. They should look more like this.

rad = radius(x1, y1, x2, y2);

rad, is the value you'd print out as well as pass to your other functions like

area = areaOfCir( rad );

Don't call area() inside in your radius function.
Call it from main only.

Member Avatar
gurusaranya
Newbie Poster
4 posts since May 2010
Reputation Points: -4 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

ok then post using constructor

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article