Nested For Loop Pattern
Mar 15, 2016 at 6:32am UTC
I've searched and have only found diamond patterns or box patterns with an asterisk.
I need to make the following pattern below with a nested for loop. The pattern will grow in size depending on the user input. This is an example of an input of three:
+===+===+
| | |
| | |
| | |
+===+===+
| | |
| | |
| | |
+===+===+
Here is what I have so far:
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
#include <iostream>
using namespace std;
int main()
{
int windowSize;
cout << "Enter the window size: " ;
cin >> windowSize;
for (int column = 1; column <= windowSize; column++)
{
for (int row = 1; row <= windowSize; row++)
{
if (row == 1 || row == (windowSize - 1) * 3 + windowSize)
cout << "+===" <<endl;
else
cout << "| " <<endl;
}
}
cout<<endl;
system("pause" );
return 0;
}
And my result:
Enter the window size: 3
+===
|
|
+===
|
|
+===
|
|
Press any key to continue . . .
Last edited on Mar 15, 2016 at 6:39am UTC
Mar 15, 2016 at 8:05am UTC
What does the input affect? The number of = and |?
are these input 0 and 1?
+++
+++
+++
+=+=+
| | |
+=+=+
| | |
+=+=+
Are you limited on the number of loops that you can have?
Mar 15, 2016 at 2:11pm UTC
Not sure if this is the simplest possible way but I hope it helps
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
#include <iostream>
using namespace std;
int main()
{
int windowSize;
cout << "Enter the window size: " ;
cin >> windowSize;
cout<<endl;
cout<<"+" ;
for (int column=1; column<=windowSize; column++)cout<<"=" ;
cout<<"+" ;
for (int column=1; column<=windowSize; column++)cout<<"=" ;
cout<<"+" ;
cout<<endl;
for (int row = 1; row <= windowSize; row++)
{
cout<<"|" ;
for (int column = 1; column <= windowSize; column++)cout<<" " ;
cout<<"|" ;
for (int column = 1; column <= windowSize; column++)cout<<" " ;
cout<<"|" ;
cout<<endl;
}
cout<<"+" ;
for (int column=1; column<=windowSize; column++)cout<<"=" ;
cout<<"+" ;
for (int column=1; column<=windowSize; column++)cout<<"=" ;
cout<<"+" ;
cout<<endl;
for (int row = 1; row <= windowSize; row++)
{
cout<<"|" ;
for (int column = 1; column <= windowSize; column++)cout<<" " ;
cout<<"|" ;
for (int column = 1; column <= windowSize; column++)cout<<" " ;
cout<<"|" ;
cout<<endl;
}
cout<<"+" ;
for (int column=1; column<=windowSize; column++)cout<<"=" ;
cout<<"+" ;
for (int column=1; column<=windowSize; column++)cout<<"=" ;
cout<<"+" ;
cout<<endl;
cin.get();
return 0;
}
Mar 15, 2016 at 3:35pm UTC
I see you've already got a solution. I tried a different approach.
I deviated slightly from the problem, by making the number of window panels a variable acquired from the user (so, the example diagram has 2 sets of panels, each panel is 3 units in size).
Also I used a lot of variables so it's easier to make the different characters used configurable by the user in future.
Finally I store the entire display in a single string, which I print out to standard output at the end of the program.
This makes the nested for-loop obvious and uncluttered. Hope it helps or at least amuses you :-)
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
#include <iostream>
#include <limits>
using namespace std;
int main()
{
size_t dim = 0; // dimension
size_t pan = 2; // number of panels
char rowType1Symbol = '=' ; // rows type 1 character
char rowType2Symbol = ' ' ; // rows type 2 character
char columnType1Marker = '+' ; // columns type 2 marker
char columnType2Marker = '|' ; // columns type 1 marker
char newLineSymbol = '\n' ; // new line
cout << "Dimensions?> " ;
cin >> dim;
cin.ignore(numeric_limits<streamsize>::max(), '\n' );
cout << "Panels?> " ;
cin >> pan;
cin.ignore(numeric_limits<streamsize>::max(), '\n' );
string rowType1Token = columnType1Marker + string(dim, rowType1Symbol); // row type 1 token
string rowType2Token = columnType2Marker + string(dim, rowType2Symbol); // row type 2 token
string rowType1;
for (size_t i = 0; i < pan; ++i)
{
rowType1 += rowType1Token;
}
rowType1 += columnType1Marker;
rowType1 += newLineSymbol;
string rowType2;
for (size_t i = 0; i < pan; ++i)
{
rowType2 += rowType2Token;
}
rowType2 += columnType2Marker;
rowType2 += newLineSymbol;
string display = rowType1;
/*************** nesting ******************/
/******************************************/
for (size_t i = 0; i < pan; ++i) // -> Outer loop
{
for (size_t j = 0; j < dim; ++j) // -> Inner (nested) loop
{
display += rowType2;
}
display += rowType1;
}
/******************************************/
/************** end of nest ***************/
cout << endl << display << endl;
return (0);
}
Mar 16, 2016 at 2:42am UTC
Thanks s8050 !!! and everyone else. Now that I see one of the possibilities. I can tell that I was overthinking it way too much and trying to be clever. Haha. Cheers!
Mar 16, 2016 at 12:21pm UTC
The problem with "clever" is (as the below probably shows) that it is usually hard to maintain.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include <iostream>
int main()
{
size_t dim;
std::cout << "Enter the window size: " ;
if ( std::cin >> dim && dim < 100 ) {
const auto Doppel = 2*(dim+1) + 1;
auto type = [dim]( size_t pos ) { return 0 == pos % (dim+1); };
for ( size_t row = 0; row < Doppel; ++row ) {
auto fill = type(row) ? "=+" : " |" ;
for ( size_t col = 0; col < Doppel; ++col ) {
std::cout << fill[ type(col) ];
}
std::cout << '\n' ;
}
}
return 0;
}
Topic archived. No new replies allowed.