![]() |
|
#1
|
|||
|
|||
Sorting with struct and arraysHey guys, i really need help with this problem, it s due by Tuesday and i hope someone will be able to help me with this.
This program works perfectly fine, i just need to add a sorting function that would sort either the exams, grade, avg, or even the name using the struct. I am reading from a file called "studInfo.txt" and displaying the results on "output.txt". I would need to sort any of the following and display the result in the program itself, not the "output.txt". So if someone could help me, i would greatly appreciate. I would rather have someone helping me sorting the exams or avg from the struct. Thank you very much Here is the studInfo needed: Code:
Here is the code, which works perfectly fine...just needs a sorting function: CPP / C++ / C Code:
Last edited by admin : 08-Dec-2008 at 00:39.
Reason: Please insert your example C/C++ codes between [CPP] and [/CPP] tags
|
|||
|
#2
|
|||
|
|||
Re: Sorting with struct and arraysWouldnt by chance be in the same class as this guy, would you?
__________________
My personal site: Utilities for text processing, debugging, testing and plotting |
|
#3
|
|||
|
|||
Re: Sorting with struct and arrayshaha yea, we are actually pretty much partners...but i wanted to try and see if someone else would give better explainations to this problem. Previous answers to this problem did not help very much.
Do you have any idea how to do this...sort in a fucntion using the struct? |
|
#4
|
|||
|
|||
Re: Sorting with struct and arraysPHP Code:
Adding a line below to your struct and write your regular sorting algorithm. bool operator < (const Grades& rhs) {return avg < rhs.avg;} you can replace avg by other variable in your struct for comparision. |
|
#5
|
|||
|
|||
Re: Sorting with struct and arraysQuote:
Suppose you have an array of n "somethings." If you want to sort the array, you must have defined some ways of determining whether one "something" is less than another "something," whether one "something" is equal to another "something," and whether one "something" is greater than another "something." (Otherwise, how can you tell whether it is sorted? Not only wouldn't you not know where to start; you wouldn't know when you were done!) The principle of the bubble sort can be illustrated as follows (where I use C language notation for the loops): Code:
After the code has executed both of the loops all of the times, the smallest element will be first and the largest will be last in the array. There are more efficient ways (and there are simple improvements of the bubble sort to make it more efficient), but at first, let's stick with something that works and is relatively easy to understand. What the heck do I mean by an array of "somethings"? You could have an array of integers. You could have an array of floats. You could have an array of structs. The sorting procedure and principle is the same for all. If the arrays are numerical data types (integers or floats or doubles) there are built-in comparison operators '<', '==' and '>' for "is less than," "is equal to," and "is greater than." Here is a complete example for arrays integers: CPP / C++ / C Code:
Output: Code:
Now if you have an array of structs of your own design, there is, obviously, no built-in operator to compare relative values of the structs. Consider your struct: CPP / C++ / C Code:
If you want to sort according to the struct data member string "lname", you can use the standard string comparison operator ">" in the sort loops: CPP / C++ / C Code:
Using this code, you can make a function for sorting Grades structs by lname. Just follow the design of isort() above. CPP / C++ / C Code:
Now, what if you want to sort according to avg? Make a pair of loops wherein the comparison uses the avg members: CPP / C++ / C Code:
Since the code inside the loop is different from the previous example, if you want to make this sort routine a function, it will be a different function from the one above. As you get more "into" it, you may discover that there are ways of making a single sort function that can work on anything that you want, but you have to, somehow, tell it how to do the comparison. Bottom line: Regardless of the type of sort procedure, all sort routines need some way of knowing how to do the comparison. Regards, Dave Last edited by davekw7x : 08-Dec-2008 at 18:54.
|
|
#6
|
|||
|
|||
Re: Sorting with struct and arraysWOW DAVE, thank you sooo much, this really helped, right on time too!
I greatly appreciate your time. Thank you so much, Frenchy |
Recent GIDBlog
Programming ebook direct download available by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Sorting (structures and arrays fun fun..) | odin607 | C Programming Language | 7 | 08-Feb-2008 18:58 |
| 2D arrays:dynamic allocation and freeing | bravetanveer | C Programming Language | 48 | 27-Nov-2007 16:55 |
| Arrays as function arguments - return arrays from functions | pisuke | C Programming Language | 2 | 25-Jul-2007 12:03 |
| Linked list | wedel | C Programming Language | 9 | 24-Jun-2007 23:45 |
| working with struct and array | sasukekun | C++ Forum | 2 | 06-May-2006 00:27 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The