Ask your own question, for FREE!
Mathematics 22 Online
OpenStudy (anonymous):

How can I arrange the values of a,b,c in ascending order where a will gets the lowest value and variable c gets the highest value the values of the variables are: a equals 15,b equals 10,c equals 2

OpenStudy (anonymous):

use quicksort.

OpenStudy (anonymous):

bubble sort works fine too.

OpenStudy (anonymous):

can u show me the code please?

OpenStudy (anonymous):

one moment

OpenStudy (anonymous):

oh which language?

OpenStudy (anonymous):

C++

OpenStudy (anonymous):

alright, but for C++ I think there's a library function

OpenStudy (anonymous):

its okay, ^^

OpenStudy (anonymous):

http://www.cplusplus.com/reference/algorithm/sort/ go to CS area

OpenStudy (anonymous):

then what?

OpenStudy (anonymous):

Make a loop, Compare first element with subsequent elements if A[1] > A{2} => A[1]=b => A[1] = A[2] => A{2]=b Then compare A[3] and so on

OpenStudy (anonymous):

http://ideone.com/voi35

OpenStudy (anonymous):

this is the code?

OpenStudy (anonymous):

i'll just put the values??

OpenStudy (anonymous):

only the quickSort function at the very top is the sorting function

OpenStudy (anonymous):

this one? #include <iostream> using std::cout; using std::endl; void quickSort(int A[], int left, int right) { int i = left, j = right; int tmp; int pivot = A[(left + right) / 2]; while (i <= j) { while (A[i] < pivot) i++; while (A[j] > pivot) j--; if (i <= j) { tmp = A[i]; A[i] = A[j]; A[j] = tmp; i++; j--; } }; if (left < j) quickSort(A, left, j); if (i < right)quickSort(A, i, right); }

OpenStudy (anonymous):

right

OpenStudy (anonymous):

okay so where will i put the A,B,C ??

OpenStudy (anonymous):

well, except the #include <iostream> using std::cout; using std::endl; part...

OpenStudy (anonymous):

oh okay. then where will i put their values?

OpenStudy (anonymous):

you can set A,B, and C, to be integer elements of an array.

OpenStudy (anonymous):

and simply sort the array :-D

OpenStudy (anonymous):

actually im just new on C++

OpenStudy (anonymous):

and i dont know that array sorry

OpenStudy (anonymous):

I'm new to C++ too; that code is from a C implementation. In C++ there are nicer data structures like vectors and sets.

OpenStudy (anonymous):

well if you haven't gotten to arrays and pointers yet do it Ishaan's way

OpenStudy (anonymous):

can u put the A,B and C on the code and their values? im just a newbee seriously

OpenStudy (anonymous):

No need to apply sorting algorithm for only 3 values,simply write a macro that will do the job for you ..

OpenStudy (anonymous):

btw C has qsort() inbuilt which could be used but still overkill.

OpenStudy (anonymous):

yeah qsort() is a C standard library function (though it's not usually used)

OpenStudy (anonymous):

just #include <cstdlib> and call qsort()

OpenStudy (anonymous):

lolz I used it often in SPOJ and I am sure many do still now

OpenStudy (anonymous):

what??

OpenStudy (anonymous):

confused O__O"

OpenStudy (anonymous):

#include <cstdlib> is C++ header

OpenStudy (anonymous):

#include <stdlib.h> is more C

OpenStudy (anonymous):

we were using C++

OpenStudy (anonymous):

okay lets go back to my problem. so how can i start doing it?

OpenStudy (anonymous):

start writing a macro it's really simple.

OpenStudy (anonymous):

what's macro? sorry :(

OpenStudy (anonymous):

Something like this: #define M(a,b) ((a)>(b))?(a):(b)

OpenStudy (anonymous):

then?

OpenStudy (anonymous):

sorry.., I am not doing your homework

OpenStudy (anonymous):

Hmm you can use If-else for comparing 3 integers

OpenStudy (anonymous):

yeah, either a function or a macro

OpenStudy (anonymous):

if else would be cubersome Ishaan ..

OpenStudy (anonymous):

*cumbersome

OpenStudy (anonymous):

actually this isn't a homework. i just want to learn but i can't

OpenStudy (anonymous):

Max = M(M(a,b),c); there you go..

OpenStudy (anonymous):

if-else would be too much for my really small brain ...

OpenStudy (anonymous):

and in C++ you can use stl algorithms then it will be cake-walk

OpenStudy (anonymous):

the one that you've given to me,the code, i will use that for my problem?

OpenStudy (anonymous):

here's the thing. can u show me how to do it? then i'll try to do that and see if i can make another one.

OpenStudy (anonymous):

Right If-else would be tough

OpenStudy (anonymous):

i just dont know how to do it. please show me the code then i'll make another one which i will answer all by myself.

OpenStudy (anonymous):

please? anyone? :((

OpenStudy (anonymous):

if you new to programming choose another language to learn, like JAVA or Python if it's not your homework :D

OpenStudy (anonymous):

yep it wasn't but i want to learn this thing. if u dont want me to help then fine :/

OpenStudy (anonymous):

why you want to learn C++?

OpenStudy (anonymous):

to build computer viruses!

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!