# C drawing patterns

3

I am doing the exercises before the programming exam and I can not do this:

"Write a program that receives a natural number and paint approximately a square figure on a screen using a given drawing pattern, for example, if you read the number 3, the square to be drawn is as follows:

[ "

At the moment I have this:

``````    int patron(int n);

int n;

int main()
{
scanf("%d",&n);

patron(n);
}

int patron(int n)
{
int i, j;

for(i=1; i<=n; i++)
{
for (j=1; j<=n; j++)
{
printf("+---");
}
printf("+\n");
}
printf("\n");
return 0;
}
``````

That returns this:

How do I do squares with exclamations? When you add the line breaks, everything breaks down.

asked by AguaSal 28.12.2016 в 17:06
source

0
`````` int patron(int n);

int n;

int main()
{
scanf("%d",&n);

patron(n);
}

int patron(int n)
{
int i, j;

for(i=0; i<=n; i++)
{
for (j=1; j<=n; j++)
{
printf("+---");
}
printf("+\n");
if(i<n){
for (j=1; j<=n; j++)
{
printf("!   ");
}
printf("!\n");
}

}
printf("\n");
return 0;
}
``````

source
1

With this exercise, you will paint the drawing line by line. In this case, the odd lines will always be (+ ---) and the even lines will be (!). Control it with the counter of the first FOR, depending on whether it is even or odd you paint one thing or another.

1

I would do it this way, I would modify:

``````printf("+\n");
``````

By

``````if(i != n)
printf("+\n!   !   !   !\n");
else
printf("+\n");
``````

To make something like this:

In this example, `n` equals 3.

1

I leave you an alternative

``````void pintarCuadro(int n)
{

for(int i=0; i<n+1; i++)
{
//    printf("pintar\n");
for(int j=0; j<n; j++)
{
printf("+---");
}
printf("+\n");

if(i<n)
{
for(int j=0; j<n; j++)
{
printf("!   ");
}
printf("!\n");

}
}
}

int main()
{

return 0;
}
``````

1

I show you an alternative way, which lets you choose the HIGH and the internal WIDTH of each cell.

Also, instead of doing multiple nested `for` , we precalculate the lines to be displayed, so instead of `x * y` iterations, we only do 3: precalculate the lines close , precalculate the internal lines, and a final loop to show them.

In the `for` final%, we check if we are on a line before the first line of crossover ; if we already pass it, we use the module (rest) of the division to know if we have to show an internal line of the cell, or a crossover .

As a last point, compile from C89 onwards.

``````#include <stdio.h>
#include <string.h>

#define ANCHO 2
#define ALTO 2

void showMatrix( int x ) {
int realWidth = ( ANCHO * x ) + x + 1;
char up[realWidth + 1];
char inter[realWidth + 1];
int idx;

memset( up, '-', realWidth - 1 );
up[realWidth] = 0;

for( idx = 0; idx < realWidth; idx += ANCHO + 1 )
up[idx] = '+';

memset( inter, ' ', realWidth - 1 );
inter[realWidth] = 0;

inter = '!';
inter[realWidth - 1] = '!';
for( idx = ANCHO + 1; idx < realWidth; idx += ANCHO + 1 )
inter[idx] = '!';

realWidth = ( ALTO * x ) + x + 1;

printf( "%s\n", up );
for( idx = 1; idx < realWidth ; ++idx )
if( idx <  ALTO )
printf( "%s\n", inter );
else
printf( "%s\n", idx % ( ALTO + 1 ) ? inter : up );
}

int main( void ) {
showMatrix( 6 );

return 0;
}
``````

1

Thanks to everyone for the contributions, I have solved the problem, here is the final code:

``````    int patron(int n);
int n;

int main()
{
scanf("%d",&n);

patron(n);
}

int patron(int n)
{
int i, j,k, f;

for(i=1; i<=n; i++)
{
for (j=1; j<=n; j++)
{
printf("+---");
}
printf("+\n");

for (k=1; k<=n; k++)
{
printf("!   ");
}

printf("!\n");
if (i==n){

for (f=1; f<=n; f++)
{
printf("+---");
}
printf("+\n");
}

}

printf("\n");
return 0;
}
``````

``````     for (k=1; k<=n; k++)
{
printf("!   ");
}

printf("!\n");
if (i==n){

for (f=1; f<=n; f++)
{
printf("+---");
}
printf("+\n");
}
``````