Arrays

How to avoid using global variables

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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#include <iostream>

using namespace std;

void bookSeat();

void printAll();

void printBooked(bool val);

int printLeast(char c);

void InitializeSeats();

void printSeat();

void displayMenu()

{

                                                cout << "\n 1. Book Seat" << endl;

                                                cout << " 2. All Seat Status" << endl;

                                                cout << " 3. Empty Seats and Fair" << endl;

                                                cout << " 4. Booked Seats and Fair" << endl;

                                                cout << " 5. Print A Seat" << endl;

                                                cout << " 6. Quit the program" << endl;

                                                cout << "Enter your choice:";

}

int seatFare[30]={-1};
bool isBooked[30]={false};
int main()

{
int choice=0;
bool rough;
InitializeSeats();


while (choice!=6)

{

displayMenu();
cin >> choice;

switch(choice)

{

case 1: bookSeat();

break;

case 2: printAll();

break;

case 3: printBooked(false);

break;

case 4: printBooked(true);

break;

case 5: printSeat();

break;

case 6:

break;

}

}

return 0;

}

bool printAvailable(int x,int y)

{

                int index ;

                bool found=false;

                for (index= x; index < y; ++index)

                {

                                if(isBooked[30]==false)

                {

                                cout <<"#"<<index+1<<"\t"<<seatFare[index]<<endl;

                                found=true;

                }

                }

                return found;

}

void printAll()

{
int seatFare[30]={-1};

                int index=0;

                for (index= 0; index < 30; index++)

                                cout <<"#"<<index+1<<"\t"<<seatFare[index]<<endl;

}

void printBooked(bool val)

{


                int index ;

                for (index= 0; index < 30; index++)

                {

                                if(val==isBooked[index])

                                                cout <<"#"<<index+1<<"\t"<<seatFare[index]<<endl;

                }

}

int printLeast(char c)

{

                int x,y,i,least=-1;

                if (c=='F')

                {

                                x=0;y=10;

                }

                else

                {

                                x=10;y=30;

                }

                int min=-1;

                for (i= x; i < y; ++i)

                {

                                if(false==isBooked[i] && seatFare[x]<min)

                                                least=i;

                }

                return least;

}

void InitializeSeats()

{

                int i=0,diff=200;

                seatFare[0] = 2000;

                for(i=1;i<30;i++)

                {

                  isBooked[i]=false;              

                  if(i==10)              

                  {              

                     seatFare[i]=1400;                           

                       diff = -50;                         

                    continue;                            

                                }

                  seatFare[i]=seatFare[i-1]+diff;              

                }

}

void bookSeat()

{


                int choice,seatno=-1;

                char inp;

                cout << "Welcome to Airline Passenger seat assignment" << endl;

                cout <<"1.First Class"<<endl<<"2.Economy Class"<< endl;

                cin >> choice;

                if(choice==1)

                {

                if(printAvailable(0,10))

                {

                cout << "Please enter the seat number :";

                cin >> seatno;

                if(isBooked[seatno-1]) cout << "Seat already booked";

                else

                {

                isBooked[seatno-1]=true;

                cout << "Booked Seat : "<<seatno<<endl;

                cout << "class : First Class"<<endl;

                cout << "Fare is : " << seatFare[seatno-1]<<endl;


}

                }

                                else

                {

                  seatno=printLeast('E');

                  if( seatno!= -1)

                {

                  cout << "Would you like to Book This Instead (y/n)::";

                 cin >> inp;

                  if(inp=='y')

                {



                isBooked[seatno-1]=true;

                cout << "Booked Seat :: "<<seatno<<endl;

                cout << "class :: First Class"<<endl;

                cout << "Fare :: " << seatFare[seatno-1]<<endl;

                  }

                   }

                   }

                }

                else if(choice==2)

                {

                 if(printAvailable(10,30))               

                 {               

                    cout << "Please enter the seat number :";

                      cin >> seatno;
                          
                    if(isBooked[seatno-1]) cout << "Seat already booked";                            
                    else 
                {                                                          

                                               

                   isBooked[seatno-1]=true;                                             

                   cout << "Booked Seat     :: "<<seatno<<endl;                                             

                  cout << "class   :: First Class"<<endl;                                                                                                
                   cout << "Fare :: " << seatFare[seatno-1]<<endl;
                                                                



                                                }

                                }

                                else

                                {

                   seatno=printLeast('F');

                  if( seatno!= -1)

                {

                cout << "Would you like to Book This Instead (y/n)::";

                 cin >> inp;

                 if(inp=='y')

               {



                    isBooked[seatno-1]=true;

                  cout << "Booked Seat     :: "<<seatno<<endl;

                  cout << "class :: Economy Class"<<endl;

                 cout << "Fare :: " << seatFare[seatno-1]<<endl;

                  }

                  }

                  }

                }

}

void printSeat()

{

                int num;

                cout << " Enter Seat Number" << endl;

                cin>>num;

                cout<<"#"<<num<<"\t"<<"Status :"<<isBooked[num-1]<<"\t Fare :"<<seatFare[num-1]<<endl;

}

Last edited on
How to avoid using global variables

Pass what you need as arguments.

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
#include <iostream>
using namespace std;

void bookSeat(int seatFare[], bool isBooked[]);
void printAll(int seatFare[]);
void printBooked(int seatFare[], bool isBooked[], bool val);
int printLeast(char c);
void InitializeSeats(int seatFare[], bool isBooked[]);
void printSeat(int seatFare[], bool isBooked[]);

void displayMenu()
{   cout << "\n 1. Book Seat" << endl;
    cout << " 2. All Seat Status" << endl;
    cout << " 3. Empty Seats and Fair" << endl;
    cout << " 4. Booked Seats and Fair" << endl;
    cout << " 5. Print A Seat" << endl;
    cout << " 6. Quit the program" << endl;
    cout << "Enter your choice:";
}

int main()
{   int seatFare[30]={-1};
    bool isBooked[30]={false};
    int choice=0;

    InitializeSeats(seatFare, isBooked);
    while (choice!=6)
    {   displayMenu();
        cin >> choice;
        switch(choice)
        {
        case 1: bookSeat(seatFare, isBooked);
                break;

        case 2: printAll(seatFare);
                break;

        case 3: printBooked(seatFare, isBooked, false);
                break;

        case 4: printBooked(seatFare, isBooked, true);
                break;

        case 5: printSeat(seatFare, isBooked);
                break;

        case 6: break;
    }
}
    return 0;

}

bool printAvailable(int seatFare[], bool isBooked[], int x,int y)
{   int index ;
    bool found=false;

    for (index= x; index < y; ++index)
    {   if(isBooked[30]==false)
        {   cout <<"#"<<index+1<<"\t"<<seatFare[index]<<endl;
            found=true;
        }
    }
    return found;
}

void printAll(int seatFare[])
{   int index=0;

    for (index= 0; index < 30; index++)
        cout <<"#"<<index+1<<"\t"<<seatFare[index]<<endl;
}

void printBooked(int seatFare[], bool isBooked[], bool val)
{   int index ;

    for (index= 0; index < 30; index++)
    {   if(val==isBooked[index])
            cout <<"#"<<index+1<<"\t"<<seatFare[index]<<endl;
    }
}

int printLeast(int seatFare[], bool isBooked[], char c)
{   int x,y,i,least=-1;

    if (c=='F')
    {   x=0;y=10;
    }
    else
    {   x=10;y=30;
    }

    int min=-1;
    for (i= x; i < y; ++i)
    {   if(false==isBooked[i] && seatFare[x]<min)
            least=i;
    }
    return least;
}

void InitializeSeats(int seatFare[], bool isBooked[])
{   int i=0,diff=200;

    seatFare[0] = 2000;
    for(i=1;i<30;i++)
    {   isBooked[i]=false; 
        if (i==10) 
        {   seatFare[i]=1400; 
            diff = -50; 
            continue; 
        }
        seatFare[i]=seatFare[i-1]+diff; 
    }
}

void bookSeat(int seatFare[], bool isBooked[])
{   int choice,seatno=-1;
    char inp;

    cout << "Welcome to Airline Passenger seat assignment" << endl;
    cout <<"1.First Class"<<endl<<"2.Economy Class"<< endl;
    cin >> choice;

    if(choice==1)
    {   if(printAvailable(seatFare, isBooked, 0,10))
        {   cout << "Please enter the seat number :";
            cin >> seatno;
            if(isBooked[seatno-1]) 
                cout << "Seat already booked";
            else
            {   isBooked[seatno-1]=true;
                cout << "Booked Seat : "<<seatno<<endl;
                cout << "class : First Class"<<endl;
                cout << "Fare is : " << seatFare[seatno-1]<<endl;

            }
        }
        else
        {   seatno=printLeast('E');
            if( seatno!= -1)
            {   cout << "Would you like to Book This Instead (y/n)::";
                cin >> inp;
                if(inp=='y')
                {   isBooked[seatno-1]=true;
                    cout << "Booked Seat :: "<<seatno<<endl;
                    cout << "class :: First Class"<<endl;
                    cout << "Fare :: " << seatFare[seatno-1]<<endl;
                }
            }
        }
    }
    else if(choice==2)
    {   if (printAvailable(seatFare, isBooked, 10,30)) 
        {   cout << "Please enter the seat number :";
            cin >> seatno;
            if(isBooked[seatno-1]) 
                cout << "Seat already booked"; 
            else 
            {   isBooked[seatno-1]=true; 
                cout << "Booked Seat :: "<<seatno<<endl; 
                cout << "class :: First Class"<<endl; 
                cout << "Fare :: " << seatFare[seatno-1]<<endl;
            }
        }
        else
        {   seatno=printLeast('F');
            if( seatno!= -1)
            {   cout << "Would you like to Book This Instead (y/n)::";
                cin >> inp;
                if(inp=='y')
                {   isBooked[seatno-1]=true;
                    cout << "Booked Seat :: "<<seatno<<endl;
                    cout << "class :: Economy Class"<<endl;
                    cout << "Fare :: " << seatFare[seatno-1]<<endl;
                }
            }
        }
    }
}

void printSeat(int seatFare[], bool isBooked[])
{   int num;

    cout << " Enter Seat Number" << endl;
    cin>>num;
    cout<<"#"<<num<<"\t"<<"Status :"<<isBooked[num-1]<<"\t Fare :"<<seatFare[num-1]<<endl;
}


PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
Last edited on
Can you show me how to do it? Just give me an example from my program
I just did. Read the code I posted.
Ok I got it. But unfortunately the program cannot compile. I used CodeBlocks.

undefined reference to ' printLeast(char)'
error: 1d returned 1 exit status
Line 8 should be:
 
int printLeast(int seatFare[], bool isBooked[], char c);


Line 139 should be:
 
seatno=printLeast(seatFare, isBooked, 'E');


Line 166 should be:
 
seatno=printLeast(seatFare, isBooked, 'E');


You should have been able to figure this out.
Thanks
What you're doing is can be very dangerous for your code as for high complexity which can lead to bugs and errors that are hard yo be detected. Unless you're using program as checkmarx I'd recommend taking it in advance.
Topic archived. No new replies allowed.