Super long program, how to optimize?

Hey so I'm doing an assignment (https://imgur.com/a/qykyQ) and I'm done with everything except the sort-by-name part and the search function, but it's already 200+ lines and kinda ugly, there are many parts where the code seems really repetitive.

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
#include <iostream>
#include <string>

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;


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;
    
    
    for (int iX = 0; iX<26;iX++)
    {
        for (int iY=0;iY<50;iY++)
        {
            if(OSeat.sName)
        }
    }


return 0;
}



If you see anything that can be optimized, let me know!

Also, I'm having trouble with the last two parts, if you can tell me which topics I need to learn to do them, that would be greatly appreciated. Thanks!

#define OMGSEATS 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;

:-D
"OMGSEATS" lol funny, but we haven't done the #define preprocessor, so I can't use that, also I dont understand your code at all. Explain pls?

thanks
You must have done the #include preprocessor tho, otherwise nothing would be compiling. So you could make a file with exactly duplicated lines of code, seats.cpp, then #include it in four places and it would be more organized.

Maybe they want you to use functions though. Since you haven't done the #define preprocessor its an open question if they've taught how to pass pointers to variables instead of the variables. If not, guess you're stuck with global variables if you want functions.
multi dimensional constructs are slightly slower than 1d if the compiler can't unroll it for you. They often end up, if nothing else, with an extra loop variable to maintain that is multiple bogus checks, increments, storage, etc. Use a 1-d construct even if logically it is 2-d. The speed gains are small until you have very high amounts of data, though, its probably not worth rewriting for that.

lose the stand-alone day/month/year for a single date variable, cutting assignments and such by a factor of 3?

use \n instead of endl in looped print statements.

can you do less checks in your nested ifs?
eg
if (something and value < firstbucket)
...
else if(something and value < second bucket) //its >= firstbucket here, and less than second
so no need to check a range if reworded?? I didn't check if this is possible or not, but if so...

as for reading it,
anywhere that you repeat the same code more than twice should be a function unless there is a very solid reason not to do that.


eg:

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;
^^ function


finally magic numbers suck. Get those moved out to constants.
eg
for(x = 3; x < 39; x++) // bad
for(x = start; x < somemax; x++) //good

then if you change start or somemax it changes all the loops ... its unsafe to search/replace all the 3s, its unsafe to to it by hand, its just generally bug-prone and troubling to deal with constants in the middle of code.

Last edited on
References can save typing and eye strain with zero cost:
1
2
3
4
5
6
7
8
9
10
void vInitializer(Seat vOSeat[][50])
{
    for (int iX = 0; iX<26;iX++) {
        for (int iY = 0; iY<50;iY++) {
            Seat &seat { vOSeat[iX][iY] };

            seat.cRow = (char)iX+65;
            seat.iColumn = iY+1;
            seat.bAssigned = false;
            ...

Create an operator to display a seat. Then you can call it wherever needed. Notice that I moved the code that prints the price to after the switch statement instead of repeating it each time.
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
std::ostream & operator << (std::ostream &os, const Seat &seat)
{
    os<< seat.cRow<<" "<<seat.iColumn<<" Section ";
    switch (seat.SeatLoc) {
    case 0: os <<"ORCHESTRA";
        break;
    case 1: os <<"FRONT";
        break;
    case 2: os <<"MIDDLE";
        break;
    case 3: os <<"BACK";
        break;
    }
    os << ", Price $" << cdPrice[seat.SeatLoc];
    os<<", "<<seat.Day.iMonth
      <<"/"<<seat.Day.iDay
      <<"/"<<seat.Day.iYear;
    return os;
}


void vDisplay(Seat vOSeat[][50])
{
    for (int iX = 0; iX<26;iX++) {
        for (int iY = 0; iY<50;iY++) {
            std::cout<< vOSeat[iX][iY] << '\n';
        }
    }
}

The big savings come in your main function. Why iterate though each and every seat checking whether it's in the right section when you can determine exactly which rows to search up front? When determining the rows, use data instead of code:
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
    struct PriceAndSeat {
        int price, lowX, highX;
    };
    PriceAndSeat areas[] = {
        { 150, 0, 5 },  // $150 seats are rows 0 through 4.
        { 100, 5, 10 }, // $100 seats are rows 5-9
        { 75, 10, 20 }, // etc.
        { 50, 20, 26 },
        { 0, 0, 0 },
    };
...
        // Find the seating area
        int idx;
        for (idx=0; areas[idx].price; ++idx) {
            if ( iSelectedPrice == areas[idx].price) {
                break;
            }
        }
        if (areas[idx].price == 0) {
            std::cout << "Invalid price. Please try again\n";
        } else {
            bFoundSeat = false;
            std::cout << "Assigning seat for " << sInputName << '\n';
            for (int iX = areas[idx].lowX; iX < areas[idx].highX && !bFoundSeat\
; ++iX) {
                for (int iY = 0; iY<50 && !bFoundSeat; iY++) {
                   ...
        

Perhaps an even better way to store the data is to have a "Section" class. Each section has a name, a price, and starting/ending rows (and columns?). That would make clear what I had to guess: that the prices and row locations are related to the sections.
Topic archived. No new replies allowed.