Structs?

Can someone please explain to me how I can integrate structs into my code? It'll make it much smaller because I don't like it being 200+ lines of code. What my code basically does is it asks you to input a height and a width and the program will make a 2D grid of that and it will find the largest product of 4 numbers. It runs and compiles correctly, I would just like to make it smaller.

structs largest_product_info
{
int HEIGHT;
int WIDTH;
string shape;
}

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
 #include <iostream>
 #include <fstream>
 #include <cstdlib>

 using namespace std;

 #define NUMBER_OF_CONSECUTIVE_DIGITS 4

 int main()
 {
       int index = 0;
       int WIDTH,HEIGHT;

    do{
       cout<<"Enter Width"<<endl;
       cin>>WIDTH;
       if (cin.fail()){
          cin.clear(); cin.ignore(1000, '\n');
          cout << "Please enter a valid integer\n";
          continue;
        }
       if(WIDTH <=3) {
          cout << "Please enter a width > 3\n";
       }
       else
       {
          break;
       }
    }while(1);

    do{
       cout<<"Enter Height"<<endl;
       cin>>HEIGHT;
       if(cin.fail()){
          cin.clear(); cin.ignore(1000,'\n');
          cout << "Please enter a valid integer\n";
          continue;
       }
       if(HEIGHT <= 3){
          cout << "Please enter a height >3\n";
       }
       else
       {
          break;
       }
   }while(1);

      int grid[ WIDTH ][ HEIGHT ];
      double temp = 1;
       int width = 0;
       int height = 0;
       int max_start_index_x = 0;
       int max_start_index_y = 0;
       double max_product = 0;
       int what_type_of_consecutive = 0; // 1 = horizontal, 2 = vertical
       // 3 = diaganol-down-right, 4 = diaganol-up-right

        int i = 0;
        int x = 0;
        int y = 0;
        int j=0;
 //filling with random values less than 100
 for(i=0;i<WIDTH;i++)
 {
    for(j=0;j<HEIGHT;j++)
    {
       grid[ i ][ j ] = rand()%100;
    }
 }
 i=0;
 j=0;

 //Searches from left to right.
 for ( y = 0; y < HEIGHT; y++ )
 {
   for (x = 0; x <= WIDTH - NUMBER_OF_CONSECUTIVE_DIGITS; x++)
    {
       for ( i = 0; i < NUMBER_OF_CONSECUTIVE_DIGITS; i++)
       {
          temp *= grid [x + i ] [ y ];
       }

       if ( temp > max_product )
       {
          max_product = temp;
          max_start_index_x = x;
          max_start_index_y = y;
          what_type_of_consecutive = 1;
       }

       temp = 1;
  }
 }

 temp = 1;

//Searches from top-bottom
  for ( x = 0; x < WIDTH; x++ )
     {
        for ( y = 0; y <= HEIGHT - NUMBER_OF_CONSECUTIVE_DIGITS; y++ ) 
          {
              for ( i = 0; i < NUMBER_OF_CONSECUTIVE_DIGITS; i++ )
                 {
                    temp *= grid[ x ][ y + i ];
                 }

                 if ( temp > max_product )
                   {
                      max_product = temp;
                      max_start_index_x = x;
                      max_start_index_y = y;
                      what_type_of_consecutive = 2;
                   }

                   temp = 1;
           }
     }

    temp = 1;
 //Diagonal Down-right
  for ( x = 0; x <= WIDTH - NUMBER_OF_CONSECUTIVE_DIGITS; x++ )
     {
        for ( y = 0; y <= HEIGHT - NUMBER_OF_CONSECUTIVE_DIGITS; y++ )
           {
              for (i = 0; i < NUMBER_OF_CONSECUTIVE_DIGITS; i++ )
              {
                 temp *= grid[ x + i][ y + i ];
              }

               if ( temp > max_product )
                 {
                    max_product = temp;
                    max_start_index_x = x;
                    max_start_index_y = y;
                    what_type_of_consecutive = 3;
                 }

                 temp = 1;
           }
     }

 temp = 1;
 //Diagonal up-right

 for ( x = 0; x <= WIDTH - NUMBER_OF_CONSECUTIVE_DIGITS; x++ )
     {
        for ( y = HEIGHT - 1; y >= NUMBER_OF_CONSECUTIVE_DIGITS - 1; y-- )
           {
              for ( i = 0; i < NUMBER_OF_CONSECUTIVE_DIGITS; i++ )
                 {
                    temp *= grid[ x + i ][ y - i ];
                 }

                 if ( temp > max_product)
                 {
                    max_product = temp;
                    max_start_index_x = x;
                    max_start_index_y = y;
                    what_type_of_consecutive = 4;
                 }

                 temp = 1;
           }
     }

 if ( what_type_of_consecutive != 0 )
 {
    cout.setf( ios_base::fixed, ios_base::floatfield );
    cout.precision( 0 );

    cout << "\n\nThe greatest product for " << NUMBER_OF_CONSECUTIVE_DIGITS << " consecu    tive digits is: " << max_product << "\n";
    cout << "It starts at point: (" << max_start_index_x + 1 << ", " << max_start_index_    y + 1 << ") and the digits are in ";

    switch( what_type_of_consecutive )
    {
      case 1:
          cout << "horizontal order.\n";
          cout << "The digits are: ";

          for ( i = max_start_index_x; i < max_start_index_x + NUMBER_OF_CONSECUTIVE_DIG    ITS; i++ )
          {
             cout << grid[ i] [ max_start_index_y ] << " ";
          }
          cout << "\n";

          break;

       case 2:
          cout << "vertical order.\n";
          cout << "The digits are: ";

          for ( i = max_start_index_y; i < max_start_index_y + NUMBER_OF_CONSECUTIVE_DIG    ITS; i++ )
          {
             cout << grid[ max_start_index_x ][ i ] << " ";
          }
          cout << "\n";
          break;

       case 3:
          cout << "diagonal-down-right order.\n";
          cout << "The digits are: ";

          for ( i = 0; i < NUMBER_OF_CONSECUTIVE_DIGITS; i++ )
          {
             cout << grid[ max_start_index_x + i ][ max_start_index_y - i ] << " ";
          }
          cout << "\n";
          break;

       case 4:
          cout << "diagonal-up-right order.\n";
          cout << "The digits are: ";

          for ( i = 0; i < NUMBER_OF_CONSECUTIVE_DIGITS; i++)
          {
             cout << grid[ max_start_index_x + i ][ max_start_index_y - i ] << " ";
          }
          cout << "\n";
          break;
    }
 }
 else
 {
    cout << "\nThe grid is not big enough to find a number with " << NUMBER_OF_CONSECUTIVE_DIGITS;
    cout << " consecutive digits.\n";
 }
 cout << "\n\n";
 return 0;
 }

If you really want to make the code shorter, the answer is likely to lie in employing functions, not structs.
We're practicing structs in class so I want to play around with it and practice.
No help in the forums?
Well you finished the struct 50 %.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
structs largest_product_info
{
    int HEIGHT , WIDTH;
    std::string shape;
}

int main()
{
    largest_product_info lpi;
    std::cout << "Height: " << std::flush;
    std::cin >> lpi.HEIGHT;
    std::cout << "Width: " << std::flush;
    std::cin >> lpi.WIDTH;
    std::cout << "Shape: " << std::flush;
    std::cin >> lpi.shape;
}


Also I believe that std::endl; makes the program run slightly faster than '\n' but maybe only a ms and imo std::cout << std::endl; looks better than std::cout << '\n'; or even std::cout << '\n' << std::flush;
Last edited on
Topic archived. No new replies allowed.