Finding Largest nondecreasing sequence in an array

Problem Statement
Write a c programme to find out largest not decreasing sequence in an array
#include<stdio.h>
void largestnondec(int a[],int i);
int main()
{
  int i,d,a[50];
  printf("enter the array \n");
  i=0;
  while(scanf("%d",&d)!=EOF)
    {
      a[i]=d;
      i++;
    }
  largestnondec(a,i);
  return 0;
}
void largestnondec(int a[],int i)
{
  int count,index,tempcnt,tempinx,h,j;
  count=tempcnt=tempinx=index=0;
  for(j=0;j<i;j++)
    {
      if(a[j+1]>a[j]&&j<i)
        {
          tempinx=j;
          tempcnt=0;
          while(a[j+1]>a[j]&&j<i)
   {
     j++;
     tempcnt++;
   }
          if(tempcnt>count)
            {
              count=tempcnt;
              index=tempinx;
            }
        }
   
    }
  printf("the sequence is\n");
  for(h=index;h<=index+count;h++)
    {
      printf("%d ",a[h]);
    }
  return ;
}

No comments:

Post a Comment