@dimaweyder

Как исправить эту ошибку при обходе матрицы?

#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <windows.h>


void gotoxy(int x, int y)
{
	COORD coord;
	coord.X = x;
	coord.Y = y;
	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}

int main()
{
	int i, j, k;
	printf("Input start: ");
	scanf_s("Input start: ");

	for (i = 0; i < 100; i++)
	{
		int x = 1;
		int y = 24;
		if (i % 2 == 0) {
			x += i;
			
			for (j = 0;j <= i;j++) {
				gotoxy(x - j, y - j);
				printf("1");
				Sleep(20);
			}
		}
		else {
			y -= i;
			for (j = 0;j <= i;j++) {
				gotoxy(x + j, y + j);
				printf("0");
				Sleep(20);
			}
		}
	}



	getch();
	return 0;
}


5dd981b075bc1717719159.png
5dd981b5225ad197445411.jpeg

Первое фото, направление обхода, второй ошибка.
Мне кажется что ошибка на строках 26 и 41.
КАК ИСПРАВИТЬ?
  • Вопрос задан
  • 80 просмотров
Пригласить эксперта
Ответы на вопрос 1
AnnTHony
@AnnTHony
Интроверт
5ddbc6c14379d101951366.gif

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <windows.h>

const int _DELAY = 300;

void set_cursor_position(int row, int col)
{
    COORD coord;
    coord.X = col;
    coord.Y = row;

    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}

void set_left_top(int *start_row, int *start_col, const int finish_row, const int finish_col)
{
    while (true)
    {
        Sleep(_DELAY);

        set_cursor_position(*start_row, *start_col);
        printf("1");

        *start_row = *start_row - 1;
        *start_col = *start_col - 1;

        if (*start_row < finish_row)
        {
            *start_row = *start_row + 1;
            *start_col = *start_col + 2;

            break;
        }

        if (*start_col < finish_col)
        {
            *start_col = *start_col + 1;

            break;
        }
    }
}

void set_right_bottom(int *start_row, int *start_col, const int finish_row, const int finish_col)
{
    while (true)
    {
        Sleep(_DELAY);

        set_cursor_position(*start_row, *start_col);
        printf("0");

        *start_row = *start_row + 1;
        *start_col = *start_col + 1;

        if (*start_col > finish_col)
        {
            *start_row = *start_row - 2;
            *start_col = *start_col - 1;

            break;
        }

        if (*start_row > finish_row)
        {
            *start_row = *start_row - 1;

            break;
        }
    }
}

int main()
{
    printf("*** MATRIX RESOLUTION ***\n\n");

    int rows;
    printf("ROWS: ");
    scanf("%d", &rows);

    int padding_top = rows + 4;
    int padding_left = 2;

    int cols;
    printf("COLS: ");
    scanf("%d", &cols);
    cols = cols + padding_left;

    bool direction = true;

    int start_row = padding_top;
    int start_col = padding_left;

    while (start_row != (padding_top - rows) + 1 || start_col != cols)
    {
        if (direction)
        {
            set_left_top(&start_row, &start_col, (padding_top - rows) + 1, padding_left);
        }
        else
        {
            set_right_bottom(&start_row, &start_col, padding_top, cols - 1);
        }

        direction = !direction;
    }

    padding_top = padding_top + 1;
    set_cursor_position(padding_top, 0);

    return 0;
}
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы