Nested Looping

Jan 17, 2012 at 4:12pm
I'm practicing code (deliberate practice creates expertise) and I'm stuck.
I'm supposed to create a nested loop that outputs:
aa
ab
ac
ad
ae
ba
bb
bc
bd
be
ca
cb
cc
cd
ce...
and so on thru ee.
Try as I might, I can't get this to work. Can someone show me? I understand how to get the typical nested loop rectangle of asterisks, and even a triangle of asterisks. But I can't translate that into a solution to this exercise.

Thanks- if you can pseudocode the logic it would be a great help too- I'm trying to learn to psuedocode as well.


Jan 17, 2012 at 4:40pm
for each character 'a' through 'e'
    print current character
    for each character 'a' through 'e'
        print current character
    print new line
end
Last edited on Jan 17, 2012 at 4:41pm
Jan 17, 2012 at 5:47pm
LB Thanks for the logic solution. Here's my attempt, and the result. '
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int main()
{
	 char one;
	 char two;

	 for(one = 'a'; one<='e';one++)  // for each a thru e
	 {
		  cout<<one;   // print current character
	   for(two='a';two<='e';two++) // for each a thru e
	   {
		cout<<two;  // print current character
	   }
		 cout<<endl;      // print new line
	 }


My result is
aabcde
babcde
cabcde
dabcde
eabcde

Understanding this will really help me understand what's going on in a nested loop. Thanks for your help!
Jan 17, 2012 at 10:51pm
Oops, sorry - I wrote that quite quickly. The cout<<one and cout<<endl should be in the inner loop, but still in the same order.
for each character 'a' through 'e'
    for each character 'a' through 'e'
        print current character (outer loop)
        print current character (inner loop)
        print new line
end
Last edited on Jan 17, 2012 at 10:52pm
Jan 18, 2012 at 2:37am
Ha! Thanks for checking back for me. I'll play with it to make sure I understand it. I was close, so close!

Wayne
Topic archived. No new replies allowed.