![]() |
|
#1
|
|||
|
|||
Sorting arrays need helpI am tring to write a program that implements this sort.
Here is the heart of a simple transpsition sort. What are I doing wrong? ******************** CPP / C++ / C Code:
thanks for any assistance USN CPP / C++ / C Code:
/*Unordered data 7 3 66 3 -5 22 -77 2 First pass -77 7 3 66 3 -5 22 2 Second pass -77 -5 7 3 66 3 2 22 Third pass -77 -5 2 7 3 66 3 22 Fourth pass -77 -5 2 3 7 3 66 22 Fifth pass -77 -5 2 3 3 7 22 66 Sixth pass -77 -5 2 3 3 7 22 66 Seventh pass -77 -5 2 3 3 7 22 66 */ Last edited by dsmith : 23-Oct-2004 at 06:58.
Reason: Please use [c] & [/c] for syntax highlighting
|
|
#2
|
||||
|
||||
|
Hi doc_watson2007. Welcome to GIDForums.
There are several problems that I see with your program, but probably the most noticeable is the formatting. The basic structure of your program should be grouped more like: CPP / C++ / C Code:
Notice that the indenting helps to identify which code should be in which loop. Your print statements are also slightly misplaced I believe. You should sort the entire structure as shown above and then do a completely new loop which prints the entire array. Also, you need to write the swap function. The basis of which is: CPP / C++ / C Code:
HTH. __________________
The best damn Sports Blog period. |
|
#3
|
|||
|
|||
sorting arrQuote:
Now I am getting a undef ref for swap(&arr[i], &arr[j]); /* Compare Numbers */ if (arr[i] > arr[j]) swap(&arr[i], &arr[j]); } } void swap(int *, int *); { int tmp; tmp = i; i = j; j = tmp; } return 0; } |
|
#4
|
|||
|
|||
|
function definition is problem. Are you defining your swap function code inside main() ? Is yes, then you can not do that in C. You have to move the function code outside main() either before or after. Secondly, You are referencing i and j inside swap function without declaring them. Also, i & j are pointers variables and not regular variables. So you can not use
CPP / C++ / C Code:
it should be CPP / C++ / C Code:
In case you are defining your swap function code after main(). To be able to compile without errors, You need to prototype your swap function before main().Following should be your program structure. CPP / C++ / C Code:
Use code tags when you are posting code. |
|
#5
|
|||
|
|||
sorting arraysQuote:
Thanks . This is what I have so far still head banging. CPP / C++ / C Code:
Last edited by dsmith : 01-Nov-2004 at 07:11.
Reason: Please use [c] & [/c] for syntax highlighting
|
|
#6
|
||||
|
||||
|
Hi doc_watson2007. Please, please, please use [c] & [/c], when you post C (or C++) code to this forum. Also, I think that this is a must read for proper formatting. Indentation and the such may seem like a little thing, but it is extremely important to readability and will help you catch errors in your code.
The problem with your code is that you keep switching between a swap function that takes two integer pointers and a swap function that takes two ingegers. Right now, you are passing the integers, but your function is looking for pointers. You must pass by pointer, in order for your swap function to act upon the actual values and not just a copy of the values. Therefore, this line should be changed to pass an address like you originally had: CPP / C++ / C Code:
Your swap function is correct in header definition, but remember, since you are using pointers to integers, you need to use * notation to specify the value of the what the pointer points to. Check out the differences between your swap function and nkhambal's swap function. Hint: his is correct. ![]() Good luck! [ __________________
The best damn Sports Blog period. |
|
#7
|
|||
|
|||
|
Another problem with your program is the for loops you are using for sorting the array. You are not using { } brackets after each "for" statment. If you do not use {} brackets after any of your loop conditions, only the statment next to your loop condition will be executed.
Following lines of code CPP / C++ / C Code:
Will effectively only execute first printf in the inner for loop. You need to use {} properly in your loops to get the required results. Also, there is a problem with the sort logic in your for loops. It should be. CPP / C++ / C Code:
You can add another for loop after the sorting operation is complete, to print the sorted array, instead of printing array elements inside sorting loops (unless being used for debugging purpose.) Good luck. |
|
#8
|
|||
|
|||
array swappingQuote:
CPP / C++ / C Code:
|
|
#9
|
|||
|
|||
|
I don't see any problem with your code, except one typo which is causing you entire function definition of order to be commented out.
Following line had problem CPP / C++ / C Code:
It should be CPP / C++ / C Code:
Following code complied correctly and executed and given proper result to me. CPP / C++ / C Code:
Following was the output when I executed it. Quote:
The reason its printing only pass 9 is because you have not used {} after if statement. That why if is only executing order function calls specified number times by two for loops. When it is out of for loops, its executing your printf statment. If you want print to be executed in each pass you need to use {} properly. I would like you to try it out yourselve. |
|
#10
|
||||
|
||||
|
Quote:
Your bracketting and formatting makes your program very difficult to follow. Check the format tutorial... CPP / C++ / C Code:
__________________
Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough. -- Pearl Williams |
Recent GIDBlog
Writing a book by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Deleting elements of arrays C++ | the_crazyman | C++ Forum | 25 | 30-May-2008 08:27 |
| Knight tour (arrays help needed) | dilmv | C++ Forum | 7 | 18-Oct-2004 15:31 |
| problem with arrays | melas | C Programming Language | 5 | 12-Oct-2004 05:18 |
| sorting question | fj8283888 | C Programming Language | 1 | 13-Apr-2004 21:42 |
| need help with passing 3 arrays into a function | tommy69 | C Programming Language | 14 | 07-Apr-2004 01:22 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The