### Nested Loop Pattern

Guy,i need help with generating following nested loop pattern.

*^*^*^*^*^*^
*^*^*^*^*^
*^*^*^*^
*^*^*^
*^*^

 ``123456789101112131415161718`` ``````#include using namespace std; int main()//currently stucked. { for(int a=6;a>=1;a--) { for(int b=a*2;b>=1;b--) { cout<<"*"; } cout<
Last edited on
closed account (3qX21hU5)
Ok lets look at it this way.

*^*^*^*^*^*^ <---- 1st time through your first for loop
*^*^*^*^*^ <---- 2nd time through your first for loop
*^*^*^*^ <---- 3rd time through your first for loop
*^*^*^ ect. ect.
*^*^

^^
| |
| Second pass through your second for loop
|
1st pass through your second for loop

So we are basically generating the "rows" with the first for loop, and the "columns" with the second for loop. So we need to look in the second loop to alternate between '*' and '^'.

Now what would happen if you added the '^' symbol into the cout statement after the '*' symbol?

Test that out and then I think you should be able to figure out the rest :)
 ``123456789101112131415161718`` ``````#include using namespace std; int main() { for(int a = 6; a >= 2; a--) { for(int b = 0; b < a; b++) { cout << "*^"; } cout << endl; } return 0; }``````
closed account (3qX21hU5)
Please don't just give the person the answers it teaches them absolutely nothing and doesn't help them learn what was the problem. Specially when you don't even explain what you did and just post code and that is it.
Last edited on
Contrary to what Zereo said, don't alternate between * and ^. Instead, treat them as pairs.
closed account (3qX21hU5)
Josue Molina wrote:
Contrary to what Zereo said, don't alternate between * and ^. Instead, treat them as pairs.

And where did I say that? The pattern is alternating between '*' and '^' isn't it? And also if you check the line right below where I said
 So we need to look in the second loop to alternate between '*' and '^'.
you will see this
 Now what would happen if you added the '^' symbol into the cout statement after the '*' symbol?
;)
Last edited on
Quick trying to pick a fight.

Edit: recursion is fun, too:

 ``12345678910111213141516171819`` ``````#include using namespace std; void foo(string s) { if (s.length() <= 12) { foo(s + "*^"); cout << s << endl; } } int main() { foo("*^*^"); return 0; }``````
Last edited on
 ``123456789101112131415161718192021222324`` ``````// Note that the pattern is assumed to have 2 characters // as in it points to a char [2] and they've been initialised void patternAsc(int n, char *pattern) { for (int i = 1; i < n; ++i) { for (int j = 0; j < i; ++j) { cout << pattern[0] << pattern[1]; } cout << endl; } } int main(void) { char ch[2] {'*', '^',}; patternAsc(10, ch); cout << endl; return 0; }``````

You can use that to figure out how you want it exactly...so not really giving away the whole answer..
You can include another function parameter to indicate the size of the pattern and then can add more patterns in the main function.
Last edited on
closed account (3qX21hU5)
Really am not trying to pick a fight it just annoys me when people just post the answer to a problem without even letting the person who asked the question figure it out on their own.

I was also just pointing out that this statement
 Contrary to what Zereo said, don't alternate between * and ^. Instead, treat them as pairs.
was false and that was not what I was suggesting.

So again not trying to pick a fight with you and sorry if you thought I was just letting you know in the future I would suggest against just posting the answer to questions on the forum because it teaches the person absolutely nothing and just hurts them in the long run.
This is a C++ forum, not an online academy.

I am not responsible for an adult's education in a public forum; I am here to provide solutions. If a college student is not able to peruse my solutions, then they should go back to their book, instructor, TA or tutor.

Moreover, take notice of the poster's question quality.
Last edited on
thx for all the replies.

but i was thinking of replace every odd no with "^".Not sure if it is right to do so.

however it juz doubled the counts.

 ``12345678910`` ``````for(int a=6;a>=2;a--) { for(int b=a*2;b>=1;b--) { cout<<"*"; if(b%2==1) cout<<"^"; } cout<
Move line 5 under the if, after an else.

Like so:

 ``1234`` ``````if (b % 2 == 1) cout << "^"; else cout << "*";``````
Last edited on
Thanks,i finally get how the thing works...

well,previously i was sticking to only one type of symbol in a nested loop...but seen some with more than 1 symbol..so juz trying em ;-)

googled some,but they were suggessting the 3rd loop which is not really understandable by me...
 ``12345`` ``````if(b % 2){ cout << "*"; break; } cout << "^";``````

Many ways to skin a cat. Some of them even involve not having to test for 1, which is a logical error anyways, since true is actually defined as not 0.
Topic archived. No new replies allowed.