Hi!
I've got to sort an array by moving the elements. Program is getting elements one by one and sort it on board.
Example:
Input:
2
4
6
3
7
1
4
2
Output:
1: [ 2 ]
2: [ 2 4 ]
3: [ 2 4 6 ]
4: [ 2 3 4 6 ]
5: [ 2 3 4 6 7 ]
6: [ 1 2 3 4 6 7 ]
7: [ 1 2 3 4 4 6 7 ]
8: [ 1 2 2 3 4 4 6 7 ]
This is my program, but it has many bugs esspecially with the loops. I don't know how to fix it. Thanks for all you help.
Code:
#include<stdio.h>
int main()
{
int tab[100],i , x, poz, ile=1;
while(scanf("%d",x))
{
poz=0;
while(tab[poz]<x && poz<ile)
{
++poz;
}
for(i=ile;i<=poz;i--)
tab[i]=tab[i-1];
tab[poz]=x;
ile++;
printf("[");
for(i=0;i<=poz;++i)
printf(" %d ",tab[i]);
printf("]\n");
}
}