[C++]Passing array to function properly

Questions about programming languages and debugging
Post Reply
User avatar
ayu
Staff
Staff
Posts: 8109
Joined: 27 Aug 2005, 16:00
18
Contact:

[C++]Passing array to function properly

Post by ayu »

Right, now this is an assignment from my book where I'm suposed to just make a small program that gives me 10 random numbers, then sort them, and then present the sorted ones on screen. Now that part of the program has been done for hours now. What I can't figure out is how to make my sorting function more dynamic. I want it to be able to accept any array (int) and then sort it and return it. But when I use:

Code: Select all

sizeof arr / sizeof arr[0]


inside of the sorting function, it returns with the value "1". But if I use it in the main function where the array is made in the first place, it works like a charm. Which makes me think that the array isn't passed to the function properly. But, how can it then sort the numbers? I'm greatly confused ^^

Anyway, here's my code

http://code.suck-o.com/74 (no I didn't comment it, since it's just a small assignment I couldn't be arsed to)
"The best place to hide a tree, is in a forest"

User avatar
Big-E
Administrator
Administrator
Posts: 1332
Joined: 16 May 2007, 16:00
16
Location: IN UR ____ , ____ING UR _____ .
Contact:

Post by Big-E »

I am not entirely familiar with C++ though that code looks as if youcould use the following algorithms:

Bubble sort:
Has an large complexity and is rather inefficent.

While SWAPPED
For each START to Finish
if A[ i ] > A[ i + 1]
swap A and A[i +1]
SWAPPED = True
end if
end for
end while


Another Option:

Selection Sort which has better complexity than bubble sort.

for i ← 0 to n-2 do
min ← i
for j ← (i + 1) to n-1 do
if A[j] < A[min]
min ← j
swap A and A[min]


Finally, you can do a divide and conquer approach.

insertionSort(array A)
for i = 1 to length[A]-1 do
begin
value = A
j = i-1
while j ≥ 0 and A[j] > value do
begin
A = A[j]
j = j-1
end
A[j+1] = value
end

Maybe you could do an implementation of each and submit them all. Saying you figured out that x algorithm is more efficient than y algorithm.


PS. Though, I remembered the applicable cases when to use the algoirthms, I actually had to google the algorithm itself..sad, I need to get back into an OO programming language.

G-Brain
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 467
Joined: 08 Nov 2007, 17:00
16
Location: NL

Post by G-Brain »

Big-E wrote:PS. Though, I remembered the applicable cases when to use the algoirthms, I actually had to google the algorithm itself..sad, I need to get back into an OO programming language.
Sorting algorithms really don't have anything to do with object orientation.
I <3 MariaLara more than all of you

User avatar
Big-E
Administrator
Administrator
Posts: 1332
Joined: 16 May 2007, 16:00
16
Location: IN UR ____ , ____ING UR _____ .
Contact:

Post by Big-E »

Should have been a period.

I was implying, after only using procedural language for so long, I should get back into an OO language - a la C++/Java..I guess even scripting languages have some sort of OO functionality, now..

Thanks for your clarification though.

User avatar
Nerdz
The Architect
The Architect
Posts: 1127
Joined: 15 Jun 2005, 16:00
18
Location: #db_error in: select usr.location from sucko_member where usr.id=63;
Contact:

Post by Nerdz »

Bubble sort sucks. Go for the heapsort.
Give a man a fish, you feed him for one day.
Learn a man to fish, you feed him for life.

Post Reply