Monday, December 15, 2014

Arrange array program in C with out using 3rd array

# include <stdio.h>
    # include <conio.h>
    /* arrange array */
    main()
    {
        int i[5]={4,3,1,0,2};
        int val[5]={7,9,10,1,8};
        int j,pos,tmp,tmp_pos;
        int x,y;

        for(x=0;x<=3;x++)
        {
        for(y=x+1;y<=4;y++)
        {
            if(i[x]>i[y])
            {
                tmp=val[x];
                val[x]=val[y];
                val[y]=tmp;
                tmp=i[x];
                i[x]=i[y];
                i[y]=tmp;
            }
        }
        }
        clrscr();
        for(j=0;j<=4;j++)
        printf("%3d,",val[j]);
}

No comments:

Post a Comment