stanley cho
stanley cho

Reputation: 153

Making rectangle based on input

input

6 6 4

output

######
#....#
#....#
#....#
#....#
######

input

7 7 4

output

#######
#.....#
#.....#
#.....#
#.....#
#.....#
#######

This is the code I have so far.

 #include <stdio.h>

int main()
{
int width;
int breadth;

scanf("%d", &width);
scanf("%d", &breadth);
if (width == 1 && breadth == 1)
{printf ("#\n");}
else{
for(int i = 0; i<breadth; i++){
    for(int j = 0; j<width;j++)
    {
        {printf("#");}

    }
    printf("\n");}
}

return 0;}

Does anyone know how to replace the non boundary area with "."?

ps:there is no need to worry about the third input, it does not affect the shape of rectangles since its just going to be used next task.

I am guessing that I could perhaps change my for loops but I ran out of ideas at this point.

It would be greatly appreciated if someone could help me return identical output from above.

Thank you.

Upvotes: 2

Views: 161

Answers (5)

Ajay Brahmakshatriya
Ajay Brahmakshatriya

Reputation: 9203

Using conditions as suggested by other answers is the ideal way but if that is unclear, you can break down the printing into smaller loops as

for (i=0; i<width; i++)          // prints the first line
    printf("#");                 //
printf("\n");                    // 

for (j=0; j<breadth-2; j++){     // prints middle breadth-2 lines
    printf("#");                 // prints the starting hash in middle lines
    for (i=0; i<width-2; i++)    // prints the '.'s  
        printf(".");
    printf("#\n");               // prints the last hash in middle lines 
}

for (i=0; i<width; i++)          // prints the last line  
    printf("#");                 // 
printf("\n");                    // 

Although not very efficient, the logic is very easy to understand. Let me know if further explanation is required.

Edit: Actually after writing this answer I later realized that this code will infact be faster than the ones with branches. Since we have split the iteration domains ourselves, there are no branches. This reduces any chance of branch prediction failing, which would certainly happen in the solution with if conditions since the condition is true for small cases. Ofcourse it wouldn't matter for small iterations like 6x6, but just putting the thought out there.

Upvotes: 1

Andyliu
Andyliu

Reputation: 31

I have check it by gcc ,it works.Please see the notes in code:

   int main()
 {
   int width;
   int breadth;

   scanf("%d",&width);
   scanf("%d",&breadth);
   for(int i = 0; i < width; i++){
    for(int j = 0; j < breadth; j++ ){
       //first line and last line 
        if((i == 0)||i == (width - 1)){
        printf("#");
       //first column and last column
    }else if((j == 0)||(j == (breadth -1))){
        printf("#");
       //other case 
    }else{
        printf(".");
    }   
    }
    printf("\n");
 }
 return 0;
}

Upvotes: 1

nic3ts
nic3ts

Reputation: 347

You should use counters. For example, one counter should be refering to which line you're printing and another one the column you're printing on.

If the line counter is 0, print all the line as '#' cause it's going to be the upper boundary line (this creates -> #######)

Else If the line counter is same as the height of the shape, then it's the finishing border line (#######)

Else if it's >0, print the first character as '#', then print '.' until another counter reaches the width of the shape -1 (for example in 7 7 that would be 5 (since 0 also counts)) and then another '#' (#.....#)

I think this is enough help for you to get it done by yourself, should you require further assistance tho, just ask.

Upvotes: 1

Nguai al
Nguai al

Reputation: 958

Here is the working code. You were almost there. You need the condition statement to check the boundary.. Please see the line that checks the boundary

#include <stdio.h>

int main()
{
    int width;
    int breadth;

    scanf("%d %d", &width, &breadth);

    if (width == 1 && breadth == 1)
       printf ("#\n");
    else
    {
       for(int i = 0; i<breadth; i++)
       {
          for(int j = 0; j<width;j++)
              //Check for boundary.
              if( j == 0 || j == width - 1 || i == 0 || i == breadth - 1 )
                 printf("#");
              else 
                 printf(".");
          printf("\n");
       }
    }
   return 0;
}

Upvotes: 1

Vishwajeet Vishu
Vishwajeet Vishu

Reputation: 492

You want to replace non-boundary area with ".". To achieve that you need a condition that satisfies your boundary.

#include <stdio.h>
int main()
{
int width;
int breadth;
scanf("%d", &width);
scanf("%d", &breadth);
if (width == 1 && breadth == 1)
{printf ("#\n");}
else{
for(int i = 0; i<breadth; i++){
for(int j = 0; j<width;j++)
{
if(i==0||j==0)    
   printf("#");
else if(i==(breadth-1)||j==(width-1))
   printf("#"); 
else
   printf(".");
}
printf("\n");}
}
return 0;
}

Hope this works. Let me know if it does not or any problem is there.

Upvotes: 2

Related Questions