0

I am currently doing exercices on Kattis and I meet a problem with I think the float. I must compare the size of matchstick with the size of box

Sibice problem on Kattis.com

Here is a picture of the exercise

Here is picture of this exercise

I can make examples but when I submit I past only the first two...

Here is a picture of my submission

Here is picture of my submission

I don't have a trace or explication for know my errors... I tried to change the type of my variables but there is no change... I think the problems is float but I need it.

Here is my code

#include <stdio.h>

void sibice(float n, float w, float h)
{
    float v = 0;

    for(float i = 0; i != n; i += 1) {
        scanf("%f", &v);
        if(v < w + h / 2)
            printf("DA\n");
        if(v == w + h / 2)
            printf("DA\n");
        if(v > w + h / 2) {
            printf("NE\n");
        }
    }
}

int main(void)
{
    float n = 0;
    float w = 0;
    float h = 0;

    scanf("%f %f %f", &n, &w, &h);
    sibice(n, w, h);
    return (0);
}

Do you think that I can optimize my code ?

PtiCassarin
  • 3
  • 1
  • 3
  • Are you sure you are asking how to optimize the code and not how to correct it? Why use float when inputs are integers? Just move division to the left side, like`v * 2 < w + h`. `if(v < w + h / 2)` and `if(v == w + h / 2)` - just `if(v <= w + h / 2)`... Don't you need to calculate the diagonal in the box? `w + h / 2` - is equal to `w + (h / 2)` and why would you calculate it? what would it be equal to? – KamilCuk Mar 24 '20 at 13:12
  • 2
    Welcome to SO! Please, don't post question as images, since textual data is useful to experts in order to build the answer for you (and clicking links doesn't encourage reading). Instead, icorporate all information contained in the images within the question. – Roberto Caboni Mar 24 '20 at 13:12
  • Are you sure you need to use `float`? All the inputs are integers. In the example, the third match fits the box because 5^2 <= 3^2 + 4^2 (pythagoras). No division or square root is needed. Please see [Is floating point math broken?](http://stackoverflow.com/questions/588004/is-floating-point-math-broken) and [Why Are Floating Point Numbers Inaccurate?](https://stackoverflow.com/questions/21895756/why-are-floating-point-numbers-inaccurate) – Weather Vane Mar 24 '20 at 13:14
  • Your math is wrong, the longest size that fits in the box is not `w + h / 2`. This has nothing to do with `float`. – flyx Mar 24 '20 at 13:17
  • @KamilCuk Note: The `v *2 < w + h` suggestion is not functionally the same as `v < w + h / 2` as the latter is like `v < w + (h/2)`. IAC, neither compare is needed. – chux - Reinstate Monica Mar 24 '20 at 13:42
  • `for(float i = 0; i != n; i += 1)` is prone to infinite loops. `i` might never actually equal `n`, and at some point, `i += 1` won't increment `i` at all because floating-point math is not exact, and there's only so much resolution in a `float`. Once `i` gets big enough, `1` isn't large enough by itself to even appear in the bits that make up the value held in `i`. – Andrew Henle Mar 24 '20 at 14:07

2 Answers2

1

Incorrect test

If a match can fit is a more like v*v <= h*h + w*w than v < w + h / 2. Can it diagonally fit?
See Pythagorean theorem

 #include <math.h>

   ...
   float hyp = hypotf(h,w);  // sqrt(h*h + w*w)
   for(float i = 0; i != n; i += 1) {
      scanf("%f", &v);
      // if(v < w + h / 2)
      if(v < hyp)

No need for 3 tests

One test is sufficient.

    if(v <= hyp) {
      printf("DA\n");
    } else {
      printf("NE\n");
    }

Integer math

As all calculations are whole numbers, code could use int only math.

 void sibice(int n, int w, int h) {
   int hyp2 = h*h + w*w;
   for (int i = 0; i != n; i += 1) {
      int v;
      scanf("%d", &v);
      if (v*v <= hyp2)
      ...

Other issues may exist

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
0

Why do you need to use float? With these changes the code passes the tests:

#include <stdio.h>
#include <stdlib.h>

void sibice(int n, int w, int h)
{
    int v;
    int result;

    for (int i = 0; i != n; i += 1) {
        result = scanf("%d", &v);
        if (1 != result)
            exit(1);
        if (v * v <= w * w + h * h)
            printf("DA\n");
        else
            printf("NE\n");
    }
}

int main(void)
{
    int n = 0;
    int w = 0;
    int h = 0;
    int result = 0;

    result = scanf("%d %d %d", &n, &w, &h);
    if (3 != result)
        return 1;
    sibice(n, w, h);
    return (0);
}