Search code examples
ctile

Find the number of part tiles required to tile a floor of given dimensions


This program must calculate how many tiles are needed to tile a floor. The tiles are 8 inches by 8 inches. Tiles can be used as a whole or a part of the tile can be used. Only one usable piece can be cut from a tile. That is, if a piece is cut from a tile, the rest of the tile must be thrown away. The program accepts the length and width of the room and returns how many whole tiles are used and how many part tiles are used. The length is given in inches.

I have tried this problem and have no problem obtaining the number of full tiles required but I seem to be having no luck at the number of part tiles required. I have written this program for it.

#include <stdio.h>
int main()
{
    int l, b, full, l1, bl, part;
    float ar2, ar3;
    scanf("%d%d", &l, &b);
    ar3 = (float)(l * b) / 64;
    l1 = l / 8;
    bl = b / 8;
    full = l1 * bl;
    ar2 = ar3 - full;
    part = ar2 * 2;
    printf("%d\n%d", full, part);
    return 0;
}

Solution

  • The common way is to have tiles parallel to the walls. In that case, you can just find the number of tiles necessary in both directions, and the total number of tiles will the the product of those 2 numbers. The trick to get the number of partial tiles is to separately compute the number of whole tiles and then the number of whole and partial tiles. Code becomes:

    #include<stdio.h>
    
    int main()
    {
      int l,b,full,l1,bl,part;
      int partl = 0, partb = 0;
    
      scanf("%d%d",&l,&b);
    
      l1=l/8;                       // number of whole tiles
      if ((l % 8) != 0) {
          partl = 1;                // will need one partial at the end
      }
      // second direction
      bl=b/8;
      if ((b % 8) != 0) {
          partb = 1;                // will need one partial at the end
      }
      full=l1*bl;                   // total number of whole tiles
      if (partl) l1 += 1;           // total (including partial) per direction
      if (partb) bl += 1;
      part = l1 * bl - full;        // partial is total - number of whole tiles
      printf("%d\n%d",full,part); 
      return 0;
    }