|
-
March 30th, 2017, 01:04 PM
#1
Issue with c++ code
Hey! I am having issue with my code, the calculations and cout won't come out correct. Any help is much appreciated.
The following formula gives the distance between two points, (x1, y1) and (x2, y2) in the Cartesian plane:
(x2-x1)^2+(y2-y1)^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 the user to enter the center and a point on the circle. The program should then output the circle’s radius, diameter, circumference, and area. Your program must have at least the following function:
Code:
//Preprocessor Directives
#include<iostream>
#include<cmath>
#include<iomanip>
//defining constants
#define sq 2
#define PI 3.1416
using namespace std;
//delclaring var
double distance(int x1, int x2, int y1, int y2);
double radius(int x1, int x2, int y1, int y2);
double circumference(int r);
double area(int r);
a. distance: This function takes as its parameters four numbers that represent two 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 returns the circle’s radius.
c. 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 2pr.)
d. Area: This function takes as its parameter a number that represents the radius of the circle and returns the circle’s area. (If r is the radius, the area is PIr^2.)
Assume that p = 3.1416.
//making calc
double distance(int x1, int x2, int y1, int y2)
{
return sqrt(pow(x2 - x1, sq) + pow(y2 - y1, sq));
}
double radius(int x1, int x2, int y1, int y2)
{
return distance(x1, x2, y1, y2);
}
double circumference(int r)
{
return sq * PI * r;
}
double area(int r)
{
return PI * pow(r, sq);
}
// main function
int main()
{
int x1, y1, x2, y2;
cout << "Enter x and y cordinates of center of circle: " << endl;
cin >> x1 >> y1;
cout << "Enter x and y cordinates of a point of the circle: "<< endl;
cin >> x2 >> y2;
double r = radius(x1,y1,x2,y2);
double c = circumference(r);
double a = area(r);
//out put to user
cout << "\nRadius of circle: " << r;
cout << "\nDiameter of circle: " << r;
cout << "\nCircumference of circle: " << c;
cout << "\nArea of circle: " << a;
system("pause");
// exit main
return 0;
}
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|