Click here to Skip to main content
14,429,861 members
Rate this:
Please Sign up or sign in to vote.
See more:
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:

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 13-Nov-19 3:57am

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

It is very similar:
#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, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100