Question:
Urgent C++ array sorting question?
mossman
2008-03-20 16:20:50 UTC
I have to create a C++ program that has one main function and one void function. "sortof" must be the void function's name. the void function has 2 arguements, which are both 1-D arrays of length 4. the main program asks the user for 4 different numbers. these are stored in one of the arrays. the function is then called and it sorts the numbers form smallest to biggest in the second array. the main program then prints both the input array and the sorted array to the screen. As far as sorting goes, i'm lost. it says i'm supposed to just determine the max, the nin and then do the same for the two left behind. HELP PLEASE!!!
Four answers:
2008-03-21 10:22:14 UTC
I have a quicksort function on my website. It is easy and can be implemented in your code



iamtruancy.co.nr

Navigate to c++, click the link "median" or "mode"



void ord(int size){



int i, j;



//test original number against all



for (i = 0; i < size; i++){



for (j = i + 1; j <= size; j++){



//arr is your array

//if new number is bigger than swap them



if ( arr[j] > arr[i] )



swap(arr[j], arr[i]);}



//show array



cout << endl << arr[i]; }}
2008-03-20 16:25:51 UTC
I do not under stand where you are stuck at. Did you get the arrays to work? Or are you stuck on that? If you need any help go on google and search for "C++ Array Functions" and you should get some help.
venkatesh
2008-03-21 00:25:10 UTC
void sortof(double f[], double s[] ,int size)//u need to pass size also

{

int temp;

//copy the original array

for(int i=0;i
s[i]=f[i];

for(int i=0;i
{

if(s[i]>s[i+1])

{

temp=s[i];

s[i]=s[i+1];

s[i+1]=temp;

}

}







}

thats it



after calling sort of function



display min=s[0]

max=s[size-1]



that s all
fixedinseattle
2008-03-20 16:53:00 UTC
sounds like you need to implement a sorting function, unless you can use the standard qsort method in C.



google "quick sort" and "insertion sort" to get started on that.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...