Thread: Tiles in kitchen

  1. #1
    Registered User
    Join Date
    Nov 2017
    Posts
    12

    Tiles in kitchen

    Good day, I need help with this code, please.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <float.h>
    
    #define EPS 1e-5
    
    
    
    double gcd(double val1, double val2)
    {
        double tmp, a;
    
    
    
        if(val1 > val2){
    
            tmp = val1;
            val1 = val2;
            val2 = tmp;
        }
    
        a = fmod(val2, val1);
    
        if(fabs(a) < EPS){
        return val1;
        }
    
        else {
        return gcd(a, val1);
        }
    }
    
    double lcm(double num1, double num2)
    {
        double lcmr, gcdr;
        gcdr = gcd(num1, num2);
    
        lcmr = ((num1 * num2) / gcdr);
    
        return lcmr;
    
    }
    
    double decimal(double a1)
    {
        int a1i, k;
        double r, i;
        k = 0;
        a1i = a1;
        r = (a1 - a1i);
    
        for(i = 0; i < 1; i = i + 0.1)
        {
            if( fabs(i - r) <= EPS * fabs(i + r) )
            {
                k = 1;
            }
        }
    
        if(k != 1){
            printf("Nespravny vstup.\n");
            return 0;
        }
        return k;
    }
    
    int main()
    {
        double tile, joint, value1, x, y, max, alf, blf;
        int in, a, b;
        value1 = 0.1;
        max = 10000000;
        printf("Minimalni velikost: \n");
        if (scanf(" %lf %lf", &x, &y) !=2 || (x - max) > EPS * fabs(x + max) || (y - max) > EPS * fabs(y + max) || x <= 0 || y <= 0){
        printf("Nespravny vstup.\n");
        return 0;
        }
    
         decimal(x);
         decimal(y);
    
        printf("Zadej cisla: \n");
        while( ( in = scanf("%lf %lf", &tile, &joint)) != EOF)
        {
    
        decimal(tile);
        decimal(joint);
    
        if(in != 2 || tile < 0 || joint < 0)
        {
            printf("Nespravny vstup.\n");
            return 0;
        }
    
        if((tile - max) > EPS * fabs(tile + max) || tile <= 0)
        {
            printf("Nespravny vstup.\n");
            return 0;
        }
    
            value1 = lcm(value1, tile + joint);
    
        }
    
    
        a = x / value1;
        b = y / value1;
    
        alf = a * value1;
        blf = b * value1;
    
        while(x >= alf)
        {
    
        alf = alf + value1;
    
        }
    
    
        while(y >= blf)
        {
    
        blf = blf + value1;
    
        }
    
        printf("alf: %lf\n", alf);
        printf("blf: %lf\n", blf);
    
    
        return 0;
    }
    I have this code and need to calculate minimal width and length of the kitchen.

    Tiles in kitchen-index-php-png

    I have unknown count of tiles with unknow extent and joint. This code is perfect when joint = 0, but when I add joint, code doesnt work. The accuracy is on millimeters, so 0.1, 0.2, 0.3 ... I dont know which tile will be used so every tile + joint have to pave the kitchen. And I can use only whole tile. I just dont know, where to add the joint. Thank you.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    You're missing something very fundamental here, and that is basic math. If you pay attention, you're see that if you have to lay out N tiles in a line, the joints between then are (N+1). Therefore, if you had an area that was A*B, in order to have a perfect fit you'd need the formulas:
    Code:
    tileLength*N + joint*(N+1) = A
    tileWidth*M + joint*(M+1) = B
    solve for N and M, and only if both are natural/whole numbers the dimensions are correct.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Nov 2017
    Posts
    12
    I tried it but this works for max 2 tiles. If I had 3 or more tiles Its not effective. The A and B are inputs of minimal kitchen size. So

    Code:
    tileLength*N + joint*(N+1) >= A
    tileWidth*M + joint*(M+1) >= B
    

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by GReaper View Post
    You're missing something very fundamental here, and that is basic math. If you pay attention, you're see that if you have to lay out N tiles in a line, the joints between then are (N+1). Therefore, if you had an area that was A*B, in order to have a perfect fit you'd need the formulas:
    Code:
    tileLength*N + joint*(N+1) = A
    tileWidth*M + joint*(M+1) = B
    solve for N and M, and only if both are natural/whole numbers the dimensions are correct.
    Hint: The above post implies using math not a computer to solve the equations.
    Edit2: After solving, at least partly, the equations you can then use the new equations in your program

    Edit: You will likely need floor in your program floor(3) - Linux man page

    Tim S.
    Last edited by stahta01; 11-30-2017 at 06:11 PM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Kitchen Timer sugestions for improvements
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 12-08-2005, 11:53 AM
  2. tiles
    By pode in forum Game Programming
    Replies: 2
    Last Post: 01-01-2003, 09:01 PM
  3. Tiles
    By JoshG in forum Game Programming
    Replies: 18
    Last Post: 06-05-2002, 08:39 AM

Tags for this Thread