Hey guys,
this is my first time working with parallel arrays, and while I think I have a good understanding of them and how to sort them using digits, I have yet to understand or find a good example online on sorting using strings. Can anyone explain how to sort parallel arrays in descending order by string?
I can't think of an example of where parallel arrays are any better than structures. Here's how I'd do it if I had an array of students with names and student numbers.
It's is for school, and I am required to use parallel arrays.
Trust me, I have heard from many people that there are many ways to do it differently...and better...as in more efficient :/ but... requirements are requirements.. and the examples my professor gives us...do not sort by string...but by int...and he wants us to sort by string
I don't even think I've seen the words parallel arrays since my intro to C++ class lol. Anyway, strings are nothing more than a series of characters, which are nothing more than ints themselves. In memory, everything is the same. Look at this: http://www.asciitable.com/
As you can see, as you go up in alphabet the value of each letter also increases.
Yeah sort of...i guess. I mean i understand where you are coming from, just not how to put it into code. I got this...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
void SelectionSort (string Customer[], int counter)
{
int i;
int j;
int min;
for (i = 0; i < counter - 1; i++)
{
min = i;
for (j = i + 1; j < counter; j++)
if (Customer[j] < Customer[min])
min = j;
Customer[i].swap(Customer[min]);
}
}
void SelectionSort (string Customer [], double Charge [], int counter)
{
int i;
int j;
int min;
for (i = 0; i < counter - 1; i++)
{
min = i;
for (j = i + 1; j < counter; j++)
if (Customer[j] < Customer[min])
min = j;
Customer[i].swap(Customer[min]);
Charge[i].swap(Charge[min]);
}
}
I guess I don't understand what I am going to swap.
I don't want to do what I have there, I need them to be in descending order according to Customer, not Charge. So if i do what I have done above, it will put both in descending order. I need to Charge array to line up with the Customer array once that has been sorted.
How can swap do that?
void SelectionSort (string Customer [], double Charge [], int counter)
{
int i;
int j;
int min;
for (i = 0; i < counter - 1; i++)
{
min = i;
for (j = i + 1; j < counter; j++) // braces here:
{
if (Customer[j] < Customer[min])
{
min = j;
}
}
Customer[i].swap(Customer[min]);
Charge[i].swap(Charge[min]);
}
}
Once you only sort the Customer array and not the Charge array, they won't be parallel any more and there will be no more way to link one to another (without overcomplicating this issue any further).
I guess the reason it wasn't working before is...
I keep getting an error that says expression must have class type (referring to the first Charge on line 18.