c - Find Sequences of any range -


given array of numbers, print each , every range available. example array : 9, 3, 5, 7, 4, 8, 1 output: 1, 3-5, 7-9 note: please execute problem without using additional array.

how proceed? *

#include<stdio.h> int main() { int a[]={9,8,8,7,6,5,14}; int n= sizeof(a) / sizeof(a[0]); int i,j; int temp; for(i=0;i<n;i++)          {                for(j=i+1;j<n;j++)                {                      if(a[i]>a[j])                      {                            temp=a[i];                            a[i]=a[j];                            a[j]=temp;                      }                }         } } 

* 1st sort in ascending order, don't know next? p.s : coding in c.

the next step identify sequences. try following loop (not debugged):

first= next= a[0]; (i=1; i<n; i++) {     if (a[i] > next+1) {         if (next>first)              printf("%d-%d,", first, next);         else printf("%d,", first);         first= next= a[i];     }     else next++; } 

Comments

Popular posts from this blog

qt - Using float or double for own QML classes -

Create Outlook appointment via C# .Net -

ios - Swift Array Resetting Itself -