[] comparision

M

mm

Hi,

Given a couple of arrays with values:

[3] [2] [1] [0]
A 2 9 9 9
B 3 0 0 0

what is the best way to compare these values assuming that array position
[0] represents the least significant value.
ie: A<B is true in this example.

Currently I'm iterating through the arrays checking each position and
setting flags as appropriate. Is there a better way to achieve this goal.

Thanks, Matt.
 
J

Jeroen Mostert

mm said:
Given a couple of arrays with values:

[3] [2] [1] [0]
A 2 9 9 9
B 3 0 0 0

what is the best way to compare these values assuming that array position
[0] represents the least significant value.

int valA = a[3] * 1000 + a[2] * 100 + a[1] * 10 + a[0];
int valB = b[3] * 1000 + b[2] * 100 + b[1] * 10 + b[0];

Now compare valA and valB.

If your problem is more general than this, generalize appropriately...
 
P

Pavel Minaev

Hi,

Given a couple of arrays with values:

        [3]  [2]  [1]  [0]
A      2     9     9    9
B      3     0     0    0

what is the best way to compare these values assuming that array position
[0] represents the least significant value.
ie: A<B is true in this example.
Currently I'm iterating through the arrays checking each position and
setting flags as appropriate. Is there a better way to achieve this goal.

If the numbers in your arrays have reasonable limited range, then
Jeroen's solution is the best one (assuming you can fit the result
into a value of some numeric type, even if it's Decimal). Otherwise,
there are various approaches, but they all boil down to iterating two
collections in parallel anyway, even if it's not explicit. Note also
that you don't really need to set any flags - you just start from the
most important elements, and move on to least important. As soon as a
pair of digits compares unequal, you immediately know that one array
is "less" or "greater" than another.
 
B

Ben Voigt [C++ MVP]

Jeroen said:
mm said:
Given a couple of arrays with values:

[3] [2] [1] [0]
A 2 9 9 9
B 3 0 0 0

what is the best way to compare these values assuming that array
position [0] represents the least significant value.

int valA = a[3] * 1000 + a[2] * 100 + a[1] * 10 + a[0];
int valB = b[3] * 1000 + b[2] * 100 + b[1] * 10 + b[0];

Yuck yuck yuck. This requires reading all the values. Not to mention
assumptions about the range of values in each element.

Just iterate and return on the first encountered difference. Then only the
identical prefix will need to be read, not the entire arrays.
 
J

Jeroen Mostert

Ben said:
Jeroen said:
mm said:
Given a couple of arrays with values:

[3] [2] [1] [0]
A 2 9 9 9
B 3 0 0 0

what is the best way to compare these values assuming that array
position [0] represents the least significant value.
int valA = a[3] * 1000 + a[2] * 100 + a[1] * 10 + a[0];
int valB = b[3] * 1000 + b[2] * 100 + b[1] * 10 + b[0];

Yuck yuck yuck. This requires reading all the values. Not to mention
assumptions about the range of values in each element.
What, no mention about the most obvious assumption of all, that the arrays
have exactly four elements? :)

That's why I wrote

You get what you pay for.
 
F

Family Tree Mike

mm said:
Hi,

Given a couple of arrays with values:

[3] [2] [1] [0]
A 2 9 9 9
B 3 0 0 0

what is the best way to compare these values assuming that array position
[0] represents the least significant value.
ie: A<B is true in this example.

Currently I'm iterating through the arrays checking each position and
setting flags as appropriate. Is there a better way to achieve this goal.

Thanks, Matt.


No matter what base system (base-10, base-16, base-4102, etc), it comes down
to this:

1. Check the length of the arrays. The longer is the larger number.
2. If both are the same length, itterate from the top index until one of the
arrays has a larger value than the other. If you get to the end, then they
are equal.
 
M

mm

This is what I am currently doing. Just to clarify too, that the arrays are
not fixed length and may be anywhere from 1 to 100+ elements. Thanks for the
replies - loks like I'll stick with what I've got. Matt.

Family Tree Mike said:
mm said:
Hi,

Given a couple of arrays with values:

[3] [2] [1] [0]
A 2 9 9 9
B 3 0 0 0

what is the best way to compare these values assuming that array position
[0] represents the least significant value.
ie: A<B is true in this example.

