473,769 Members | 1,674 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to sort parallel arrays in c++?

say, if you had parallel arrays containing the sales person id, the month, and
the sales figures (for that person in that month), sorting on sales figure and
preserve the order of figures within the data about the same person:

Original Data:
id month sales
+-----+ +-----+ +-----+
| 432 | | 8 | | 89 |
+-----+ +-----+ +-----+
| 123 | | 8 | | 116 |
+-----+ +-----+ +-----+
| 555 | | 8 | | 203 |
+-----+ +-----+ +-----+
| 432 | | 9 | | 105 |
+-----+ +-----+ +-----+
| 123 | | 9 | | 143 |
+-----+ +-----+ +-----+
| 555 | | 9 | | 187 |
+-----+ +-----+ +-----+

Sorted by sales:
id month sales
+-----+ +-----+ +-----+
| 432 | | 8 | | 89 |
+-----+ +-----+ +-----+
| 432 | | 9 | | 105 |
+-----+ +-----+ +-----+
| 123 | | 8 | | 116 |
+-----+ +-----+ +-----+
| 123 | | 9 | | 143 |
+-----+ +-----+ +-----+
| 555 | | 9 | | 187 |
+-----+ +-----+ +-----+
| 555 | | 8 | | 203 |
+-----+ +-----+ +-----+

is that possible to do this in c++? Thanks!
Jul 22 '05 #1
6 3531
Xiaozhu wrote:
say, if you had parallel arrays containing the sales person id, the month, and
the sales figures (for that person in that month), sorting on sales figure and
preserve the order of figures within the data about the same person:

Original Data:
id month sales
+-----+ +-----+ +-----+
| 432 | | 8 | | 89 |
+-----+ +-----+ +-----+
| 123 | | 8 | | 116 |
+-----+ +-----+ +-----+
| 555 | | 8 | | 203 |
+-----+ +-----+ +-----+
| 432 | | 9 | | 105 |
+-----+ +-----+ +-----+
| 123 | | 9 | | 143 |
+-----+ +-----+ +-----+
| 555 | | 9 | | 187 |
+-----+ +-----+ +-----+

Sorted by sales:
id month sales
+-----+ +-----+ +-----+
| 432 | | 8 | | 89 |
+-----+ +-----+ +-----+
| 432 | | 9 | | 105 |
+-----+ +-----+ +-----+
| 123 | | 8 | | 116 |
+-----+ +-----+ +-----+
| 123 | | 9 | | 143 |
+-----+ +-----+ +-----+
| 555 | | 9 | | 187 |
+-----+ +-----+ +-----+
| 555 | | 8 | | 203 |
+-----+ +-----+ +-----+

is that possible to do this in c++? Thanks!


Your problem is not defined enough. There is ambiguity. What
should happen if the initial data are

+-----+ +-----+ +-----+
| 432 | | 8 | | 89 |
+-----+ +-----+ +-----+
| 123 | | 8 | | 116 |
+-----+ +-----+ +-----+
| 555 | | 8 | | 203 |
+-----+ +-----+ +-----+
| 432 | | 9 | | 105 |
+-----+ +-----+ +-----+
| 123 | | 9 | | 103 | ** different than yours
+-----+ +-----+ +-----+
| 555 | | 9 | | 187 |
+-----+ +-----+ +-----+

?

My answer is this: I'd not have "parallel arrays". I'd put all
the values in a structure

struct salesdata {
int id;
int month;
int value;
};

and sorted the array of these structs based on some criteria.

Victor
Jul 22 '05 #2
Xiaozhu writes:
say, if you had parallel arrays containing the sales person id, the month, and the sales figures (for that person in that month), sorting on sales figure and preserve the order of figures within the data about the same person:


That's a severe problem with parallel arrays and is one of the many reasons
you need structures.
Jul 22 '05 #3
* Xiaozhu:
say, if you had parallel arrays containing the sales person id, the month, and
the sales figures (for that person in that month), sorting on sales figure and
preserve the order of figures within the data about the same person:

Original Data:
id month sales
+-----+ +-----+ +-----+
| 432 | | 8 | | 89 |
+-----+ +-----+ +-----+
| 123 | | 8 | | 116 |
+-----+ +-----+ +-----+
| 555 | | 8 | | 203 |
+-----+ +-----+ +-----+
| 432 | | 9 | | 105 |
+-----+ +-----+ +-----+
| 123 | | 9 | | 143 |
+-----+ +-----+ +-----+
| 555 | | 9 | | 187 |
+-----+ +-----+ +-----+

