Any ideas and is it possible? How can I compare two numbers represented as an array? For example: I need to create a condition "while the first number is more than the second", and here's the catch: the numbers are represented as arrays.
Closed due to the fact that the essence of the question is incomprehensible by the participants of Kromster , aleksandr barakin , 伪位蔚蠂慰位蠀蟿 , Denis , Streletz July 13, 16 at 13:10 .
Try to write more detailed questions. To get an answer, explain what exactly you see the problem, how to reproduce it, what you want to get as a result, etc. Give an example that clearly demonstrates the problem. If the question can be reformulated according to the rules set out in the certificate , edit it .
|
1 answer
In an unsigned case, just start the comparison with the higher elements of the array. For example, considering unsigned long array of four unsigned char , you could write
int cmp(unsigned char * a, unsigned char * b) { for(int i = 3; i >= 0; --i) { if (a[i] < b[i]) return -1; if (a[i] > b[i]) return 1; } return 0; } int trivial(unsigned long a, unsigned long b) { return (a < b) ? -1 : (a > b) ? 1 : 0; } void main() { for(int i = 0; i < 1000; ++i) { unsigned long a = (rand() << 16) + rand(); unsigned long b = (rand() << 16) + rand(); if (cmp((unsigned char *)&a,(unsigned char *)&b) != trivial(a,b)) { printf("Error for %lu %lu\n",a,b); } } } In the landmark case, you must consider the sign Since different representations can use a different way of working with a sign, here you have to look at a specific implementation ...
- and what does the trivial function do? - Mar
- @ Zhenya, compares two numbers and returns the result of the comparison in the form (-1,0,1) - (less, equal, more). - 伪位蔚蠂慰位蠀蟿
|
lexicographical_compare. - 伪位蔚蠂慰位蠀蟿