How to create a rectangle using asterisks in C # (for cycle)

5

(For) Create a program that asks the user for width (for example, 5) and height (for example, 4) and write a rectangle formed by that number of asterisks, eg:

At the moment I have this:

Console.WriteLine("introduce el tamañno de cuadrado");
int tam = int.Parse(Console.ReadLine());
int i, j;
for (i = 1; i <= tam; i++) // 15 lineas 
{
    for (j = 1; j <= tam; j++) // números a cada línea 
        Console.Write("*", j);
    Console.WriteLine(" ");
}
Console.ReadKey();

But I do not know how to make use of the value of the width, I would greatly appreciate it if someone could help me.

    
asked by Ricardo Portillo 14.08.2018 в 17:16
source

5 answers

5

You have to ask (as you are doing with the height) the length and save it in a variable chord.

Then you use each variable in the indicated for

Console.WriteLine("introduce el alto del rectangulo");
int alto = int.Parse(Console.ReadLine());
Console.WriteLine("introduce el ancho del rectangulo");
int ancho = int.Parse(Console.ReadLine());
int i, j;
for (i = 1; i <= alto; i++) // For para el alto 
{
    for (j = 1; j <= ancho; j++) // For para el ancho
        Console.Write("*", j);
    Console.WriteLine(" ");
}
Console.ReadKey();
    
answered by 14.08.2018 / 17:33
source
4

I found it fun, so I've put this way, with only one cycle:

public static void Main (string[] args)
{
  int Ancho, Alto; string Asteriscos = null;

  Console.Write("Escriba el ancho: "); // Pedimos la anchura.
  if (!int.TryParse(Console.ReadLine(), out Ancho))
    return;
  Console.Write("Escriba el alto: "); // Pedimos altura
  if (!int.TryParse(Console.ReadLine(), out Alto))
    return;

  Asteriscos = new string('*', Ancho); // Creamos los asteriscos bonitos.
  for (int i = 0; i < Alto; i++) 
  {
    // Imprimimos asteriscos como sea necesario.
    if (i == 0 || i == (Alto - 1))
      Console.WriteLine(Asteriscos);
    else
      Console.WriteLine("*{0," + (Ancho - 2) + "}*", ' ');
  }
}

The only thing that this does is write a value or another depending on the value of the cycle variable ( i ), if it is in the first or last position, write a lot of asterisks, otherwise, only write 2 asterisks and format the output to make the shape.

Notice how to get the input values, they do not control the entry of a negative value or if the value of the width or height is valid to make the figure.

Some results:

Escriba el ancho:  5    Escriba el ancho:  10
Escriba el alto:  5     Escriba el alto:  6
*****                   **********
*   *                   *        *
*   *                   *        *
*   *                   *        *
*****                   *        *
                        **********

Here is a repl.it

Greetings:)

    
answered by 14.08.2018 в 17:43
2

Try something like this: Where v is the number of lines and h is the number of characters per line.

            int i, j;
            for (i = 1; i <= v; i++) // numero de lineas lineas 
            {
                var line = "";
                for (j = 1; j <= h; j++)// números a cada línea 
                {
                    line = line + "*";
                }
                Console.WriteLine(line);
            }
    
answered by 14.08.2018 в 17:30
2

This should be my estimate.

Console.WriteLine("introduce el ancho del rectangulo");
int ancho = int.Parse(Console.ReadLine());
Console.WriteLine("introduce el largo del rectangulo");
int largo = int.Parse(Console.ReadLine());
int i, j;
for (i = 1; i <= ancho; i++)  
{
for (j = 1; j <= largo; j++) 
    Console.Write("* ", j);
Console.WriteLine("\n");
}

I hope I help you.

    
answered by 14.08.2018 в 17:32
2

I know that the exercise is about the for loop and that you already have enough answers, but I'll give you a small alternative that is somewhat more efficient.

So far almost all the answers are like this (in heavy-code):

para i de 0 a alto 
  para j de 0 a ancho
    escribe "*"
  escribe salto de línea  

This will generate the square you want, but it is not really efficient because you are performing the same operation many times and always with the same result. In fact, the order of algorithmic complexity is O (n 2 ), which is far from ideal.

The thing is that the lines are always the same, so you do not need to generate a new one every time, you just generate it once with a for (saving it in a chain), and then with another loop for you write it as many times as you need. That way the order of algorithmic complexity will be reduced to O (n).

Console.Write("Introduce el ancho del cuadrado: ");
int ancho = int.Parse(Console.ReadLine());
Console.Write("Introduce el alto del cuadrado: ");
int alto = int.Parse(Console.ReadLine());

string linea = "";

// aquí generas la cadena a escribir por pantalla
for (int i = 1; i <= ancho; i++)
  linea += "* ";

// aquí muestras la cadena tantas veces como el alto
for (int j = 1; j <= alto; j++) 
  Console.WriteLine(linea);

You can see it working on this repl.it .

    
answered by 14.08.2018 в 18:07