![]() |
| ||
| Sorting a 2d array of strings in C++?? Hi y'all! I found this forum through google and I was hoping that I could get some help!! I've been assigneda rather odd assignment by my C++ teacher. I have a 2d array of names (names[17][20]) which has to be sorted alphabetically via selection sort, we can not use pointers. We then have to do a Binary Search and find a name input by the user, if the name is found we have to display it and its position in the array, if the name is not found we have to return the name and the fact that it's not found. My 3 friends and I have spent all weekend on this assignment, and searching for possible help, but we have only been able to come up with following (I'm sorry for the way this is formatted I don't know how to do the cool formatting I've seen on here) : #include <iostream>Code tags added. -Narue When it sorts, it only does about 1/2 of them and then leaves the rest (it also cuts up some of the names :mad: ). Also we can't get the search function to work! Could y'all please help us?! :cry: :cry: |
| ||
| Re: Sorting a 2d array of strings in C++?? When I have code that gives me trouble, I break it down into parts. For example, to make sure your sort works, perfect the algorithm using integers instead of strings. Then modify it to use strings. Perfect your binary search algorithm on integers, then modify it to work with strings. At that point you only need to build a framework around those two functions. It looks a lot like you were trying to chew up the whole problem at once, and you got bogged down in the slew of problems that popped up. |
| ||
| Re: Sorting a 2d array of strings in C++?? Quote:
I've tried to get it to work with ints we're trying it with ints now but translating to strings is something that we do not know how to do. |
| ||
| Re: Sorting a 2d array of strings in C++?? >I don't know how to change the code to work with strings Clearly. >we're trying it with ints now No, you're trying it with arrays of char from the looks of your code. >translating to strings is something that we do not know how to do. Now you're lying. If you and your buddies didn't know how to do it then you wouldn't have gotten so far. You're on the right track. |
| ||
| Re: Sorting a 2d array of strings in C++?? Quote:
This is the second assignment that there are no examples out of the book and our teacher's response is: "Well its in there." When pressured to point to a page his response changes to "It is." (without referencing a page or any chapter) When asked how do you do it, he responds "You do." So please if you could help us by fixing what ever little problems or at least explicitly point them out so that we can get a better feel for how to fix it. Thanks greatly! Matt LeGare, Charles Lee, John Christensen |
| ||
| Re: Sorting a 2d array of strings in C++?? This assumes you're sorting by last name: #include <cstring> |
| ||
| Re: Sorting a 2d array of strings in C++?? Narue, you're code is full of bugs. :cool: Lol, I tried running it and it displayed a lot of funny stuff. Anyway, Fodo_bett, this is the exact same problem I'm working on, whenever I'm having trouble I go to the index. I think page 221 will will help you, the major goal of the problem has a focus of bringing in past information. I haven't worked out the problem myself yet, but I think I'll figure it out by the end of the day, and I have no prior C++ experience except this class and I also have no clue what pointers are. I can also use help when it comes to sorting alphabetically. What I understand from the problem is that it wants us to first sort out the data, and then search it using a binary search method. Sorting out the string has to be sort of like sorting the names alphabetically only within the char array. After sorting them out, it's a matter of searching and comparing elements. It's a freaking pain. Also page 443 talks about arrays in the strings. |
| ||
| Re: Sorting a 2d array of strings in C++?? I think both of us read the problem wrongly... we're not trying to sort it "alphabetically" per say. It just needs to sort out the strings in such a way that we can search the array for the name. Binary search. |
| ||
| Re: Sorting a 2d array of strings in C++?? I found the solution. Use strcpy to convert string values as an assignment operatoer over the '=' sign. |
| ||
| Re: Sorting a 2d array of strings in C++?? Don't bump year old threads, it's rude. >Narue, you're code is full of bugs. ... Words fail me. Don't *EVER* say this unless you can prove it. If you can't, you've stumbled upon the worst possible insult to a programmer. >Lol, I tried running it and it displayed a lot of funny stuff. Just because you run it and it doesn't work doesn't mean the code is buggy. It's more likely that you simply don't know what you're doing, and this "bug report" proves it. Don't ever comment on my code again. |
| All times are GMT -4. The time now is 6:53 am. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC