How to sort a two dimensional array's string which is a part of a structure?

Hey so this is kind of a beginner question, sorry for posting here, but I wasn't able to get an answer there, so I'm posting here. Hope that's okay.

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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
 
#include <iostream>
#include <string>
#include <algorithm>

const double cdPrice[4] ={150,100,75,50};
char cBuy = 'y';
int iSelectedPrice;
std::string sInputName;
int iSelectDay;
int iSelectMonth;
int iSelectYear;
char cTemp;
bool bFoundSeat = false;
int iTotalPay = 0;
bool bFoundName = false;


enum Section
{
    ORCHESTRA,
    FRONT,
    MIDDLE,
    BACK,
};

struct Date
{
    int iDay;
    int iMonth;
    int iYear;
};


struct Seat
{
    char cRow = 'n';
    int iColumn;
    Section SeatLoc;
    bool bAssigned;
    std::string sName;
    Date Day;
};


void vInitializer(Seat vOSeat[][50])
{
    for (int iX = 0; iX<26;iX++)
    {
        for (int iY = 0; iY<50;iY++)
        {
            vOSeat[iX][iY].cRow = (char)iX+65;
            vOSeat[iX][iY].iColumn = iY+1;
            vOSeat[iX][iY].bAssigned = false;
            vOSeat[iX][iY].sName = "";
            vOSeat[iX][iY].Day.iDay = 1;
            vOSeat[iX][iY].Day.iMonth = 1;
            vOSeat[iX][iY].Day.iYear = 1900;
            if (vOSeat[iX][iY].cRow <= 'E')
                vOSeat[iX][iY].SeatLoc = ORCHESTRA;
            else if (vOSeat[iX][iY].cRow <= 'J')
                vOSeat[iX][iY].SeatLoc = FRONT;
            else if (vOSeat[iX][iY].cRow <= 'T')
                vOSeat[iX][iY].SeatLoc = MIDDLE;
            else
                vOSeat[iX][iY].SeatLoc = BACK;

        }
    }
}

void vDisplay(Seat vOSeat[][50])
{
     for (int iX = 0; iX<26;iX++)
    {
        for (int iY = 0; iY<50;iY++)
        {
            std::cout<< vOSeat[iX][iY].cRow<<" "<<vOSeat[iX][iY].iColumn<<" Section ";
            switch (vOSeat[iX][iY].SeatLoc)
            {
                case 0: std::cout <<"ORCHESTRA, Price $"<<cdPrice[0];
                        break;
                case 1: std::cout <<"FRONT, Price $"<<cdPrice[1];
                break;
                case 2: std::cout <<"MIDDLE, Price $"<<cdPrice[2];
                break;
                case 3: std::cout <<"BACK, Price $" <<cdPrice[3];
                break;
            }
            std::cout<<", "<<vOSeat[iX][iY].Day.iMonth<<"/"<<vOSeat[iX][iY].Day.iDay<<"/"<<vOSeat[iX][iY].Day.iYear<<std::endl;

        }
    }

}




