mercredi 14 juin 2023

C language pattern decoder

I am trying to develop a pattern decoder using a C language program. The pattern will be like for example:

5 8
1
2
4 
3 
1
1 3 2 1 0 1 2 1

It is a 5x8 board with painted and unpainted dots or squares. The pattern might be represented in a .txt file. The same representation can be seen on the attached image. The expected output is as follows, where represented by a '-' are the unpainted dots or squares and by '#' the painted ones.

Expected pattern

-#------
-##-----
####----
-----###
------#-

Program output

#-------
##------
####----
###---#-
####-###

I tried writing some code but it is not displaying or printing correctly and I have tried debugging with no success.

#include <stdio.h>

#define ROWS 5
#define COLS 8

int main() {
    int row_counts[] = {1, 2, 4, 3, 1};
    int col_counts[] = {1, 3, 2, 1, 0, 1, 2, 1};

    char board[ROWS][COLS];
    int i, j;

    // Initialize the board with unpainted dots
    for (i = 0; i < ROWS; i++) {
        for (j = 0; j < COLS; j++) {
            board[i][j] = '-';
        }
    }

    // Paint the dots based on row and column counts
    for (i = 0; i < ROWS; i++) {
        for (j = 0; j < row_counts[i]; j++) {
            board[i][j] = '#';
        }
    }
    
    for (j = 0; j < COLS; j++) {
        for (i = ROWS - 1; i >= ROWS - col_counts[j]; i--) {
            board[i][j] = '#';
        }
    }

    // Print the decoded pattern
    for (i = 0; i < ROWS; i++) {
        for (j = 0; j < COLS; j++) {
            printf("%c", board[i][j]);
        }
        printf("\n");
    }

    return 0;
}

Aucun commentaire:

Enregistrer un commentaire