#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);
        }
    }
}

Dani AI

Generated

Quick diagnosis: three things combine to make the program behave oddly.

  • A syntax error in the posted printf — printf ("%c",a[row][col); is missing a closing parenthesis and won't compile as-is.
  • The test (row==col)||(row!=col) is always true (either equal or not), so it does nothing useful. Replace it with a meaningful condition (for example row == col for a diagonal) or drop it to fill every cell.
  • Using row = row + 2 in both the fill and the print loops touches only rows 0 and 2; the other cells remain uninitialized and printing them is undefined behaviour. Also there is no newline between rows, so the output looks like one long line.

A simple, safe version that fills a 4x4 block with 'x' and prints it (different from the snippets already in the thread) looks like this:

#include <stdio.h>

int main(void)
{
    char a[4][4];
    for (int r = 0; r < 4; ++r)
        for (int c = 0; c < 4; ++c)
            a[r][c] = 'x';

    for (int r = 0; r < 4; ++r) {
        for (int c = 0; c < 4; ++c)
            putchar(a[r][c]);
        putchar('\n');
    }
    return 0;
}

Notes and troubleshooting tips: if the intended result is a diagonal, set a[r][c] = 'x' only when r == c. Always initialize arrays (e.g. char a[4][4] = {{0}}; or with a loop) before printing. Compile with warnings enabled (gcc -std=c11 -Wall -Wextra -pedantic) to catch mistakes and use tools like Valgrind to detect use of uninitialized memory. As asked, decide whether the goal is a full grid or a subset; correctly pointed out the tautology in the condition and the row-increment issue — fixing those three points will make the output predictable.

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.