
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:
(x2x1)^2+(y2y1)^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;
}

March 30th, 2017, 02:22 PM
#2
Re: Issue with c++ code
Note that you are using SQ (defined as 2) for 2 different reasons  as a power and as a multiplier of 2. It would be better if these different uses had different names.
Also in c++ it is good practice to use a const int rather than a define for constants eg
Code:
const int sq = 2;
const double pi = 3.1416;
For circumference() and area(), the parameter should be of type double, not type int  as given the coords of the centre and a point on the circ, the radius may not be a whole number!
Also, the diameter of a circle is twice the radius  not equal!
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only  not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums  and not via private messages!
C++17 Compiler: Microsoft VS2019 (16.7.4)

March 31st, 2017, 01:58 AM
#3
Re: Issue with c++ code
Originally Posted by 2kaud
Note that you are using SQ (defined as 2) for 2 different reasons  as a power and as a multiplier of 2. It would be better if these different uses had different names.
The problem still remains because the power of SC is semantically different in every formula where it is used. To resolve this one would have to introduce a different SC for each occurrence. This shows that SC really does not qualify as a global constant, it is inherently local in nature.
Compare: Say you have two arrays in different parts of your program and the only thing they have in common is the same size 10. Should you then make 10 a global constant and use it to define both? No, because the two 10 are unrelated and only have local meaning (where they still can be defined as constants of course).
So for this reason I think pow(x,2) is better then pow(x,SC) where SC is a global constant. If SC on the other hand is locally defined (in a context where it keeps its semantic even if it were changed), that would be fine.
Finally for the sake of convenience and code readability I suggest pow(x, 2) is replaced with a square function, like
Code:
double sqr(double x) {
return std::pow(x,2);
}
or even,
Code:
double sqr(double x) {
return x*x;
}
Last edited by wolle; March 31st, 2017 at 02:31 AM.
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
OnDemand Webinars (sponsored)
