0
#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);
        }
    }
}
3
Contributors
2
Replies
4
Views
4 Years
Discussion Span
Last Post by Gonbe
1

What result are you trying to achieve here?

Votes + Comments
wanted to print a shape like that in row 1 and 3 there are 4 astericks...
1

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..

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.