Tuesday, July 14, 2009

Sorting 5 numbers using Pointers and Arrays in C++?

Hi i need to write code to sort out 5 numbers using pointers and arrays in C++. I take 5 numbers from user input. I am using a 1 dimensional array but i am not sure how i can sort them out. I have written some code below but its not sorting them out.





i need to show the unsorted sequence and then show the sorted sequence in 3 passes.





I havent much used pointers so can someone help me out on this please?





Here is my code


#include %26lt;iostream%26gt;


using namespace std;





int main()


{





int numbers [5];


int i;


int *ptr;


int *ptr=numbers;





cout%26lt;%26lt;"Please enter 5 numbers to be sorted:\n\n";


for (i = 0; i %26lt; 5 ; i++ )





cin %26gt;%26gt; numbers[i];





cout%26lt;%26lt;"\nUnsorted:\n";


for (i = 0; i %26lt;= 5-1; i++)


{


cout%26lt;%26lt; numbers [i]%26lt;%26lt;" ";


cout%26lt;%26lt;"\n\nSorted:\n";


}





for (i = 0; i %26lt;= 5-1; i++)


cout%26lt;%26lt;numbers[i]%26lt;%26lt;" ";


return 0;





}

Sorting 5 numbers using Pointers and Arrays in C++?
since you said it is not sorting,no wonder!


you need to use


the statement using


s=a[0];


for(i=0;i%26lt;4;i++)


{


if(s%26lt;a[i])


{


s=a[i];


}


}


No comments:

Post a Comment