#include <iostream>
#include <iomanip>
#include <cmath>
#include <random>
#include <memory>
usingnamespace std;
class Point
{
private:
int m_x;
int m_y;
public:
Point(int x, int y)
{
m_x = x;
m_y = y;
}
void setX(int x)
{
m_x = x;
}
void setY(int y)
{
m_y = y;
}
int getX()
{
return m_x;
}
int getY()
{
return m_y;
}
double distanceFromOrigin()
{
return sqrt(pow(m_x, 2) + pow(m_y, 2));
}
};
int main()
{
constint SIZE = 5;
int *myPointerX = newint[SIZE];
int *myPointerY = newint[SIZE];
default_random_engine engine{ static_cast<unsignedint>(time(0)) };
uniform_int_distribution<int> randomIntforx{ -5, 5 }; // random number generator for x
uniform_int_distribution<int> randomIntfory{ -5, 5 }; // random number generator for y
for (size_t i = 0; i < SIZE; i++) // generates random (x,y) coordinates
{
myPointerX[i] = randomIntforx(engine);
myPointerY[i] = randomIntfory(engine);
cout << "x: " << myPointerX[i] << setw(5) << "y: " << myPointerY[i];
cout << " Distance from Origin: # ";
if (i % 1 == 0) // starts a new line after coordinates and disance have been displayed
{
cout << endl << endl;
}
}
// below I am testing the output of the distance from origin
Point coordinates(*myPointerX, *myPointerY); // putting in the value of the first coordinates
cout << fixed << showpoint << setprecision(2);
cout << coordinates.distanceFromOrigin(); // displaying the distance from origin from the first coordinates
cout << endl << endl;
delete[] myPointerX; // freeing memory from heap
delete[] myPointerY; // freeing memory from heap
return 0;
}
The output looks something like this:
x: 1 y: -3 Distance from Origin: #
x: -3 y: -4 Distance from Origin: #
x: -5 y: -5 Distance from Origin: #
x: -5 y: 3 Distance from Origin: #
x: 1 y: -1 Distance from Origin: #
3.16
I say like "this", because the numbers will be random each time. "3.16" Is the distance from origin for the first coordinates in the output, (1, -3). The "#" after each "Distance from Origin: " is where I will put values like "3.16." I do not know how I should go about creating a loop that will do the other coordinates, and not just the first, since a pointer always points to the first address in an array. Is it it even possible to do so with how I have my program set up?
Thanks for the reply and help. Though I am not familiar with C++ 11, I can probably reverse engineer everything to what it is like in my OP. And thanks for the links. I will try to understand this new form (to me) that looks more efficient for next time.