Typ wchar_t i problem z rozwiązaniem zadania

0

Witam
oto link do zadania: link
moje rozwiązanie:
#include <wchar.h>

#include <wchar.h>

// X_string is preallocated and large enough to handle
// n * n squares, n newlines, and the terminating nul-character
wchar_t *x (unsigned n, wchar_t X_string[n * (n + 1) + 1])
{
	X_string[0] = L'\0'; // write to X_string
  for(int i = 0, j=n-1, z=0;z<n;z++)
  {
    for(int k=0;k<n;k++)
    {
      if(k==i||k==j)
        swprintf(X_string+wcslen(X_string),n * (n + 1) + 1,L"%lc", L'■');
      else
        swprintf(X_string+wcslen(X_string),n * (n + 1) + 1,L"%lc",L'□');
    }
    swprintf(X_string+wcslen(X_string),n * (n + 1) + 1,L"%lc",L'\n');
    i++, j--;
  }
  swprintf(X_string+wcslen(X_string),n * (n + 1) + 1,L"%lc",L'\0');
  	return X_string; // return it to allow function chaining

próbowałem też coś takiego:

#include <wchar.h>

// X_string is preallocated and large enough to handle
// n * n squares, n newlines, and the terminating nul-character
wchar_t *x (unsigned n, wchar_t X_string[n * (n + 1) + 1])
{
int index = 0;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (i == j || i == n - j - 1) {
                X_string[index++] = L'■';
            } else {
                X_string[index++] = L'□';
            }
        }
        X_string[index++] = L'\n';
    }
    X_string[index] = L'\0';
	return X_string; // return it to allow function chaining
}

i cały czas dostaję odpowiedź z serwera
for n = 3, expected:
■□■
□■□
■□■
but got:
■□■
□■□
■□■

Podpowie ktoś gdzie jest błąd?

4
    X_string[index-1] = L'\0';
0

Dziękuję

0

Bolączka takich zabaw., w pewnym momencie szkoda już na to nerwów. Ale dobre dla początkujących.

1 użytkowników online, w tym zalogowanych: 0, gości: 1