Currently I'm iterating through the arrays checking each position and
setting flags as appropriate. Is there a better way to achieve this goal.

Thanks, Matt.


No matter what base system (base-10, base-16, base-4102, etc), it comes
down to this:

1. Check the length of the arrays. The longer is the larger number.
2. If both are the same length, itterate from the top index until one of
the arrays has a larger value than the other. If you get to the end, then
they are equal.
 
P

Peter

Jeroen said:
Ben said:
Jeroen said:
mm wrote:
Given a couple of arrays with values:

[3] [2] [1] [0]
A 2 9 9 9
B 3 0 0 0

what is the best way to compare these values assuming that array
position [0] represents the least significant value.
int valA = a[3] * 1000 + a[2] * 100 + a[1] * 10 + a[0];
int valB = b[3] * 1000 + b[2] * 100 + b[1] * 10 + b[0];

Yuck yuck yuck. This requires reading all the values. Not to
mention assumptions about the range of values in each element.

If the specifications are not sufficiently precise, a good programmer
always assumes things that make his job easier.

What, no mention about the most obvious assumption of all, that the
arrays have exactly four elements? :)

And always exactly the same 4 elements? The original spec says: "Given
a couple of arrays with values...".
 
J

jmostert

Jeroen said:
Ben said:
Jeroen Mostert wrote:
mm wrote:
Given a couple of arrays with values:
       [3]  [2]  [1]  [0]
A      2     9     9    9
B      3     0     0    0
what is the best way to compare these values assuming that array
position [0] represents the least significant value.
 int valA = a[3] * 1000 + a[2] * 100 + a[1] * 10 + a[0];
 int valB = b[3] * 1000 + b[2] * 100 + b[1] * 10 + b[0];
Yuck yuck yuck.  This requires reading all the values.  Not to
mention  assumptions about the range of values in each element.

If the specifications are not sufficiently precise, a good programmer
always assumes things that make his job easier.
What, no mention about the most obvious assumption of all, that the
arrays have exactly four elements? :)

And always exactly the same 4 elements? The original spec says: "Given
a couple of arrays with values...".

No, that's where you can confidently draw the line -- because if they
were always the same elements in both arrays, the answer would be a
constant and there would be no need to solve the problem
algorithmically at all. Since the OP asked for such a solution, we may
assume the element values, at least, *can* vary. :)
 
J

Jeff Johnson

If the specifications are not sufficiently precise, a good programmer
always assumes things that make his job easier.

Then I must be a bad programmer, because I always go back to the source and
ask for clarification.
 
J

Jeroen Mostert

Jeff said:
Then I must be a bad programmer, because I always go back to the source and
ask for clarification.
Well now.

There are certainly jobs where you're handed specifications and are in no
position to ask for clarification, if only because it would take time you're
not getting. You can either demand better working conditions or make do with
the best assumptions you can manage. You are probably expected to do the
latter, but you can always try the former.

On the other hand, there are also jobs where you are in the position of
being able to ask people for clarification on anything, as many times as is
required, until you've hammered out a specification that doesn't raise
questions on either end. Even so, just because you're in that position
doesn't mean you're going to get it right, since either end could
inadvertently introduce assumptions that should have been questions.

Things often hover about in the middle, and we probably get the jobs we
deserve anyway.
 
P

Peter

Jeroen said:
There are certainly jobs where you're handed specifications and are
in no position to ask for clarification, if only because it would
take time you're not getting. You can either demand better working
conditions or make do with the best assumptions you can manage. You
are probably expected to do the latter, but you can always try the
former.

On the other hand, there are also jobs where you are in the position
of being able to ask people for clarification on anything, as many
times as is required, until you've hammered out a specification that
doesn't raise questions on either end. Even so, just because you're
in that position doesn't mean you're going to get it right, since
either end could inadvertently introduce assumptions that should have
been questions.

Things often hover about in the middle, and we probably get the jobs
we deserve anyway.

Yes - and just to be clear, what I wrote was not serious, it was just
for fun. Often, if questions arise with regards to specifications (and
in my experience they always do), we usually make some "good"
assumptions, present them to the customer (usually via email as quite
often our customers are not available at the precise moment we require
their attention), and continue working. Usually this works well. The
customer will either very quickly tell us our assumptions are incorrect
and explain their specifications better, or accept our decisions.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top