Sunday, June 6, 2021

Arranging values in an array in zig zag manner

 Few values are to be taken from user and the values will be arranged in ascending order but in a zig zag manner. The smallest value will be placed at the extreme left side (at zero index location) and the next value will be at the extreme right location (last index location). Similarly the third value in the ‘1’ index location and 4rth value at the (size-2) location of the array and so on.


#include < stdio.h>
void main()
{
 int c=0,min,minl,k,arr[100],i,j,n,t,x=0,y;
 clrscr();
 printf("\nHow many elements:");
 scanf("%d",&n);
 for(i=0;i< n;i++)
 {
  printf("\nValue=");
  scanf("%d",&arr[i]);
  }
  printf("\nOriginal=");
      for(i=0;i< n;i++)
    printf("%d   ",arr[i]);
  y=n-1;
 for(i=x;i<=y;i++)
 {
  min=arr[i];
  minl=i;
    for(j=i+1;j<=y;j++)
  {
   if(arr[j]< min)
   {
    min=arr[j];
    minl=j;
    }
    }
    if(c%2==0)
    {
     t=arr[x];
     arr[x]=min;
     arr[minl]=t;
     x++;
     }
     else
    {
    t=arr[y];
     arr[y]=min;
     arr[minl]=t;
     y--;
     i--;
     }
     c++;
     }
     printf("\nFinal=");
    for(i=0;i< n;i++)
    printf("%d   ",arr[i]);
    getch();
    }

No comments:

Post a Comment

Subscribe via email

Enter your email address:

Delivered by FeedBurner