Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

How can I put a 0 in that location of the arrangement (C++)?

+1 vote
asked Mar 8, 2020 by A

I have to make an arrangement (array) were, when the number given by the person is even, it displays something like this:

Example: N=6

1 1 1 1 1 1

1 0 0 0 0 1

1 0 0 0 0 1

1 0 0 0 0 1

1 0 0 0 0 1

1 1 1 1 1 1

And when the number is odd it is something like this (with the 0 in the center):

Example: N=7

0 0 0 0 0 0 0

0 1 1 1 1 1 0

0 1 1 1 1 1 0

0 1 1 0 1 1 0

0 1 1 1 1 1 0

0 1 1 1 1 1 0

0 0 0 0 0 0 0

I have everything already, except the 0 in the center part... which I don't really know how to put. Can anybody help me please? This is what I got:

*******************************************************************************

#include <iostream>

using namespace std;

int main()

{

    int ren, col, n, res=0;

    cout<<"Give me a number: ";

    cin>>n;

    int arreglo[n][n];

    res=n%2;

    for (ren=0; ren<n; ren++)

    {

        for (col=0; col<n; col++)

        {

            if (res==0)

            {

                if (ren==0 || col==0 || ren==n-1 || col==n-1)

                    arreglo[ren][col]=1;

                else

                    arreglo[ren][col]=0;

                    cout<<arreglo[ren][col]<<"  ";

            }

            else

            {

                if (ren==0 || col==0 || ren==n-1 || col==n-1)

                    arreglo[ren][col]=0;

                else

                    arreglo[ren][col]=1;

                    cout<<arreglo[ren][col]<<"  ";

            }

        }

        cout<<"-"<<endl;

    }    

    return 0;

}

1 Answer

0 votes
answered Mar 9, 2020 by FreeKill
#include <iostream>
#include <cstdlib>

using namespace std;

int main()

{

    int ren, col, n, res=0;

    cout<<"Give me a number: ";

    cin>>n;

    int arreglo[n][n];

    res=n%2;

    for (ren=0; ren<n; ren++)

    {

        for (col=0; col<n; col++)

        {

            if (res==0)

            {

                if (ren==0 || col==0 || ren==n-1 || col==n-1)

                    arreglo[ren][col]=1;

                else

                    arreglo[ren][col]=0;

                    cout<<arreglo[ren][col]<<"  ";

            }

            else

            {
                
                if (ren==0 || col==0 || ren==n-1 || col==n-1)

                    arreglo[ren][col]=0;
                
                else if((ren==div(n,2).quot)&&(col==div(n,2).quot))
                
                    arreglo[ren][col]=0;
                
                else

                    arreglo[ren][col]=1;

                    cout<<arreglo[ren][col]<<"  ";

            }

        }

        cout<<"-"<<endl;

    }    

    return 0;

}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...