Why is garbage value coming while executing my c programm ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MANISH AGRAWAL
    New Member
    • Aug 2010
    • 1

    #1

    Why is garbage value coming while executing my c programm ?

    /*programming for partition the elements of the array so that all the even numbers move to one side and odd numbers to the other side of the array.*/

    #include<stdio. h>
    int main()
    {
    int i,t,p,x,z,temp;
    printf("enter the size of array\n");
    scanf("%d",&i);

    int a[i];
    printf("enter the element of array\n");

    for(t=0;t<i;t++ )
    scanf("%d",&a[t]);

    for(x=0;x<i;x++ )
    {
    for(p=0;p<i;p++ )
    {
    if (a[p]%2==1)
    {
    temp=a[p];
    a[p]=a[p+1];
    a[p+1]=temp;
    }
    }
    }
    printf("require d array:");
    for(z=0;z<i;z++ )

    printf("%d\n",a[z]);
    return 0;
    }
  • newb16
    Contributor
    • Jul 2008
    • 687

    #2
    Because you read from uninitialized memory.

    Comment

    Working...