By using this site, you agree to our Privacy Policy and our Terms of Use. Close

Forums - General Discussion - Help with C++ Issue

Hey guys,

I'm working on a C++ assignment for college. For anyone who knows some C++, I'd guess it'd be really simple, but I'm really struggling with it. Basically, I have to start by making an array of 100 random numbers between 0 and 250. Then, I have to sort the array, in descending order. Finally, I have to have the sorting of the array set up as a separate function. I can do the first 2 steps, but when I try to make the sorting of the array a separate function, I'm doing something wrong and it's not doing anything (when it even compiles at all, seeing as when I try to make changes, they are inevitably messing up the code completely). I was trying to use my most recent finished assignment (which also had a separate function for part of the program) to help, but I just can't seem to get this one working. If anyone thinks they might be able to help, could you let me know and then I can PM you with my code issues.

Hopefully someone can be of help, thanks guys!



Around the Network

I know c++ you can pm me or just post the code here your choice.



I've sent you the code by PM. I had to rename a variable, because [ b ] (without the gaps) was read as a formatting tag by the PM script :/



I sent u a pm last night. I was out shopping for black Friday. Hopefully that was helpful.



Possibly just delete the function and start again.

In any case sorting is a function which has a bunch of solutions - are you looking for any solution or for an efficient solution?

If you're looking for an efficient solution quicksort (http://en.wikipedia.org/wiki/Quicksort) is something every programmer should eventually know how to implement, however in place quicksort is actually reasonably hard to get your head around. If you don't need an efficient solution I recommend bubble sort - it has terrible efficiency (average case O(n^2)) but is ridiculously simply to write. http://en.wikipedia.org/wiki/Bubble_sort

 

Edit:

Heres a terribly optimised version of your assignment I wrote in Python to give you the idea of how bubble sort works

 

from random import sample

 

def sort(arr):

    for n in range(1, len(arr)):

        for i in range(1,len(arr)):

            if(arr[ i] > arr[i-1]):

                tmp = arr[ i]

                arr[ i] = arr[i-1]

                arr[i-1] = tmp

 

arr = sample(xrange(250),100)

sort(arr)

print(arr)