Reputation: 23
I would like to print the below pattern, where user enters n and m (4 and 6) the outside boundary should be made up with stars and inside should be filled with circles.
****** ****** ****** ******
*oooo* *oooo* *oooo* *oooo*
*oooo* *oooo* *oooo* *oooo*
*oooo* *oooo* *oooo* *oooo*
*oooo* *oooo* *oooo* *oooo*
****** ****** ****** ******
****** ****** ****** ******
*oooo* *oooo* *oooo* *oooo*
*oooo* *oooo* *oooo* *oooo*
*oooo* *oooo* *oooo* *oooo*
*oooo* *oooo* *oooo* *oooo*
****** ****** ****** ******
****** ****** ****** ******
*oooo* *oooo* *oooo* *oooo*
*oooo* *oooo* *oooo* *oooo*
*oooo* *oooo* *oooo* *oooo*
*oooo* *oooo* *oooo* *oooo*
****** ****** ****** ******
****** ****** ****** ******
*oooo* *oooo* *oooo* *oooo*
*oooo* *oooo* *oooo* *oooo*
*oooo* *oooo* *oooo* *oooo*
*oooo* *oooo* *oooo* *oooo*
****** ****** ****** ******
The code I have written is as below. What I am doing wrong?
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int n,i,j,k,m,n1;
printf("enter table size and enter square size");
scanf("%d %d", &n, &n1);
for (i = 0; i < n; i++) {
for (j = 0; j < n1; j++)
{
for ( k = 0; k < n; k++)
{
for ( m = 0; m < n1; m++)
{
{
if(i==0 || i==n-1)
{
printf("*");
}
else if(j==0 || j==n-1)
{
printf("*");
}
else
{
printf("o");
}
}
}
printf(" ");
}
printf("\n");
}
printf("\n");
}
system("pause");
return 0;
}
Upvotes: 0
Views: 218
Reputation: 212
The corrected code is
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int n,i,j,k,m,n1;
printf("enter table size and enter square size");
scanf("%d %d", &n, &n1);
for (i = 0; i < n; i++) {
for (j = 0; j < n1; j++)
{
for ( k = 0; k < n; k++)
{
for ( m = 0; m < n1; m++)
{
{
if(j==0 || j==n1-1)
{
printf("*");
}
else if(m==0 || m==n1-1)
{
printf("*");
}
else
{
printf("o");
}
}
}
printf(" ");
}
printf("\n");
}
printf("\n");
}
system("pause");
return 0;
}
Your index variables does the following:
|----------------n----------------------|
|--m--|
- ******* ******* ******* ******* *******
| *o ...
| *o ...
j *o ...
|
|
-
So basically you must print * when j is in first row or last row of each square or when m is in first row or last row of the square (n1).
Hope this helps
Upvotes: 1