nested for loops simple question

Write a program to display following output by using nested for loop

A9 A8 A7 A6 A5 
B9 B8 B7 B6 B5 
C9 C8 C7 C6 C5 
D9 D8 D7 D6 D5 
E9 E8 E7 E6 E5 


Last edited on
I dont see a question anywhere.
I means how to display the output like above??
Break down the program and make small tests out of it. Let's think of some tests you can apply to this...

1) Print out "A9". Write a program to do that, compile it, and run it.
2) Print out the first row. Write a program to do that, compile it, run it, and make sure it looks right. For this, you will need something to store that information (perhaps a string array?)
3) Print out two rows. In order to do this, your string array now has to have both a row and column... two dimensions. So you will first output something like array[0][0], array[0][1], array[0][2]... etc. for the first row, then when you get to the second row, array[1][0], array[1][1], etc. For this go around, don't worry about loops and just output them all by typing in the array values over and over. Compile your program, run it, and make sure it displays the right things.
4) Now make it display all 5 rows. You should be able to notice a pattern in what you did in the 3rd step. Use loop variables (such as array[row][col]) to display the information. Compile and run your program.

You can see more information on how to break things into steps and help getting unstuck on a blog post I recently wrote: http://www.mycpptutor.com/blog/2015/03/31/how-to-get-unstuck-on-your-programming-assignment/

Good luck!
Here you go....
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
char ch='A';

int num=9,i,j;

for(i=0;i<5;i++)
	{
	num=9;  //if you don't do this then num will keep on decreasng
		for(j=0;j<5;j++)
		{
		cout<<ch<<num<<" ";
		num--;        //decrementing the num value
		}           //end of column
	cout<<endl;
	ch++;
	}     //this is the simplest I could make you could also use arrays

Topic archived. No new replies allowed.