Sorted by sales:
id month sales
+-----+ +-----+ +-----+
| 432 | | 8 | | 89 |
+-----+ +-----+ +-----+
| 432 | | 9 | | 105 |
+-----+ +-----+ +-----+
| 123 | | 8 | | 116 |
+-----+ +-----+ +-----+
| 123 | | 9 | | 143 |
+-----+ +-----+ +-----+
| 555 | | 9 | | 187 |
+-----+ +-----+ +-----+
| 555 | | 8 | | 203 |
+-----+ +-----+ +-----+

is that possible to do this in c++? Thanks!


Well the problem has nothing to do with parallell arrays (because you can
always just sort an array of indices to the real arrays), but it has to do
with availability of a _stable_ sort, one which preserves the order of
records with equal keys.

You can implement a stable sort yourself, but I gather the question is
whether the built-in sorting algorithms in the standard library are stable.

list::sort is documented as stable in §23.2.2.4/31, and also <algorithm>
has a stable sort called, suprise!, std::stable_sor t (§25.3.1.2).

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #4
"Xiaozhu" <ge********@gma il.com> wrote in message
news:f8******** *************** ***@posting.goo gle.com...
say, if you had parallel arrays containing the sales person id, the month, and the sales figures (for that person in that month), sorting on sales figure and preserve the order of figures within the data about the same person:

Original Data:
id month sales
+-----+ +-----+ +-----+
| 432 | | 8 | | 89 |
+-----+ +-----+ +-----+
| 123 | | 8 | | 116 |
+-----+ +-----+ +-----+
| 555 | | 8 | | 203 |
+-----+ +-----+ +-----+
| 432 | | 9 | | 105 |
+-----+ +-----+ +-----+
| 123 | | 9 | | 143 |
+-----+ +-----+ +-----+
| 555 | | 9 | | 187 |
+-----+ +-----+ +-----+

Sorted by sales:
id month sales
+-----+ +-----+ +-----+
| 432 | | 8 | | 89 |
+-----+ +-----+ +-----+
| 432 | | 9 | | 105 |
+-----+ +-----+ +-----+
| 123 | | 8 | | 116 |
+-----+ +-----+ +-----+
| 123 | | 9 | | 143 |
+-----+ +-----+ +-----+
| 555 | | 9 | | 187 |
+-----+ +-----+ +-----+
| 555 | | 8 | | 203 |
+-----+ +-----+ +-----+

is that possible to do this in c++? Thanks!


Perhaps I'm missing something, but it seems to me a linked list of pointers
would do what you want quite easily. Something like:

struct StrSales {
StrSales *next;
int *id;
int *month;
int *sales;
};

This can then be made into a list that points to the original arrays so they
are not changed, but you can sort them anyway you wish by stepping through
the linked list.

I wonder if I explained that right, but I hope you see the concept.

--
Mabden
Jul 22 '05 #5
Xiaozhu wrote:

say, if you had parallel arrays containing the sales person id, the month, and
the sales figures (for that person in that month), sorting on sales figure and
preserve the order of figures within the data about the same person:

Original Data:
id month sales
+-----+ +-----+ +-----+
| 432 | | 8 | | 89 |
+-----+ +-----+ +-----+
| 123 | | 8 | | 116 |
+-----+ +-----+ +-----+
| 555 | | 8 | | 203 |
+-----+ +-----+ +-----+
| 432 | | 9 | | 105 |
+-----+ +-----+ +-----+
| 123 | | 9 | | 143 |
+-----+ +-----+ +-----+
| 555 | | 9 | | 187 |
+-----+ +-----+ +-----+

Sorted by sales:
id month sales
+-----+ +-----+ +-----+
| 432 | | 8 | | 89 |
+-----+ +-----+ +-----+
| 432 | | 9 | | 105 |
+-----+ +-----+ +-----+
| 123 | | 8 | | 116 |
+-----+ +-----+ +-----+
| 123 | | 9 | | 143 |
+-----+ +-----+ +-----+
| 555 | | 9 | | 187 |
+-----+ +-----+ +-----+
| 555 | | 8 | | 203 |
+-----+ +-----+ +-----+

is that possible to do this in c++? Thanks!


