Wypisanie rombu w ASCII

0

Witam, mam zadanie które polega na zbudowaniu amuletu i po wielu próbach udało mi się stworzyć program w którym wszystko działa oprócz dla liczb parzystych wyskakuje za dużo kropek i nie mogę dojść dlaczego. Proszę o mała pomoc :)

petla for.png

#include <iostream>

using namespace std;

int main()
{
    int n, i, j;
    cin >> n;

    if (n % 2 == 0)
        i = 1;
    else
        i = 0;

    for (i = 0; i < n / 2 + 1; i++) {
        for (j = 0; j < n / 2 - i; j++)
            cout << ".";
        for (j = n / 2 - i; j < (n + 1) / 2 + i; j++)
            cout << "X";
        for (j = (n + 1) / 2 + i; j < n; j++)
            cout << ".";
        cout << endl;
    }
    for (i = n / 2; i > 1 / 2 - 1; i--) {
        for (j = 0; j < n / 2 - i; j++)
            cout << ".";
        for (j = n / 2 - i; j < (n + 1) / 2 + i; j++)
            cout << "X";
        for (j = (n + 1) / 2 + i; j < n; j++)
            cout << ".";
        cout << endl;
    }
    return 0;
}
1

i < n / 2 + 1
albo
i > 1 / 2 - 1
WTF?

0

przepraszam za blad bo kombinowałam tutaj bardziej poprawne:

#include <iostream>

using namespace std;

int main()
{
int n,i,j;
cin >> n;

if(n%2==0)
    i=1;
else
    i=0;


for( i; i<n/2+1; i++)
{
    for(j=0; j<n/2-i; j++)
        cout << ".";
    for(j=n/2-i; j<(n+1)/2+i; j++)
        cout << "X";
    for(j=(n+1)/2+i; j<n; j++)
        cout << ".";
        cout << endl;
}
for( i=n/2; i>0; i--)
{
    for(j=0; j<n/2-i; j++)
        cout << ".";
    for(j=n/2-i; j<(n+1)/2+i; j++)
        cout << "X";
    for(j=(n+1)/2+i; j<n; j++)
        cout << ".";
        cout << endl;
}
    return 0;
}
```cpp
0

Sformatowany kod z kolorowaniem składni (użyj cytuj by zobaczyć jak):

#include <iostream>

using namespace std;

int main()
{
    int n, i, j;
    cin >> n;

    if (n % 2 == 0)
        i = 1;
    else
        i = 0;

    for (i; i < n / 2 + 1; i++) {
        for (j = 0; j < n / 2 - i; j++)
            cout << ".";
        for (j = n / 2 - i; j < (n + 1) / 2 + i; j++)
            cout << "X";
        for (j = (n + 1) / 2 + i; j < n; j++)
            cout << ".";
        cout << endl;
    }
    for (i = n / 2; i > 0; i--) {
        for (j = 0; j < n / 2 - i; j++)
            cout << ".";
        for (j = n / 2 - i; j < (n + 1) / 2 + i; j++)
            cout << "X";
        for (j = (n + 1) / 2 + i; j < n; j++)
            cout << ".";
        cout << endl;
    }
    return 0;
}

Po uruchomieniu wygląda na to, że działa (trzeba naprawić warrning): https://wandbox.org/permlink/V9M9eOBzERdhxTEu https://wandbox.org/permlink/2GuPDxOwpgtotZQC

0
#include <iostream>

using namespace std;

int main()
{
int n,i,j;
cin >> n;

if(n%2==0)
    i=1;
else
    i=0;


for( i; i<n/2+1; i++)
{
    for(j=0; j<n/2-i; j++)
        cout << ".";
    for(j=n/2-i; j<(n+1)/2+i; j++)
        cout << "X";
    for(j=(n+1)/2+i; j<n; j++)
        cout << ".";
        cout << endl;
}
for( i=n/2; i>0; i--)
{
    for(j=0; j<n/2-i; j++)
        cout << ".";
    for(j=n/2-i; j<(n+1)/2+i; j++)
        cout << "X";
    for(j=(n+1)/2+i; j<n; j++)
        cout << ".";
        cout << endl;
}
    return 0;
}

czy tak jest poprawnie?

0

Ale zakręcone! Nie da się po ludzku?

#include <iostream>
using namespace std;

int main()
{
    int n;
    cin>>n;
    for(int N=n<<1,y=1;y<=N;y+=2,cout<<endl) for(int x=1;x<=N;x+=2) cout<<((abs(y-n)+abs(x-n))<=n?'#':'.');
    return 0;
}

https://ideone.com/ClBv8n

1

https://wandbox.org/permlink/UONAJRFpi0gLJtBK

std::ostream& printDimond(std::ostream& out, size_t size, char fill='X', char space='.', char endline = '\n')
{
    for (size_t spaceCount = (size - 1) / 2; spaceCount > 0; --spaceCount) {
        auto spaces = std::string(spaceCount, space);
        out << spaces << std::string(size - 2 * spaceCount, fill) << spaces << endline;
    }
    if (size % 2 == 0) {
        out << std::string(size, fill) << endline;   
    }
    for (size_t spaceCount = 0; spaceCount < (size + 1) / 2; ++spaceCount) {
        auto spaces = std::string(spaceCount, space);
        out << spaces << std::string(size - spaceCount * 2, fill) << spaces << endline;
    }

    return out;
}

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