hi guys! i am doing a bubble sort on using the C language. I have to prompt the user to enter 10 integers values of his/her choice.
Store these values into an array in the order the user entered them.
Call a function that will display the values entered in either
i) Ascending order
ii) Descending order
i have already written it but i need some suggestions on how to improve my program. I was told to use use the Do While function but i cant think of anything.
thanks!
here is what have come up with:
Store these values into an array in the order the user entered them.
Call a function that will display the values entered in either
i) Ascending order
ii) Descending order
i have already written it but i need some suggestions on how to improve my program. I was told to use use the Do While function but i cant think of anything.
thanks!
here is what have come up with:
Code:
# include<stdio.h> void array_direction(int a[],char k); void main (void) { int n; int z[10]; char order; int s; printf("Please enter the values of your array:\n"); for(n=0;n<10;n++) { printf(" please enter %d:",n+1); scanf("%d",&z[n]); } printf(" How do you want your array sorted\n"); fflush(stdin); printf("Pease enter A for ascendind order or D for descending order\n:"); scanf("%c",&order); array_direction(z, order); printf(" The order is:\n"); for(s=0;s<10;s++) { printf("%3d ",z[s]); } } void array_direction(int x[],char k) { int pass; int t; int hold; if( k=='a'||k=='A') { for (pass=0;pass<10; pass++) { for(t=0;t<10;t++) { if (x[t]>x[t+1]) { hold=x[t]; x[t]=x[t+1]; x[t+1]=hold; } } } } if(k=='d'||k=='D') { for (pass=0;pass<10; pass++) { for(t=0;t<10;t++) { if(x[t]<x[t+1]) { hold=x[t]; x[t]=x[t+1]; x[t+1]=hold; } } } } }
Comment