Sure.
It's not that hard, if you write the sorting function
on your own.
Every sort algorithm needs to do 2 things:
compare array elements
eventually swap array elements

The first one is easy: Just code in on what array wou
want the comparison be based.
The second one is easy to: Instead of just swapping
the entries of a single array, you swap the elements
of all the parallel arrays at the same index positions:

....
temp = sales[i];
sales[i] = sales[j];
sales[j] = temp;

temp = month[i];
month[i] = month[j];
month[j] = temp;

temp = id[i];
id[i] = id[j];
id[j] = temp;
....

The idea is: Whatever you do to one array, you do to all
of the parallel arrays. This way they stay always in sync.
*But* - The bigger question is:
Why do you have parallel arrays in the first place? You
should have a struct which models a single entry consisting
of id, month and sales and then have an array of those.

--
Karl Heinz Buchegger
kb******@gascad .at
Jul 22 '05 #6
On Tuesday 31 August 2004 09:53 am, Xiaozhu did deign to grace us with the
following:
say, if you had parallel arrays containing the sales person id, the month,
and the sales figures (for that person in that month), sorting on sales
figure and preserve the order of figures within the data about the same
person:

Original Data:
id month sales
+-----+ +-----+ +-----+
| 432 | | 8 | | 89 | .... is that possible to do this in c++? Thanks!


I'm only a C++ n00b, but I've copied and pasted my fair share of code ;-),
and the first thing that jumped out at me was "spreadshee t."

My comment would put the thread OT for the NG, but isn't there a facility
to bring in objects from other apps' libraries? Just instantiate some kind
of spreadsheet class. But that's implementation-dependent, so I'll go
flog myself now. ;-)

Cheers!
Rich

Jul 22 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2865
by: James | last post by:
Hi, I have the following code: <PRE> <?php $username = "####t"; $password = "####"; $hostname = "####"; mysql_connect($hostname, $username, $password) or die("Unable to connect to
6
10003
by: Der Andere | last post by:
I have an array of pointers (to a class) which I want to have sorted. I have implemented the < operator for the class but I guess STL sort will sort the pointers according to _their_ values (the addresses). Or will it work as I intended? Do you know ways to circumvent the problem? Thanks, Matthias -- Für emails Anweisung in der Adresse befolgen
12
5309
by: lifeshortlivitup | last post by:
I am trying to construct two, one-dimensional parallel arrays that will give me a letter grade for whatever score I enter. The two arrays I need are minimum score and grade. I don't understand how to build the score array. The scores are as follows: 0 - 299 should result in a F 300 - 349 should result in a D 350 - 399 should result in a C 400 - 449 should result in a B ...
8
5263
by: earla12 | last post by:
Hi, I'm working on a program that takes the following input from the user: Student Name, Student Number, and Student GPA The information is then supposed to be stored in three separate arrays. Prior to sending the output to the screen, a bubble sort has to be performed so that it's sorted by Student Name.
18
3394
by: godofredo | last post by:
Hi everyone. I have 3 arrays of doubles that represent 3D coordinates. The arrays are of 8000 points so I put here a little example. x = { 0.3 , 0.6 , 0.2} y = {2.0 , 0.3 , 0.2} z = {1.0 , 2.0 , 3.0} So x(0),y(0),z(0) is a 3D point x(1),y(1),z(1) is another 3D point and so on. What I would like to do is to sort the 3D points in space from the closest to (0,0,0), so the 3 arrays are simultaneously sorted but maintaining the fact that...
1
5661
by: guest | last post by:
I am doing a program for Intro to Computer Programming where we take an array of strings and we must sort them alphabetically. we are supposed to use a bubble sort, but i know the code if meant for integers. How do i get a bubble sort to put names in alphabetical order?
1
1990
by: joor | last post by:
Hi I am a beginner and currently trying to create a small program. I seem to be stuck on the multiplication of their elements. Eg. I have an Array with 4 different prices for 4 different products and have created 2 other Arrays to which I have assigned values through window.prompt(parseFloat) , what I am trying to do is multiplicate the values on the parallel arrays by their corresponding indexes and keep a running total so I can have an...
3
2979
by: lolcheelol | last post by:
typedef struct record1{ int n1; string name1; char c1; double d1; } set1; int swap2(int &n1, int &n2){ int temp; temp=n1;
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10211
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10045
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9994
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9863
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8870
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6673
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3561
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.