int main()
{
    Seat OSeat[26][50];
    vInitializer(OSeat);
    vDisplay(OSeat);

    while (cBuy == 'y')
    {
        std::cout<<"Enter seat price"<<std::endl;
        std::cin>>iSelectedPrice;
        iTotalPay += iSelectedPrice;
        std::cout <<"Enter customer name"<<std::endl;
        std::cin.ignore();
        std::getline(std::cin,sInputName);
        std::cout<<"Enter date MM/DD/YYYY"<<std::endl;
        std::cin>>iSelectMonth>>cTemp>>iSelectDay>>cTemp>>iSelectYear;
        for (int iX = 0; iX<26;iX++)
        {
            for (int iY = 0; iY<50;iY++)
            {
                if(iSelectedPrice == 150 && iX <5)
                {
                    if(OSeat[iX][iY].sName==" " && bFoundSeat == false)
                    {
                            OSeat[iX][iY].sName = sInputName;
                            OSeat[iX][iY].Day.iDay = iSelectDay;
                            OSeat[iX][iY].Day.iMonth = iSelectMonth;
                            OSeat[iX][iY].Day.iYear = iSelectYear;
                            bFoundSeat = true;
                    }
                }
                if(iSelectedPrice == 100 && iX>4 && iX<10)
                {
                    if(OSeat[iX][iY].sName==" " && bFoundSeat == false)
                    {
                            OSeat[iX][iY].sName = sInputName;
                            OSeat[iX][iY].Day.iDay = iSelectDay;
                            OSeat[iX][iY].Day.iMonth = iSelectMonth;
                            OSeat[iX][iY].Day.iYear = iSelectYear;
                            bFoundSeat = true;
                    }
                }
                if(iSelectedPrice == 75 && iX>9 && iX<20)
                {
                    if(OSeat[iX][iY].sName==" " && bFoundSeat == false)
                    {
                            OSeat[iX][iY].sName = sInputName;
                            OSeat[iX][iY].Day.iDay = iSelectDay;
                            OSeat[iX][iY].Day.iMonth = iSelectMonth;
                            OSeat[iX][iY].Day.iYear = iSelectYear;
                            bFoundSeat = true;
                    }
                }
                if(iSelectedPrice == 50 && iX>19)
                {
                    if(OSeat[iX][iY].sName==" " && bFoundSeat == false)
                    {
                            OSeat[iX][iY].sName = sInputName;
                            OSeat[iX][iY].Day.iDay = iSelectDay;
                            OSeat[iX][iY].Day.iMonth = iSelectMonth;
                            OSeat[iX][iY].Day.iYear = iSelectYear;
                            bFoundSeat = true;
                    }
                }
            }
        }
        bFoundSeat = false;
        std::cout<<"Buy again?"<<std::endl;
        std::cin>>cBuy;
    }

    std::cout<<"assigned seats"<<std::endl;

    for(int iX = 0; iX<26;iX++)
    {
        for(int iY = 0; iY<50;iY++)
        {
            if (OSeat[iX][iY].sName !=" ")
                {std::cout<< OSeat[iX][iY].cRow<<" "<<OSeat[iX][iY].iColumn<<" Section ";
                switch (OSeat[iX][iY].SeatLoc)
                {
                    case 0: std::cout <<"ORCHESTRA, Price $"<<cdPrice[0];
                    break;
                    case 1: std::cout <<"FRONT, Price $"<<cdPrice[1];
                    break;
                    case 2: std::cout <<"MIDDLE, Price $"<<cdPrice[2];
                    break;
                    case 3: std::cout <<"BACK, Price $" <<cdPrice[3];
                    break;
                }
                std::cout<<", "<<OSeat[iX][iY].sName<<", purchase date "<<OSeat[iX][iY].Day.iMonth<<"/"<<OSeat[iX][iY].Day.iDay<<"/"<<OSeat[iX][iY].Day.iYear<<std::endl;
                }
        }
    }
    std::cout<<"Total price is $"<<iTotalPay<<std::endl;


    std::cout<<"Enter name to search for"<<std::endl;
    std::cin.ignore();
    std::getline(std::cin,sInputName);



    for (int iX = 0; iX<26;iX++)
    {
        for (int iY=0;iY<50;iY++)
        {
            if(OSeat[iX][iY].sName == sInputName)
            {
                 std::cout<< "Found at "<<OSeat[iX][iY].cRow<<" "<<OSeat[iX][iY].iColumn<<" Section ";
                 bFoundName = true;
            switch (OSeat[iX][iY].SeatLoc)
            {
                case 0: std::cout <<"ORCHESTRA, Price $"<<cdPrice[0];
                        break;
                case 1: std::cout <<"FRONT, Price $"<<cdPrice[1];
                break;
                case 2: std::cout <<"MIDDLE, Price $"<<cdPrice[2];
                break;
                case 3: std::cout <<"BACK, Price $" <<cdPrice[3];
                break;
            }
            std::cout<<", "<<OSeat[iX][iY].sName<<", Purchase date "<<OSeat[iX][iY].Day.iMonth<<"/"<<OSeat[iX][iY].Day.iDay<<"/"<<OSeat[iX][iY].Day.iYear<<std::endl;

            }
        }
    }

    if (!bFoundName)
        std::cout<<"Name not found!"<<std::endl;




getchar();
return 0;
}



so essentially what I have to do is cout the customers name alphabetically, but what makes it complicated is that they are strings in a class, which itself is an array, and a two dimensional one at that, and that messes it up for some reason.


I'm really dumbfounded, I have no clue how to write this thing. Any help is appreciated, thanks.
Last edited on
I think it's a bad idea to create a second thread for the same topic. It just confuses people who want to deal with it.
I wasn't able to get an answer there

In http://www.cplusplus.com/forum/beginner/234372/ ?

It does seem like you did get answers, but the communication broke down for some reason.
The people who read and answer questions in Beginners are, by and large, the same people who read and answer questions in General. Creating a new thread here isn't going to make it any more likely that you'll get an answer. The only thing it achieves is to waste people's time.

If there's something unsatisfactory about the help you were given in your other thread, the most productive thing you can do is go back to that thread, and explain more clearly what it is you're looking for, that you haven't found yet.
For some reason when I reloaded that page it gave me some kind of error and ask if I wanted a contact form or to return to the main menu (something like that) so I assumed the thread had been deleted.


My mistake.
Weird - I wonder if it's related to the performance problems we've been seeing on this website recently. Some kind of timeout, maybe?
Last edited on
@Thomas this is a really uncommon problem, since it's sorting a string, which is in a struct, which itself is 2d, and the vast majority of strings are empty. I don't imagine too many people are searching google for answers.




this is a really uncommon problem,

Not really.

You have a list of objects. You change the order of the objects by some criteria. That is sorting. It does not matter whether the objects are ints, pointers, people, or continents. The sort is concerned only about two things:
A) How to test whether two objects are in desired order?
B) If they are not, how to make them change places?

Only the A really matters, and it does not care about the type of the list the objects are in.
In the previous thread I showed how you can treat a 2D array of int as a 1D array and sort it with std::sort. The same principle applies to structs, the difference is that you need a compare function for the sort function.
Try this:
1
2
3
4
5
6
7
8
9
10
bool FirstSeatSmaller(const Seat& s1, const Seat& s2)
{
  // TODO return true if Seat s1 should go before Seat s2 otherwise false
}

// in main call this

  // Please note that second parameter of sort need to point 
  // one element behind the last element in the array 
std::sort(&OSeat[0][0], &OSeat[25][50], FirstSeatSmaller); // #include <algorithm> 

Last edited on
One issue is that you still haven't told us - in either thread - what you mean by "sort a 2D array".

- Do you want each "row" sorted independently?
- Do you want the entire thing sorted as if it were a 1D array, but split into rows, so that OSeat[0][0] < OSeat[0][49] < OSeat[1][0] etc ?
- Do you want something else?

Until you clearly and precisely tell us what you want, any help we give you can only be a guess.
Last edited on
Topic archived. No new replies allowed.