0

I have an array of numbers (int numoftoys[]) and a parallel array of strings (string names[]) whose entries correspond to the numbers. My assignment requires me to sort the number array so when I print, it prints the highest number first, then the next highest, and so on. How would I do that and keep the array that holds the names synced up with the number array?

Rob Kennedy
  • 161,384
  • 21
  • 275
  • 467
Sam LaManna
  • 425
  • 2
  • 7
  • 15
  • 4
    Have you tried to do any work on this at all? – Marcin Dec 08 '11 at 16:33
  • Well, to be fair, since Sam doesn't know how to start in the first place, it would be stupid to expect any code from him for us to review. A gentle nudge into the right direction would do. – sbi Dec 08 '11 at 16:35
  • 4
    @sbi: That's what his tutor is for. Not us. – Lightness Races in Orbit Dec 08 '11 at 16:36
  • OTOH, Sam, you will have to specify some of the constraints. Sorting in C++ is simple, using the standard library. (Hence my first take on an answer.) But are you even allowed to do that? Or do you have to implement your own sorting algorithm? – sbi Dec 08 '11 at 16:37
  • @Tomalak: That's how it worked where you and I studied. It might not be as easy for everyone, though. (And, principally, _any_ homework question could be deflected with "ask your tutor", so it's not a valid objection, IMO.) – sbi Dec 08 '11 at 16:39
  • 2
    @sbi: It's a valid objection if, like me, you feel that all questions with the `homework` tag ought to be summarily nuked. :) And, no, anyway asking for corrections or presenting a specific issue is quite different from just throwing the homework question out at the internet. – Lightness Races in Orbit Dec 08 '11 at 16:41
  • @sbi There are no constraints on how i sort, just that i sort the data – Sam LaManna Dec 08 '11 at 16:42
  • @Tomalak: http://meta.stackexchange.com/a/70155/133368 – sbi Dec 08 '11 at 16:47
  • 1
    @sbi: Yes, I've read that. Just because you post an URL to it doesn't mean I will suddenly magically agree. – Lightness Races in Orbit Dec 08 '11 at 16:49
  • @Sam: Well, then my idea doesn't violate the constraints. `:)` Most of us would write this within a few minutes. You would take longer, but it gives you the opportunity to learn to use the std lib, which is a handy thing to know. – sbi Dec 08 '11 at 16:49
  • @Tomalak: Posting the URL spared me the trouble of reiterating the arguments. `:)` – sbi Dec 08 '11 at 16:50
  • @sbi: Well I can't disagree with _that_ ;) – Lightness Races in Orbit Dec 08 '11 at 16:50
  • Ive read over this: http://mathbits.com/mathbits/compsci/arrays/bubble.htm and i stil dont understand it fully. (better now but still dont know what to do) – Sam LaManna Dec 08 '11 at 16:51
  • What is the array named in the example at the bottom of that page that i linked above? – Sam LaManna Dec 08 '11 at 16:52
  • The linked code is *not* using arrays! Unless that site is for your class, I would not use it. If it is for your class, then you are not using arrays! – crashmstr Dec 08 '11 at 16:54
  • Its not, i found it in google. That explains why it didnt make sense.... – Sam LaManna Dec 08 '11 at 17:03

2 Answers2

3

Since this is homework, I'm not going to post any code, but here's an idea:

Put your data into a std::map, and iterate over that. Iteration over a map is in order of the keys.

sbi
  • 219,715
  • 46
  • 258
  • 445
  • 3
    The sort key is required to be the # of toys produced, which is not necessarily unique, so `std::map<>` is out. But, `std::set >` would work wonderfully. – Robᵩ Dec 08 '11 at 17:57
  • @Rob: I _can_ read into the question that the "numbers" are the count of products, but I can assume them to be arbitrary other numbers and the question fits that as well. However, _if_ what you say is true, using a `std::multi_map` should might be easier than a set of pairs. – sbi Dec 08 '11 at 20:31
1

you need to do a bubble sort (http://en.wikipedia.org/wiki/Bubble_sort) and when you swap the array you're sorting on, swap the ones that are in parallel to it.

crashmstr
  • 28,043
  • 9
  • 61
  • 79
tb.
  • 723
  • 7
  • 16
  • ok so i add code that says if you swap something, swap that over there too? – Sam LaManna Dec 08 '11 at 16:35
  • yes, when you swap the numbers, swap the elements in name array as well - the ones with the same indices as the indices you are swapping in the number array – tb. Dec 08 '11 at 16:43
  • 2
    Yes, @Crashmstr. It's a perfectly legitimate algorithm, it's easy to implement, and, most importantly for this situation, it's easy for beginners to understand and work through by hand. It's probably the algorithm being taught in class. – Rob Kennedy Dec 08 '11 at 17:02
  • @RobKennedy I know, I know. And it is a viable solution for solving the problem as well. P.S. mergesort is my favorite for performance and understandability. – crashmstr Dec 08 '11 at 17:05
  • Wish I had never learned bubble sort. So many other sorts that I rather have at my finger tips... – Michael Dorgan Dec 08 '11 at 17:19
  • 1
    @crashmstr: My favorite for understandability and performance is `std::sort()`. Everybody knows it, it's guaranteed to be bug free, and I have yet to run into a situation where its performance isn't adequate. – sbi Dec 08 '11 at 20:28