Difficulty implementing chess board

Asked

Viewed 72 times

1

I need to implement the code that manages a chess board in ppm. It would be in the standard 8x8 format, with the option of the user to set the pixel number, the doubt would be when to loop to "multiply" the pixels. In the code below is coming out in the standard 8x8:

#include <stdio.h>

int main()
{
    int larg, alt;
    larg = alt = 8;

    printf("P2\n");
    printf("%d\t%d\n", larg, alt);
    printf("255\n");

    int i, j;
    int tamanho = 2;

    for(i = 0; i < larg; i++){
        for( j = 0; j < alt; j++){
            if((i + j) %2 != 0){
                    printf("0\t");
            }
            else{
                    printf("255\t");
            }
        }
        printf("\n");
    }
    return 0;
}

I hope that has been made clear, and I apologise in advance for any possible mistake, as this is my first question.

  • "a chessboard in ppm" - What does that mean ppm ? And what is the particular difficulty you are having in concrete ? What part is not being done/ failed to do ?

1 answer

0


First I think you’re drawing the board starting with the columns, not the lines. But considering the board is square, it wouldn’t make a difference...

for(i = 0; i < larg; i++){
    for(k = 0; k < tamanho; k++){
        for( j = 0; j < alt; j++){
            for(l = 0; l < tamanho; l++){
                if((i + j) %2 != 0){
                        printf("0\t");
                }
                else{
                        printf("255\t");
                }
            }
        }
        printf("\n");
    }
}

I believe this should work.

  • 1

    It worked, yes! Thank you very much.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.