#include <stdio.h>
main()
{ char a[4][4];
int row,col;
    for (row=0;row<4;row=row+2)
    {
        for (col=0;col<4;col++)
        {
            if ((row==col)||(row!=col))
            {
            a[row][col]='x';
            }
        }
    }
    for (row=0;row<4;row=row+2)
    {
        for (col=0;col<4;col++)
        {
        printf ("%c",a[row][col);
        }
    }
}

Recommended Answers

All 2 Replies

What result are you trying to achieve here?

commented: wanted to print a shape like that in row 1 and 3 there are 4 astericks... +0

Hmm vague code is vague. I made a quick adjustment based on what i THINK you're trying to do here:

#include <stdio.h>
main()
{
    char a[4][4];
    int row,col;

    // Note that the body of this loop is executed only twice.
    // Is this what you want?
    for (row=0;row<4;row=row+2)
    {
        for (col=0;col<4;col++)
        {
            // Note that this condition is always true.
            // You're basically saying "something it equal to something else, or it isn't!"
            if ((row==col)||(row!=col))
            {
                //row index 0 and 2 get 'x'.
                a[row][col]='x';
            }
        }
    }

    // I assume you want to print the two rows you entered data for here.
    // (Why not do it directly, you wouldn't need the array? I assume you want
    //  to develop this further into something else..)
    // I modified the increment section. It goes through EVERY row now so not only the 2
    // you entered data for.
    for (row=0;row<4;row++)
    {
        // You only entered data for row 0 and 2.
        // So skipping the contents of the others now (while still printing the newline)
        if (row % 2 == 0)
        {
           for (col=0;col<4;col++)
            {
                printf ("%c",a[row][col]);
            }
        }

        // Note I added a newline as I THINK you want each row in the 2D array on it's own line.
        printf("\n");
    }
}

This would print the following:

xxxx

xxxx

If this is not what you want, provide more information..

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.