i want an output and i have tried but can't get this...

#include <cstdlib>
#include <iostream>
#include <iomanip>
using namespace std;

int main(int argc, char *argv[])
{
    int r,c,s;
    for(s=15,r=6;r>=1;r--,s++)
    {
                              cout<<setw(s);
                              for(c=1;c<=r;c++)
                              cout<<"X"<<endl;
                              }

    system("PAUSE");
    return EXIT_SUCCESS;
}

OUTPUT:     X X X X X X
             X X X X X
              X X X X
               X X X
                X X
                 X

above is the code and its output tell me where i am wrong and how to correct it...also tell me the corrected code and how these loops are controlling X ?????

Here, this is an example of your problem, but reversed, from top to bottom:

#include <iostream>
using namespace std;
#define SIZE 12
int main(){
    for (int i=0;i<SIZE/2;i++){
        for (int j=0;j<SIZE;j++){
            cout<<" ";
            if (j >= (SIZE/2-i)-1 && (SIZE/2-1) >= j) cout<<"X";
            else if (j > SIZE/2) break;
        }
        cout<<endl;
    }
    return 0;
}

Having as output this:

      X 
     X X 
    X X X 
   X X X X 
  X X X X X 
 X X X X X X 

Now let me explain a bit this code, so that you can understand the problem, and how to actually do yours:
For this example, I had to start with 1 X and finish with a line full of X's. How can you do it than? Well, you'll have to think a bit how you want to represent it. You know that on each row, you'll need a specific number of X's, number which is related to the actual number of the rows. So, for example, when the row number is 3, you'll have to have 4 X's (provided that you start counting the rows from 0, and not from 1).

So, let's get crackin':
As you can see, I have a generic MACRO SIZE which can be equal to whatever number you want. Since we'll be using integer division, numbers like 14 and 15 will yield the same result (since 14/2 = 7 and 15/2 = 7 - note, integer division).
So, I start this with a for loop, with i from 0 to SIZE/2. This is the row count I was talking about earlier. Having the row count, you'll need to know where to actually put the X. You can think of it as to a matrix, where i's are the rows, and j's are the columns. So, after the i loop started, we need to know the "column" position where to place the X, so we start with a for loop with j taking values between 0 and SIZE. After each i iteration we need to print a new line. Ok, but where exactly do we need to put the X's?
Well, you can think of a smart formula, like this:
you'll need to place the X's starting from SIZE/2-i and you need to place as many as the row number indicates (the i), that is, if you're on the 3rd row, e.g. i=3, and SIZE is 12, by the simple formula we get that:
12/2 - 3 = 3
So, starting from the 3rd "column" position (j == SIZE/2-1 == 3), we need to print the X's. Note that when we're done printing the X's we break out of the column loop (j loop), and we get on to the next row. Also, when we print the X's, we prin a whitespace too, so that it would look nice.

This is the basic principle. Having this code, your duty is to actually do the reverse, meaning you have to start from bottom to top (speaking from a triangle point of view).
Good luck.

Edited 3 Years Ago by Lucaci Andrew

Comments
good explanation

@mr.unknown: your code will never produce the output you gave. Remove the endl on line 13 an place it elsewhere.

hmm tanx all guys i was just confused about "for loops" for rows and columns control.....later when i make some changes to my programm like shifting endl to next line and correct use of curly braces i got my output but i still not get the point that how 2 loops control the rows and colums one by one.....

Think of it like this:

#include <iostream>
using namespace std;

int main(){
    for (int i=0;i<7;i++){
        for (int j=0;j<7;j++)
            cout<<char(int('0')+j)<<" ";
        cout<<endl;
    }
    return 0;
}

which outputs this:

0 1 2 3 4 5 6 
0 1 2 3 4 5 6 
0 1 2 3 4 5 6 
0 1 2 3 4 5 6 
0 1 2 3 4 5 6 
0 1 2 3 4 5 6 
0 1 2 3 4 5 6 

This is your matrix formed by fors. Now if you search for each for you'll see that i's point to the rows of the matrix, and j's to the columns:

i/j 0|1|2|3|4|5|6
 0  0 1 2 3 4 5 6 
 1  0 1 2 3 4 5 6 
 2  0 1 2 3 4 5 6 
 3  0 1 2 3 4 5 6 
 4  0 1 2 3 4 5 6 
 5  0 1 2 3 4 5 6 
 6  0 1 2 3 4 5 6 
for (int row = 0; row < max_rows; row++)
{
    for (int col = 0; col < max_cols; col++)
    {
        // do something at row/col
    }
}

This may make the logic more accessible.

This article has been dead for over six months. Start a new discussion instead.