Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <ctime>
using namespace std;
// below are the prototypes
double randomDouble(const double& from, const double& to);
double lineSegLen(const float& lp0x, const float& lp0y, const float& lp1x, const float& lp1y);
bool lineInCircle(const float& lp0x, const float& lp0y, const float& lp1x, const float& lp1y, const float& cmpx, const float& cmpy, const float& radius);
int main () // start the main function
{
double lp0x, lp1x, lp0y, lp1y;
double cmpx,cmpy;
double radius;
double distance;
double diameter;
srand(time(0));
lp0x=(rand() % (999+999+1)-999) / 10.0;
lp1x=(rand() % (999+999+1)-999) / 10.0;
lp0y=(rand() % (999+999+1)-999) / 10.0;
lp1y=(rand() % (999+999+1)-999) / 10.0;
cmpx=(rand() % (999+999+1)-999) / 10.0;
cmpy=(rand() % (999+999+1)-999) / 10.0;
radius=(rand() % (999-1+1)+1) / 10.0;
cout << "Line segment's 1st x coordinate: " << lp0x << endl;
cout << "Line segment's 1st y coordinate: " << lp1x << endl;
cout << "Line segment's 2nd x coordinate: " << lp0y << endl;
cout << "Line segment's 2nd y coordinate: " << lp1y << endl;
cout << "\n" << endl; // space
cout << "Circle's Midpoint x coordinate: " << cmpx << endl;
cout << "Circle's Midpoint y coordinate: " << cmpy << endl;
cout << "Circle's Radius: " << radius << endl;
distance = sqrt(pow(lp0x-lp1x,2.0) + pow(lp0y-lp1y,2.0));
cout << "\n" << endl; // space
diameter = 2 * radius;
if (distance<diameter)
cout << "The line segment is in the circle!\n";
else
cout << "The line segment is not in the circle!\n";
cout << "\n" << endl; // space
return EXIT_SUCCESS;
}
#define RANDBL(min,max) min + (((double)rand() / RAND_MAX) * (max-min));
lp0x=RANDBL(1,100);
double randomDouble(double from, double to)
{
return from + ( ( ( double ) rand() / RAND_MAX ) * ( to - from ) );
}
// Call like this
lp0x = randomDouble(1,100);
The Macro will be slightly more efficient than the function as the code required for pushing parameters to the stack and calling the function will be eliminated - in reality though the saving is almost negligable. So it comes down to preference.lp0x=RANDBL(1,100);
1 + (((double)rand() / RAND_MAX) * (100-1))
int max(int a, int b)
{
if (a < b) return b;
return a;
}
...
int maxval = max(10,20);
cout << "Max : " << maxval << endl;
int a = 20;
int b = 50;
maxval = max(a, b);
cout << "Max : " << maxval << endl;
In this function we don't want to change the values passed to the function - all we want is an answer as to which is the max. As you will notice in this code we can either pass constant values or variable namesvoid swap(int a, int b)
{
int temp;
temp = a;
a = b;
b = temp;
}
int var1 = 10;
int var2 = 20;
swap(var1,var2);
// Output: a and b will NOT be swapped
In this example even though the swap code will in fact change the values of a and b around this will not affect the var1 and var2 values.// The int * means a pointer to an int. It is telling the compiler that our function
// expects to receive a pointer to a memory location that stores an int - and not
// the value of an int
void swap(int * a, int * b)
{
int temp;
// To access the value of the pointer we have to put a * in front to tell the compiler
// don't use the value of this variable (which would be a memory address) but rather
// use that value to lookup in memory where the value is stored and use that value
temp = *a;
*a = *b;
*b = temp;
}
int var1 = 20;
int var2 = 30;
// To call it we would need to pass the address of the variables - not their value
// To do that we prefix the variable with the '&' like this
swap(&var1,&var2);
In the above example the address of var1 and var2 are passed to the swap function which in turn is modified to have the parameters treated as pointers to int's rather than int values.void swap(int & a, int & b)
{
int temp = a;
a = b;
b = temp;
}
int var1 = 20;
int var2 = 30;
swap(var1,var2);
In the above example we are passing the variables by refernce but we don't have to worry about pointer values.Im sure there are other ways to do things but Im just looking to convert this code accordingly.
I think you will find - given you are in a learning situation - experts to write all the code for you.#include <iostream>
#include <cstdlib>
#include <cmath>
#include <ctime>
using namespace std;
double randomDouble();
double lineSegLen(const double& lp0x, const double& lp0y, const double& lp1x, const double& lp1y);
bool lineInCircle(const double& lp0x, const double& lp0y, const double& lp1x, const double& lp1y, const double& cmpx, const double& cmpy, const double& radius);
int main ()
{
double lp0x, lp1x, lp0y, lp1y;
double cmpx,cmpy;
double radius;
double distance;
double diameter;
srand(time(0));
lp0x= randomDouble();
lp1x= randomDouble();
lp0y= randomDouble();
lp1y= randomDouble();
cmpx= randomDouble();
cmpy= randomDouble();
radius= randomDouble();
cout << "\n" << endl; // space
cout << "Line segment's 1st x coordinate: " << lp0x << endl;
cout << "Line segment's 1st y coordinate: " << lp1x << endl;
cout << "Line segment's 2nd x coordinate: " << lp0y << endl;
cout << "Line segment's 2nd y coordinate: " << lp1y << endl;
cout << "\n" << endl; // space
cout << "Circle's Midpoint x coordinate: " << cmpx << endl;
cout << "Circle's Midpoint y coordinate: " << cmpy << endl;
cout << "Circle's Radius: " << radius << endl;
distance = lineSegLen(const double& lp0x, const double& lp0y, const double& lp1x, const double& lp1y);
cout << "\n" << endl; // space
lineInCircle(const double& lp0x, const double& lp0y, const double& lp1x, const double& lp1y, const double& cmpx, const double& cmpy, const double& radius);
cout << "\n" << endl; // space
return EXIT_SUCCESS;
}
double randomDouble()
{
return = (rand() % (999+999+1)-999) / 10.0;
}
double lineSegLen(const double& lp0x, const double& lp0y, const double& lp1x, const double& lp1y)
{
return = sqrt(pow(lp0x-lp1x,2.0) + pow(lp0y-lp1y,2.0));
}
bool lineInCircle(const double& lp0x, const double& lp0y, const double& lp1x, const double& lp1y, const double& cmpx, const double& cmpy, const double& radius, const double& diameter)
{
diameter = 2 * radius;
if (distance<diameter)
return = cout << "The line segment is in the circle!\n";
else
return = cout << "The line segment is not in the circle!\n";
}
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <ctime>
using namespace std;
double randomDouble();
double lineSegLen(const double& lp0x, const double& lp0y, const double& lp1x, const double& lp1y);
bool lineInCircle(const double& lp0x, const double& lp0y, const double& lp1x, const double& lp1y, const double& cmpx, const double& cmpy, const double& radius, const double& diameter);
int main ()
{
double lp0x, lp1x, lp0y, lp1y;
double cmpx,cmpy;
double radius;
double distance;
double diameter;
srand(time(0));
lp0x= randomDouble();
lp1x= randomDouble();
lp0y= randomDouble();
lp1y= randomDouble();
cmpx= randomDouble();
cmpy= randomDouble();
radius= randomDouble();
cout << " -----------" << endl;
cout << "| Program 3 |" << endl;
cout << " -----------" << endl;
cout << "\n" << endl; // space
cout << "Line segment's 1st x coordinate: " << lp0x << endl;
cout << "Line segment's 1st y coordinate: " << lp1x << endl;
cout << "Line segment's 2nd x coordinate: " << lp0y << endl;
cout << "Line segment's 2nd y coordinate: " << lp1y << endl;
cout << "\n" << endl; // space
cout << "Circle's Midpoint x coordinate: " << cmpx << endl;
cout << "Circle's Midpoint y coordinate: " << cmpy << endl;
cout << "Circle's Radius: " << radius << endl;
distance = lineSegLen(lp0x,lp0y,lp1x,lp1y);
diameter = 2 * radius;
cout << "\n" << endl; // space
lineInCircle(lp0x,lp0y, lp1x, lp1y,cmpx, cmpy, radius, diameter);
cout << "\n" << endl; // space
return EXIT_SUCCESS;
}
double randomDouble()
{
return (rand() % (999+999+1)-999) / 10.0;
}
double lineSegLen(const double& lp0x, const double& lp0y, const double& lp1x, const double& lp1y)
{
return sqrt(pow(lp0x-lp1x,2.0) + pow(lp0y-lp1y,2.0));
}
bool lineInCircle(const double& lp0x, const double& lp0y, const double& lp1x, const double& lp1y, const double& cmpx, const double& cmpy, const double& radius, const double& diameter)
{
if (distance<diameter)
cout << "The line segment is in the circle!\n";
else
cout << "The line segment is not in the circle!\n";
}
bool lineInCircle(const double& lp0x, const double& lp0y, const double& lp1x, const double& lp1y, const double& cmpx, const double& cmpy, const double& radius, const double& diameter)
{
diameter = 2 * radius;
return (distance<diameter);
}
// Then in your code
if (lineInCircle(lp0x,lp0y, lp1x, lp1y,cmpx, cmpy, radius, diameter);)
cout << "The line segment is in the circle!\n";
else
cout << "The line segment is not in the circle!\n";
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <ctime>
using namespace std;
double randomDouble();
double lineSegLen(const double& lp0x, const double& lp0y, const double& lp1x, const double& lp1y);
bool lineInCircle(const double& lp0x, const double& lp0y, const double& lp1x, const double& lp1y, const double& cmpx, const double& cmpy, const double& radius, const double& diameter, const double& distance);
int main ()
{
double lp0x, lp1x, lp0y, lp1y;
double cmpx,cmpy;
double radius;
double distance;
double diameter;
srand(time(0));
lp0x= randomDouble();
lp1x= randomDouble();
lp0y= randomDouble();
lp1y= randomDouble();
cmpx= randomDouble();
cmpy= randomDouble();
radius= randomDouble();
cout << "\n" << endl; // space
cout << "Line segment's 1st x coordinate: " << lp0x << endl;
cout << "Line segment's 1st y coordinate: " << lp1x << endl;
cout << "Line segment's 2nd x coordinate: " << lp0y << endl;
cout << "Line segment's 2nd y coordinate: " << lp1y << endl;
cout << "\n" << endl; // space
cout << "Circle's Midpoint x coordinate: " << cmpx << endl;
cout << "Circle's Midpoint y coordinate: " << cmpy << endl;
cout << "Circle's Radius: " << radius << endl;
distance = lineSegLen(lp0x,lp0y,lp1x,lp1y);
diameter = 2 * radius;
cout << "\n" << endl; // space
lineInCircle(lp0x,lp0y,lp1x,lp1y,cmpx,cmpy,radius,diameter,distance);
if (lineInCircle(lp0x,lp0y,lp1x,lp1y,cmpx,cmpy,radius,diameter,distance))
cout << "The line segment is in the circle!\n";
else
cout << "The line segment is not in the circle!\n";
cout << "\n" << endl; // space
return EXIT_SUCCESS;
}
double randomDouble()
{
return (rand() % (999+999+1)-999) / 10.0;
}
double lineSegLen(const double& lp0x, const double& lp0y, const double& lp1x, const double& lp1y)
{
return sqrt(pow(lp0x-lp1x,2.0) + pow(lp0y-lp1y,2.0));
}
bool lineInCircle(const double& lp0x, const double& lp0y, const double& lp1x, const double& lp1y, const double& cmpx, const double& cmpy, const double& radius, const double& diameter, const double& distance)
{
return (distance<diameter);
}
Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.
Have a better answer? Share it in a comment.
(One interesting aside, is that if you count the total number of generated points, and count the number of random points that fit inside a circle that is inside a bounding square, then if you have a good random generator, you can approximate the value PI by comparing the area of the circle with the area of the square.).