I want to print the following using for loop.
from the pattern, it's clear that when row and column is equal to each other it prints A
that gives me A from top left to bottom right
need clues

A   -   A
 A  -  A
  A - A
   A-A
----A----
   A-A
  A - A
 A  -  A
A   -   A

Count the A's, SPACEs and -'s in each line, and in each section of each line.
Look for numerical patterns.
When you see the patterns, write down some calculations that you can turn into code.
Use lots of paper, and analyze analyze analyze. Plan plan plan.

You'll need a few loops for each line.

Also, whatever algorithm you use to generate lines 1 - 4 you may be able to use for lines 6 - 9 by just reversing the "direction".

You'll be needing 2 nested loops, each one to 9. If you analyze a bit the pattern you can see that it's a square with i=9 and j=9:

i/j0 1 2 3 4 5 6 7 8
 0 A       -       A
 1   A     -     A
 2     A   -   A
 3       A - A
 4 - - - - A - - - -
 5       A - A       
 6     A   -   A
 7   A     -     A
 8 A       -       A

So i's are the columns and j's are the rows.
Notice that:
1. "A"'s are printed when i's and j's are the same.
2. "A"'s are printed when i's added by j's are equal to 8 (i+j=8);
3. The new line character '\n' or endl is printed when j=8
4. The dashes '-' are printed when i's or j's are 4, but if i=4 and j=4 at the same time, the character must be an "A" not a dash.
5. If the conditions above aren't fullfilled the space " " character will be printed.

So knowing that, how could you combine those conditions so that you get the actual pattern?
Well you'll need the "A" character to be on top of all the other characters presented in the patter (see note 4), so after putting those two nested loops, the 1st if contition would be to print the "A" character. But when it should print it? Well have a look at note 1 and 2. So you'll need to put a condition when i=j ||(or) i+j=8.
The second else if condition from these if clauses would be to print the new line (\n). But when is it good to print it? Well, accordingly to note 3, it should be printed when j=8, meaning when reaching with the loop at the end of each row. But if we're using an if-else if-else clause system, only one clause will be ran accordingly to what condition is fulfilled. So knowing that our first condition prints the character "A" each time when i=j or i+j=8, when i is 0 and j=8, the condition is true (exactly the first time), so it will go throughout it but, the 2nd condition (when j=8 it prints that new line) is not taken into consideration given the fact that the 1st condition was fulfilled. So, going back to our 1st condition we'll need to add there another if after the printing of "A" in which we would specify if j=8 to print the new line.
Ok, so basically what we've done so far is to print the A's when i=j or i+j=8 and to print the new line. So far the output should be like this:

i/j0 1 2 3
 0 A A
 1 A A
 2 A A
 3 A A
 4 A 
 5 A A
 6 A A
 7 A A
 8 A A

That's not really good, because we don't have any spaces between letters or dashes, so we'll need to add some other clauses.
Starting with the dash: by note 4 we'll need to print the dash when i is 4 or j is 4, but if i=j=4 we'll need to have an A. This is easily fix by putting this condition after the one which prints the "A" character. So this condition will be the second one, and after this the one with the new line. But the same principle from the previous clause applies, so we'll need to put again, after the printing of the dash statement, if j=8 to print the new line.
Ok, so far we have inside those 2 nested loops 3 conditions:

if i=j || i+j=8 then
    print "A"
    if j=8 then
        print '\n'
    end_if
else if i=4 || j=4 then
    print "-"
    if j=8 then
        print '\n'
    end_if
else if j=8 then
    print '\n'
end_if

This is starting to look good, but we'll have to add another thing: spaces. By note 5 we see if that the conditions above aren't fulfilled we should print a space character " ". So, this easily put by adding the final statement, the else to the if ones, as this: else print '\n'. So, our if statements should look like this:

if i=j || i+j=8 then
    print "A"
    if j=8 then
        print '\n'
    end_if
else if i=4 || j=4 then
    print "-"
    if j=8 then
        print '\n'
    end_if
else if j=8 then
    print '\n'
else 
    print '\n'
end_if

This is wrote in pseudocode, so you'll need to implement it into your code. Don't forget to add this inside of two nested loops.
If you have further questions, don't hesitate to post them.
PS. This can be done in multiple ways, this is just one of them.

Me:
Count the A's, SPACEs and -'s in each line, and in each section of each line.
Look for numerical patterns.
When you see the patterns, write down some calculations that you can turn into code.
Use lots of paper, and analyze analyze analyze. Plan plan plan.

You'll need a few loops for each line.

OK, I guess you don't have to do any of this. Just convert Lucaci's pseudo code into real code. He did the hard work for you.

Be sure to thank him.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.