Friday, May 21, 2010

How to display bubble sorted array in c?

this is my code:





void sortarr(){


int max,maxindex=0,temp;


int arr[10];


int a,b;





for(a=9;a%26gt;=0;a--){


max=arr[0];


maxindex=0;


for(b=1;b%26lt;=a;b++){


if(arr[b]%26gt;max){


max=arr[b];


maxindex=b;


}


}


temp=arr[a];


arr[a]=arr[maxindex];


arr[maxindex]=temp;


}


}

How to display bubble sorted array in c?
Well, the first thing that's troubling is that there isn't any initialization of the array. That is, you haven't assigned any values to the array members, so there will be "garbage" in the array. Depending upon which compiler you are using, you may get warning messages about trying to access an uninitialized variable. You should add some code to initialize the array members:


arr[0] = 7;


arr[1] = 55;


arr[2] = 17;


etc.





To print out the members of your array, just add another "for" loop:


for (a=0; a%26lt;= 9; ++a) {


printf("arr[%d] = %d\n", a, arr[a]);


}

flower seeds

No comments:

Post a Comment