Help with code..

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
int o = 45341;
int x = 600;
int b = 802;
int d = 230;
int t = 1000;
int y = 424;
int z = 432;
void Form1::Form1_Load(System::Object^  sender, System::EventArgs^  e)

{
	
	if (abs(x - z) < o)
		o=abs(x - z);

	if (abs(b - z) < o)
		o=abs(b - z);

	if (abs(d - z) < o)
		o=abs(d - z);

	if (abs(t - z) < o)
		o=abs(t - z);

	if (abs(y - z) < o)
		o=abs(y - z);

	int oo1;
	int oo2;
	oo1 = z+o;
	oo2 = z-o;
	this->label24->Text = oo1.ToString();
	this->label25->Text = oo2.ToString();
	this->label26->Text = o.ToString();
}

This code will give me the closest number to z, but what if i want to get the 2nd closest number? Anyone have any ideas on how i can do this?
Repeat the algorithm using the closest value to z as z. But in practice you should add all those into a System::List<int> collection and sort! Then find in the collection the closest number to z. The second closest is iether the next to the closest, or the previous to the closest.
Thanks for the quick answer. Also can you explain a bit more i don't quit understand what to do, quit new at this.
Which part you need help with? For repeating the algorithm you would just copy/paste that code, and re-run all those if's of yours, but before you run for the second time, you do z = oo1 (or oo2, check you code; side note: You should use more meaningful variable names).

If you are wondering about System::List<int>, it really was System::Collections::Generic::List<int>. My bad. See http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx .
Thanks for the clarification.

Also i did..

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
	int oo1;
	int oo2;
	int oo11;
	int oo22;
	int oo111;
	int oo222;

int o = 45341;
int x = 600;
int b = 802;
int d = 230;
int t = 1000;
int y = 424;
int z = 400;
void Form1::button1_Click(System::Object^  sender, System::EventArgs^  e)
{

	if (abs(x - z) < o)
		o=abs(x - z);

	if (abs(b - z) < o)
		o=abs(b - z);

	if (abs(d - z) < o)
		o=abs(d - z);

	if (abs(t - z) < o)
		o=abs(t - z);

	if (abs(y - z) < o)
		o=abs(y - z);

	oo1 = z+o;
	oo2 = z-o;
	this->label27->Text = oo1.ToString();
	this->label28->Text = oo2.ToString();
	this->label29->Text = o.ToString();
	
	//*/
}

void Form1::button2_Click(System::Object^  sender, System::EventArgs^  e)
{
	z = oo1;

	if (abs(x - z) < o)
		o=abs(x - z);

	if (abs(b - z) < o)
		o=abs(b - z);

	if (abs(d - z) < o)
		o=abs(d - z);

	if (abs(t - z) < o)
		o=abs(t - z);

	if (abs(y - z) < o)
		o=abs(y - z);

	oo11 = z+o;
	oo22 = z-o;
	this->label30->Text = oo11.ToString();
	this->label31->Text = oo22.ToString();
	this->label32->Text = o.ToString();
	
	//*/
}

void Form1::button3_Click(System::Object^  sender, System::EventArgs^  e)
{
	z = oo2;

	if (abs(x - z) < o)
		o=abs(x - z);

	if (abs(b - z) < o)
		o=abs(b - z);

	if (abs(d - z) < o)
		o=abs(d - z);

	if (abs(t - z) < o)
		o=abs(t - z);

	if (abs(y - z) < o)
		o=abs(y - z);

	oo111 = z+o;
	oo222 = z-o;
	this->label2->Text = oo111.ToString();
	this->label3->Text = oo222.ToString();
	this->label4->Text = o.ToString();
	
	//*/
}


Nothing is coming up right.
Hey, it's your algorithm, not mine. Debug it to determine where it goes wrong. Or you might want to give the List<> collection a try.
Topic archived. No new replies allowed.