SPOJ Problem - CPTTRN6 - Character Patterns (Act 6)

This is a problem from SPOJ. We have to print a character pattern according to the input given.

We have input

33 1 2 1 4 4 1 2 2 5 3 2  

Here 3 is the number  of test cases.
Then integers in next line are 
l - the number of horizontal elements, 
c - the number of vertical elements in the grid; 
h and w - the high and the with of the single rectangle respectively. 
i.e  
l c h w

The output is
  .|. .|. -+- .|. .|. -+- .|. .|. -+- .|. .|. ..|..|..|..|.. --+--+--+--+-- ..|..|..|..|.. --+--+--+--+-- ..|..|..|..|.. --+--+--+--+-- ..|..|..|..|.. --+--+--+--+-- ..|..|..|..|..  ..|..|..|..|..|.. ..|..|..|..|..|.. ..|..|..|..|..|.. --+--+--+--+--+-- ..|..|..|..|..|.. ..|..|..|..|..|.. ..|..|..|..|..|.. --+--+--+--+--+-- ..|..|..|..|..|.. ..|..|..|..|..|.. ..|..|..|..|..|.. 

Here is the program to print above pattern in C++.
#include <iostream>
#include <cstdio>

int main()
{
  int t;
  scanf("%d", &t);
  while(t--)
  {
    int l, c, h, w;
    scanf("%d %d %d %d", &l, &c, &h, &w);
    int vertical_length = h * (l+1) + l;
    int horizontal_lenght = w * (c+1) + c;

    for(int i = 1; i <= vertical_length; i++)
    {
      for(int j = 1; j <= horizontal_lenght; j++)
      {
        if(i % (h+1) == 0 && j % (w+1) == 0)
        {
          printf("+");
        }
        else if(j % (w+1) == 0)
        {
          printf("|");
        }
        else if(i % (h+1) == 0)
        {
          printf("-");
        }
        else
        {
          printf(".");
        }
      }
      printf("\n");
    }
  }
}
 
Here we have used scanf() and printf() because they are faster then cout and cin.

You can download this code from here.
You can run this program from here. (Don't forget to enter values)


                                                                  
                                                                    
You may also like
SPOJ Problem - CPTTRN5 - Character Patterns (Act 5) 
C++: Swapping Node Links in Linked List
What is "namespace" and why do we use it?
Which Linux distribution is the best for a programmer?
What programming languages do what?

Comments