![]() |
|
#1
|
|||
|
|||
Sorting an Array using a functionHello all,
I have a program that I am currently working on that is supposed to fill an array from a text file, then (after finding min and max value of array) is supposed to sort the values from lowest to highest. All was going well until I hit a brick wall. Apparently the void function Sort (shown near bottom under sort function comment) is incorrectly being called upon or something. I am not sure, as I'm fairly new to C language. The error code i receive is: [31 F:\main1.cpp invalid use of void expression]. I THINK that's the only thing that is stopping the program from running, however i could be wrong. Okay, here is the code, please help me. CPP / C++ / C Code:
|
|
#2
|
||||
|
||||
Re: Sorting an Array using a functionThe problem is here:
CPP / C++ / C Code:
You are calling the sort function 10 times (once on each iteration of the for loop), and attempting to print its output each time, which is void. You should sort the array once, and then loop through the array and print each element, like this: CPP / C++ / C Code:
You're not initializing the variable t either. You'll need to give it a value, or none of this will work. You're also missing a closing curly brace in if-else statement. Also, if you're planning on doing bigger sorting tasks, you should consider a more efficient sorting algorithm, such as merge-sort. The difference would be negligible when the array only has ten elements, but it can make a big difference for larger arrays, __________________
www.blake-foster.com |
|
#3
|
|||
|
|||
Re: Sorting an Array using a function[REVISED]
Blake, Thank you so much, that was some solid advice. I have implemented the changes in the code, however, i believe I have made a mistake somewhere, as the array was not sorted, BUT the program did run. I have created a function to normalize the array, however i keep receiving this error:[35 C:\Dev-Cpp\main7.cpp cannot convert `double (*)(double*, int)' to `double' for argument `2' to `void norm(double*, double, double)' ] The complier points to line 35 (will be indicated with ***) I assume its from calling upon another function that uses integers in its operation. I BELIEVE a cast operator might be necessary to allow this function to work, but like I said I am a rookie. The output i am looking for is having the original array next to the sorted array. I am not sure where to begin, but i do know that the order of the first array must be preserved. Should i do this by setting a variable equal to the array within a loop? Here is the code i have so far(revised): CPP / C++ / C Code:
Help would be much appreciated, sorry for the revision, thanks for reading. |
|
#4
|
|||
|
|||
Re: Sorting an Array using a functionWell i finally got it! Thank you for your help Blake! Until next time !
|
Recent GIDBlog
Python ebook by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| run script command on ns2.26 | newbie06 | Computer Software Forum - Linux | 62 | 11-Dec-2008 00:52 |
| Getting a line error in register | oggie | MySQL / PHP Forum | 5 | 13-Apr-2008 17:16 |
| Need Help with input files. | Efferus | C++ Forum | 2 | 24-Nov-2007 17:19 |
| [Include] Doubly-linked List | dsmith | C Programming Language | 6 | 14-Apr-2006 14:12 |
| Need help deleting the last element in the array | headphone69 | C++ Forum | 2 | 15-Mar-2006 20:31 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The