Tuesday, April 5, 2016

C program to shift all the negative values in left in array

#include<stdio.h>
void main()
{
 clrscr();
 int n;
 int a[10]={4,8,6,-9,0,6,11,-4,3,7};
 int i,j;
 int count=0;
 int temp;
 for(i=0;i<10;i++)
  {
   printf("%d ",a[i]);
  }
 if(a[0]<0)
  {
   count=1;
  }
 for(i=1;i<10;i++)
  {
   if(a[i]<0)
    {
     for(j=i;j>count;j--)
      {
       temp=a[j];
       a[j]=a[j-1];
       a[j-1]=temp;
      }
     count=count+1;
    }
  }
 printf("\n");
 for(i=0;i<10;i++)
  {
   printf("%d ",a[i]);
  }
}

output
-9 -4 4 8 6 0 6 11 3 7

1 comment:

  1. Thats what i was looking for ...but you should also add explanation of the program in comments.

    ReplyDelete