Determining order from a list of integers...

This is a discussion on Determining order from a list of integers... within the C Programming forums, part of the General Programming Boards category; Hi, I've a random list of distinct integers as: 1 8 90 1 8 10 11 12 9 5 4 ...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    141

    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. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    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 ++) {
            /* ... */
        }
    }
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    141
    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. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    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.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    95
    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. #6
    Registered User
    Join Date
    Dec 2005
    Posts
    141
    The last piece of code seems to have some logical error! Its not working...

  7. #7
    Registered User
    Join Date
    Dec 2005
    Posts
    141
    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. #8
    Registered User
    Join Date
    Dec 2005
    Posts
    141
    Also, how to handle the other order (I mean descending here) ?

    Thanks for all your help,
    Angkar

  9. #9
    Registered User
    Join Date
    Dec 2005
    Posts
    141
    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. #10
    Registered User
    Join Date
    Dec 2005
    Posts
    141
    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. #11
    Registered User
    Join Date
    Nov 2005
    Posts
    95
    Quote 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

    Quote 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. #12
    Registered User
    Join Date
    Dec 2005
    Posts
    141
    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. #13
    Registered User
    Join Date
    Dec 2005
    Posts
    141
    Nope it was a problem with my definition!Its resolved now!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 07-05-2010, 10:43 AM
  2. Adding nodes to a linked list
    By bluescreen in forum C Programming
    Replies: 4
    Last Post: 11-09-2006, 12:59 AM
  3. Anyone good with linked list.....I am not....
    By chadsxe in forum C++ Programming
    Replies: 11
    Last Post: 11-10-2005, 01:48 PM
  4. 1st Class LIST ADT
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 11-09-2001, 06:29 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21