Not getting the right output

Sorry for the numerous posts I made. As I'm new to C++, I really do need help. Hope you will understand. The center test failed to get the correct output but I already made a center function which I thought was correct and now I really do not know why it failed. Can anyone enlighten me? Thanks!

Output:
- Polygon::center test failed
5.91573 8.56946
1.93828 8.13865

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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
class Object
{
public:

private:
	float d;
public:
	Object(float n) : d(n){}
struct PointType
{
	float x;
	float y;
	PointType( float x1,  float y1) :x(x1),y(y1){}
	PointType(){}
};
};
class Point :public Object 
{
 
private:
    PointType mpoint;
	
public:

    Point(const PointType& pt, float& y1) : mpoint(pt), Object(y1) {}
	
  	 Point center() 
	{
	     return *this;
	}
	  float x()
	{
		return mpoint.x2;

	}
	  float y()
	{
		return mpoint.y2;
	}
};
	
class Polygon :  public Point
{
private:
	Object::PointType *mpt;
	int msize;
	
public:
	   PointType& operator[](int index)  
	 {
		 return mpt[index];
	 }
	Polygon(PointType* points, int npoints, float depth)
	:  msize(npoints), mpt{new Object::PointType[npoints]}, Point(*mpt,depth){
		for(int i = 0; i < msize; ++i)
		{
		 mpt[i]  = points[i];
		}
	}
	Polygon center() 
	{
	     return *this;
	}
	 float x()
	{
		return mpt-> x2;
	}
	 float y()
	{
		return mpt-> y2;
	}
};

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cmath>

const float EPSILON = 1e-5f;

bool is_near(float x, float y)
{
	return std::abs(x - y) < EPSILON;
}

float frand()
{
	return 10.0f * float(rand()) / float(RAND_MAX);
}

int main()
{
	srand(unsigned(time(0)));
	int count = 0;
	int max_count = 0;

	int max_index = 3 + rand() % 8;
	float* xs = new float[max_index];
	float* ys = new float[max_index];
	float depth = frand();
	float x = 0;
	float y = 0;
	Polygon::PointType* vertices = new Polygon::PointType[max_index];
	for (int index = 0; index < max_index; ++index)
	{
		xs[index] = frand();
		ys[index] = frand();
		vertices[index] = Polygon::PointType(xs[index], ys[index]);
		x += xs[index];
		y += ys[index];
		y += ys[index];
	}
	x /= static_cast<float>(max_index);
	y /= static_cast<float>(max_index);
	Polygon polygon(vertices, max_index, depth);
	delete[] vertices;
        if (is_near(polygon.center().x(), x) &&
		is_near(polygon.center().y(), y))
	{
		++count;
	}
	else
	{
		std::cout << "  - Polygon::center test failed" << std::endl;
	}
	std::cout <<polygon.center().x()<< " " << x <<std::endl;
		std::cout <<polygon.center().y()<< " " << y <<std::endl;

	++max_count;
}
Last edited on
polygon.center() returns the first point in the list of points that makes up the polygon.
Thanks for the help. Anyway, I did try to put the first point. Is this the way to do it? becoz I am still not getting the correct output.
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
class Polygon :  public Point
{
private:
	Object::PointType *mpt;
	int msize;
	
public:
	   PointType& operator[](int index)  
	 {
		 return mpt[index];
	 }
	Polygon(PointType* points, int npoints, float depth)
	:  msize(npoints), mpt{new Object::PointType[npoints]}, Point(*mpt,depth){
		for(int i = 0; i < msize; ++i)
		{
		 mpt[i]  = points[i];
		}
	}

        Polygon center() 
	{
	     return *this;
	}
	 float x()
	{
		return mpt[0].x2;
	}
	 float y()
	{
		return mpt[0].y2;
	}
};
Last edited on
My point is that polygon.center() gives you the first point which is not necessarily at the centre of the polygon. I have no idea what polygon.x() and polygon.y() is supposed to return (also the centre coordinates?).

I assume x2 and y2 are the same as x and y because x2 and y2 has not been declared anywhere.

I also don't think you should have two lines that do y += ys[index]; when calculating the centre y-coordinate.
Last edited on
main() can't be modified. So theres nothing I can do about it. x2 and y2 are the same as x and y. The center is the same as the first point as what you said so it is not necessarily the center. But why am I not getting the correct output? So frustrating.
Last edited on
But why am I not getting the correct output? So frustrating.
The center is [...] not necessarily the center.


main() can't be modified. So theres nothing I can do about it.
Are you sure you haven't accidentally duplicated that line of code? It doesn't look right to me.
Oh.. yup you're right. It was accidentally duplicated by me.. Thanks Peter! However, the center function still does not work as it should. haiz~
However, the center function still does not work as it should.

It's because you haven't implemented the center() function correctly.
Oh, What am I doing wrong with my center function? I am really not sure what else to do with the center function...
Last edited on
As I have said earlier, the center() function returns the first point, not the center (average) of all points as it should.
Last edited on
hello can you help me? my output for standard derivation is incorrect. how to fix this ??

#include <iostream>
#include <math.h>

using namespace std;


int main()
{
int i,sum,sum3,sum4;
float mean,sd;
int a[101]={};
int sum2[101]={};
int b[101]={};
sum3=0;

int n;

int array[100]={0};//number of numbers must not more than 100
cout<<"This program will calculate mean and standard deviation for less 100"<<endl;
cout<<"How many numbers? " ;
cin>>n;

if (n<=100)
{
for (i=1; i<=n;i++)//the value must be positive or more than 1
{
cout<<"Please enter data number "<<i<<" : ";
cin >> a[i];
sum += a[i];
mean=sum/n;
b[i] += (a[i]-mean);
sum2[i]= pow(b[i],2);
sum3 = (sum3 + sum2[i]);
sum4 = (sum3/n);

}

sd = sqrt(sum4);
cout<<"Mean, x = "<<mean<<endl;
cout<<"standard deviation = "<<sd<<endl;
cout<<sum4<<endl;

}
else//less than 0 or negative value
{
cout<<"error";

cout<<endl<<endl;

}
}
Topic archived. No new replies allowed.