Multidimensional array sorting

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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// Plokstumoje duoda N tasku pagal Y kordinate reikia surikiuoti

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
     int x;
     int y;
      ifstream padarom("padarom.txt");
      padarom >> x >> y;
       int skaicius[10][10];
       int e = 0;


          for(int i = 0; i <x;i++)
          {
              for(int j = 0; j < y; j++)
              {
                  padarom >> skaicius[i][j];
                   cout << skaicius[i][j] << " ";

              }
               cout << endl;
          }

           for(int i = 1; i < x; i++)
           {
               for(int j = 0; j < y;j++)
               {
                   for(int z = j; z < y; z++)
                   {
                       int laikinas;
                        if(skaicius[i][j] < skaicius[i][z])
                        {
                            laikinas = skaicius[i][j];
                            skaicius[i][j] = skaicius[i][z];
                            skaicius[i][z] = laikinas;
                        }
                   }
               }
           }

            cout << " Surikiuotas " << endl;
             for(int i = 0; i < x; i++)
             {
                 for(int j = 0; j < y; j++)
                 {
                     cout << skaicius[i][j] << " ";
                 }
                  cout << endl;
             }

    return 0;

}


So I will give you an example of what do i need to do:
Lets say we have a text file with input

Tom 15
Andrew 18
John 4

I want to sort it by names so it would be
Andrew
John
Tom

and I want the number which would mean years in this instance to follow them so that the output would be

Andrew 18
John 4
Tom 15

How can I do that?
You are better off using two 1-dimensional arrays (parallel arrays), rather than a single 2-dimensional array.

Read in the names in one array, and the ages in the other.

Use bubble sort to sort by name. Every time you swap element in the name array, swap the same indices in the age array.

So something like this:

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
int main()
{
    SIZE = however many names there are in the file
    int names[SIZE], ages[size]; // create 2 separate arrays of same size
    
    for(int i = 0 to SIZE-1)
    {
        file >> names[i] >> ages[i]; // put name in name array, and age in age array
    }
    
    for(i = 0 to SIZE-1)
    {
        for(j = 0 to SIZE-2)
        {
            if( name[j] > name[j+1] ) // if names out of order
            {
                swap(names[j], name[j+1]); // swap the names
                swap(ages[j], ages[j+1]); // swap their corresponding ages
            }
        }
    }
    
    return 0;

}


Obviously, above is not exactly real c++, just something I put together quickly.
Last edited on
closed account (iw7Gy60M)
If you don't have to use arrays, another option would be to use an std::map<std::string, int>. For each line you will read the name into a string variable and the age into an integer variable, then construct a pair from the name and age and insert it into the map. Here's a simple example using console input:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <string>

int main()
{
   string inputName;
   int inputAge;
   std::map<string, int> myMap;

   for(int i = 0; i < 5; ++i)
   {
      cout << "Enter name: ";
      cin >> inputName;
      cout << "Enter age: ";
      cin >> inputAge;
      cout << endl;

      myMap.insert(std::make_pair(inputName, inputAge));
   }

   return 0;
}


You can adapt the above to read from your text file instead of the console. The nice thing about a map is that it does the sorting for you.
Last edited on
Thank you both, it did work and im glad i asked for help :)
Topic archived. No new replies allowed.