D
D
dimaweyder2019-11-23 22:00:57
Algorithms
dimaweyder, 2019-11-23 22:00:57

How to fix this error when traversing the matrix?

#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
First photo, bypass direction, second error.
It seems to me that the error is on lines 26 and 41.
HOW TO FIX?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Fedoryan, 2019-11-25
@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;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question