Click here to Skip to main content
15,113,855 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Python
def cuadrado(width, height):

    if width % 2 == 0:
        width += 1
    if height % 2 ==  0:
        height +=1
        
    for row in range(height):
        for col in range(width):
            if 0 < row < height - 1 and 0 < col < width - 1:
                print(" ", end = "")
            elif col % 2 == 0:
                print("+", end = "")
            else:
                print("-", end = "")
        print()

def main():
    alto = int(input("Dame el alto del cuadrado: "))
    ancho = int(input("Dame el ancho del cuadrado: "))

    cuadrado(ancho, alto)

main()


What I have tried:

Python
def cuadrado(width, height):

    if width % 2 == 0:
        width += 1
    if height % 2 ==  0:
        height +=1
        
    for row in range(height):
        for col in range(width):
            if 0 < row < height - 1 and 0 < col < width - 1:
                print(" ", end = "")
            elif col % 2 == 0:
                cout("+", end = "")
            else:
                cout("-", end = "")
        cout()

def main():
    alto = int(input("Dame el alto del cuadrado: "))
    ancho = int(input("Dame el ancho del cuadrado: "))

    cuadrado(ancho, alto)

main()
Posted
Updated 5-Nov-20 0:19am
v3

1 solution

It is very similar:
C++
#include <iostream>
using namespace std;

void cuadrado( size_t w, size_t h )
{
  w |= 1;
  h |= 1;

  for ( size_t r=0; r<h; ++r )
  {
    for (  size_t c=0; c<w; ++c)
      if ( r && r < (h-1) && c && c <(w-1))
        cout << ' ';
      else if ( c & 1 )
        cout << '-';
      else
        cout << '+';
    cout << endl;
  }
}

int main()
{
  size_t alto, ancho;
  cout << "Dame el alto del cuadrado: ";
  cin >> alto;
  cout << "Dame el ancho del cuadrado: ";
  cin >> ancho;

  cuadrado(ancho, alto);
}
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900