I am stumped on how I should go about creating a loop to display values.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <iostream>
#include <iomanip>
#include <cmath>
#include <random>
#include <memory>

using namespace 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()
{
	const int SIZE = 5;
	int *myPointerX = new int[SIZE];
	int *myPointerY = new int[SIZE];

	default_random_engine engine{ static_cast<unsigned int>(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?

Link to code: http://cpp.sh/76zbd
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <iostream>
#include <iomanip>
#include <cmath>
#include <random>
#include <ctime>

class Point
{
private:
	int m_x;
	int m_y;

public:
    // explicit: https://en.cppreference.com/w/cpp/language/converting_constructor
    // member initializer list: https://en.cppreference.com/w/cpp/language/initializer_list
	explicit Point( int x = 0, int y = 0 ) : m_x(x), m_y(y) {}

	void setX( int x )
	{
		m_x = x;
	}

	void setY(int y)
	{
		m_y = y;
	}

	int getX() const // const
	{
		return m_x;
	}

	int getY() const // const
	{
		return m_y;
	}

	double distanceFromOrigin() const // const
	{
		return std::sqrt( m_x*m_x + m_y*m_y );
	}
};

int main()
{
	const int SIZE = 5;
	Point myPoints[SIZE]; // avoid new/delete

	std::default_random_engine engine{ static_cast<unsigned int>( std::time(nullptr) ) };
	std::uniform_int_distribution<int> randomIntforXorY{ -5, 5 };

    // range based loop: http://www.stroustrup.com/C++11FAQ.html#for
	for( Point& pt : myPoints ) pt = Point{ randomIntforXorY(engine), randomIntforXorY(engine) } ;

    std::cout << std::fixed << std::setprecision(2) ;
	for( Point pt : myPoints )
	{
        std::cout << "x: " << std::setw(2) << pt.getX() << "  y: " << std::setw(2) << pt.getY()
                  << "   Distance from Origin: " << pt.distanceFromOrigin() << '\n' ;
	}
}

http://coliru.stacked-crooked.com/a/f3dc1ca436525e0e
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.
Last edited on
Topic archived. No new replies allowed.