# Thread: Determining order from a list of integers...

1. ## Determining order from a list of integers...

Hi,

I've a random list of distinct integers as:

1 8 90 1 8 10 11 12 9 5 4 2

I want to find a series of ordered numbers (ascending or descending) with each series having length >3.

E.g.
1 < 8 < 90 has a strict order but this will not be returned since its length is <=3
But 1 < 8 < 10 < 11 < 12 will be returned since it has a strict order and has length = 5.
Similarly 9 > 5 > 4 > 2 will also be returned.

Could anyone please suggest an ways to implement this?

Thanks,
Angkar

2. Something like this:
Code:
```for(start = 0; start < numbers; start ++) {
for(x = start+1; !fail && x < numbers; x ++) {
if(number[x-1] < number[x]) n ++;
else fail = 1;
}

if(!fail && n > 3) {}

for(x = start+1; x < numbers; x ++) {
/* ... */
}
}```

3. When I do the following:

Code:
```for(start = 0; start < 7; start ++)
{
int fail=0;
for(x = start+1; !fail && x < 7; x ++)
{
if(n[x-1] < n[x]) count++;
else fail = 1;
}

if(!fail && count > 3)
{
printf("strict order found %d \n", count);
}

/*for(x = start+1; !fail && x < 7; x ++)
{
if(n[x-1] > n[x]) count++;
else fail = 1;
}*/

}```
I've "strict order found ... " printed 4 times but i have only 1 strict order and thats the 1st one!
Where am I going wrong?

Thanks,
Angkar

4. If you have a sequence 1,2,3,4,5 the algorithm will find 1,2,3,4,5 and 2,3,4,5 amd 3,4,5.

That code was just meant to be a starting point.

5. I would suggest somting simpler

Code:
```int   n = sizeof(arr)/sizeof(arr[0]);
int   i, strt, j;

i = 0;
while ( i < n - 1)
{
strt = i;
while ( i < n - 1 && arr[i] < arr[i+1] )
++i;
if ( i - strt >= 3 )
{
for ( j = strt ; j <= i ; ++j )
printf("%4d", arr[j]);
printf("\n");
}
++i;
}```

6. The last piece of code seems to have some logical error! Its not working...

7. To be more specific the output for 1 8 90 1 8 10 11 12 9 5 4 2
should be -
1 8 10 11 12
9 5 4 2
But this code is returning:
90 1 8 10 11 12 for the ascending order!

How to get rid of that 90?

8. Also, how to handle the other order (I mean descending here) ?

Thanks for all your help,
Angkar

9. Also if I've multiple ascending/descending orders, how do I store them?
Say for
1 8 90 1 9 10 11 12 6 5 4 2 17 21 29 34 27 26 25 23
I should have:
Arr(>,1) = 1 9 10 11 12
Arr(>,2) = 17 21 29 34
Arr(<,1) = 6 5 4 2
Arr(<,2) = 27 26 25 23
.
.

10. This algorithm is not being able to detect the break of order e.g.
for:
1 8 90 1 9 10 11 12 6 5 4 2 17 21 29 34 27 26 25 23
and ascending order and >=3 we have:
2 17 21 29 34 as one of the outputs instead of 17 21 29 34 since there is a break in the order after 2.

Any ideas?

11. Originally Posted by AngKar
To be more specific the output for 1 8 90 1 8 10 11 12 9 5 4 2
should be -
1 8 10 11 12
9 5 4 2
But this code is returning:
90 1 8 10 11 12 for the ascending order!

How to get rid of that 90?
Sorry but I get :
1 8 10 11 12

Originally Posted by AngKar
Also, how to handle the other order (I mean descending here) ?

Thanks for all your help,
Angkar

You already have the ascending, does'nt it give any idea
how to do the descending ?
Hint : try to follow the same logic.

12. I have written the same function with just the sign changed and that seems to work fine.
But I don't know why its returning 90 1 8 10 11 12 for me!
When I change for ( j = strt ; j <= i ; ++j ) to j = strt + 1 everyhting works but for the 1st element of the arr!
Also, are there any array bounds for running C in windows? I've declared an array[3600] and it fails after 1340!
Any reasons for this?

13. Nope it was a problem with my definition!Its resolved now!

Popular pages Recent additions