Parallel Arrays

Hi;
If you use parallel arrays to input an address for a phone directory program. Such as this
Walter J. Freeman
8752 W. Miles Rd.
Adrian Mi, 48719
517-423-7989
Then at a later time in the program you have to display the entire directory sorted by last name. How would you go about doing this? I'm so confused. How do I keep the arrays together and what would be the best way to input this info? At a later time I have also have to be able to modify the first name last name and such. So should the first name go into it's own array the last into its own?
The big thing is how would any information be in order if I have to display the info sorted by last name?
I'm really in a bind with this.
Thanks
How do I keep the arrays together

You do that by having only a single array of appropriate address objects.
Can you please explain sir?
Thanks
If you got this task, surely you already covered structures/classes? You should declare your own Address class and then make an array of them.
OK I will work through and try to do this. We talked of structures one day then we used them in one example. Any ideas how to input data if I use the structure?
If you use parallel arrays to input an address for a phone directory program... Then at a later time in the program you have to display the entire directory sorted by last name. How would you go about doing this?


If you know how to sort an array, you're pretty much 95% of the way there.

All you need to remember is that when you swap elements in one array, you must also swap the same elements in all of the parallel arrays.

If you're feeling spunky, you could introduce an array of indices and sort that array based on the names those indices refer to in the parallel arrays. The result of that could be used to traverse the parallel arrays in order of the names, without ever having to modify the parallel arrays.
Oh can you please elaborate cire? I have to sort the infor at one point or another. I think that I will use a structure and declare an array of that type. Now with that said can you tell me more about your idea?
Thanks,
j
This illustrates the basic idea:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <string>
#include <algorithm>

int main()
{
    std::string words[5] = { "horse", "zebra", "cow", "cornucopia", "ginger" } ;
    unsigned indices[5] = { 0, 1, 2, 3, 4 } ;

    const unsigned size = sizeof(indices) / sizeof(indices[0]) ;

    bool swapMade;

    do
    {
        swapMade = false ;

        for  ( unsigned i = 1;  i < size; ++i )
        {
            if ( words[indices[i]] < words[indices[i-1]] )
            {
                std::swap(indices[i], indices[i-1]) ;
                swapMade = true ;
            }
        }
    } while (swapMade ) ;

    std::cout << "Actual order:\n" ;
    for( unsigned i=0 ;  i < size; ++i )
        std::cout << '\t' << words[i] << '\n' ;

    std::cout << "Sorted order:\n" ;
    for ( unsigned i=0;  i < size; ++i )
        std::cout << '\t' << words[indices[i]] << '\n' ;
}
Thanks dude,
So I have a question I don't know the syntax off my head but what if you had a structure with an array declared of that type. Is it possible to sort that array in alphabetical order according to last name if. ( forgive the syntax) You have the array called info of type telephone( your structure type) and then if you did a sort algorithm with info.last name. Would all the other information fall in line behind that and then the rest would appear with that index. OK not to sound stupid but you know how when geese fly in a v there is a lead and all the rest follow. Is that what would happen if I sorted my array info of type telephone by using info.last name. I hope that makes sense.
I hope that question makes sense.
If you're using a struct:

1
2
3
4
5
6
struct entry 
{
    std::string name ;
    std::string address ;
    std::string phone ;
};


And an array:

entry directory[5] = { {"Smith Fred", "101 Spring Lane", "555-555-0000" }, /* ... */ } ;

Whenever you swap one element for another, you swap the entire struct. Every address/phone stays with it's associated name, if that's what you're asking.

If your assignment calls for parallel arrays though, that's probably not going to fly. On the other hand, if it doesn't, it will simplify the code.
Dude It says parallel arrays I emailed my instructor and he said structures. So I'm going with structures, It will be much easier I believe. That's exactly what I was asking. I believe. So if I used last name in order to sort then everything will stay together and the last names will be sorted alphabetically?
Topic archived. No new replies allowed.