Numbers question

I've googled this question many times and I've yet to come up with an answer.

Here's pretty much what i want to do. So i want to compare numbers(no limit) with one other number. Lets say..

1
2
3
4
5
6
x = 600 (always changing)
b = 802 (always changing)
d = 230 (always changing)
t = 1000 (always changing)
y = 424 (always changing)
z = 532 (always changing)


I want to compare x,b,d,t,y to z and find the closest one. I want to do this every 3 secs, also the numbers are changing every sec.

This is how i want to do it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
o = (Closest Number)

if (z > o)
{
//do something
}

and/or

if (z < o)
{
//do something
}


If anyone has a question feel free to reply I'll give you more details if needed. Thanks again I've been trying to look for an answer for a while now, and I thought it'll be better to do it this way.


You've stated what you want to do, but I really don't understand what you want. Are you having difficulty getting started, or do you have something that doesn't quite work, or perhaps you haven't started yet?
I don't know where to start? I've read tutorials online with the basic idea but end up not working the way i want it to. Since some has there limit to it, i want to do 100 numbers(or so) and compare it to 1 without using so much code but what I've seen lots of lines of code comes up.
Well, you have all these numbers that you want to compare with z. So you need to find the difference between all of them and z (one at a time) and assign the minimal difference to o.

So the algorithm looks something like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
o <- some very large number;

if absolute value of x - z  is less than o
    o <- absolute value of x - z

if absolute value of b - z  is less than o
    o <- absolute value of b - z

if absolute value of d - z  is less than o
    o <- absolute value of d - z

if absolute value of t - z  is less than o
    o <- absolute value of t - z

if absolute value of y - z  is less than o
    o <- absolute value of y - z

At this point we will have v the closest difference in o.  Now you can carry on with your z test.

if z > o
    do something
else if z < o
   do something else


I hope that helps.
Last edited on
Thanks kdw, but Im having a little trouble trying to figure out how to use the code you put?

I did..

1
2
3
4
5
6
7
8
9
10
11
int o;
int x = 600;
int b = 802;
int d = 230;
int t = 1000;
int y = 424;
int z = 532;

if "absolute value of" x - z  "is less than" o
    o  "absolute value of" x - z


and so forth, im not to good in C++. Can you help me out a bit more...
What he gave was pseudocode, it states (in text) what you should put as code statements.
http://www.cplusplus.com/reference/clibrary/cstdlib/abs/
http://www.cplusplus.com/doc/tutorial/operators/ (Relational and equality operators)
Thanks Kyon, but now im having another issue.

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
int o;
int x = 300;
int b = 202;
int d = 430;
int t = 1300;
int y = 624;
int z = 132;
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);

	this->label5->Text = o.ToString();

}


I get 0 back..

I also 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
int o;
int x = 600;
int b = 802;
int d = 230;
int t = 1000;
int y = 424;
int z = 532;
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);

	this->label5->Text = o.ToString();

}


and i got back 108. Im not sure what im doing wrong, ill be playing with the code a bit more and see if i missed something. Thanks again for your help.
abs(x - z < o) is not the same as abs(x - z) < o
Thanks kbw & Kyon, you solved my question.


Also now I'm wondering how i could get the finished number without adding it or subtraction it. Since you really don't know if you have to subtract the number or add it to the z value? Any ideas on how i could do this?
I don't quite understand the question.
If i do this..

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();
}


label24 = 440; //wrong since i added it
label25 = 424; // right since i subtracted it
label26 = 8; // the number that you need to add or subtract to get the closest number.

label25 is the right one.

But i want to find it without adding it or subtracting it, or doing it without get 2 different numbers. Only one answer is right but i get two back so how am i suppose to know which one is right without having to do it manually.
Any ideas anyone?
Topic archived. No new replies allowed.