Shirley Dong
Shirley Dong

Reputation: 19

why does my code work for some test cases but not others

I was asked to translate this algorithm into code

//compute the max of size1 and (x_offset + size2).  Call this w

//compute the max of size1 and (y_offset + size2).  Call this h

//count from 0 to h. Call the number you count with y

//count from 0 to w. Call the number you count with x

  //check if  EITHER
  //    ((x is between x_offset  and x_offset +size2) AND 
  //     y is equal to either y_offset OR y_offset + size2 - 1 )
  //  OR
  //    ((y is between y_offset and y_offset + size2) AND
  //     x is equal to either x_offset OR x_offset + size2 -1)
  // if so, print a *

  //if not,
  // check if EITHER
  //    x is less than size1 AND (y is either 0 or size1-1)
  // OR
  //    y is less than size1 AND (x is either 0 or size1-1)
  //if so, print a #

  //else print a space
//when you finish counting x from 0 to w, 
//print a newline

and here is my code

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

void squares(int size1, int x_offset, int y_offset, int size2) {
  int w = (size1 > x_offset + size2) ? size1 : x_offset + size2;
  int h = (size1 > y_offset + size2) ? size1 : y_offset + size2;
  for (int y = 0; y < h; y++) {
    for (int x = 0; x < w; x++) {
      if (((x >= x_offset && x < x_offset + size2) &&
           (x == y_offset || x == y_offset + size2 - 1)) ||
          ((y >= y_offset && y < y_offset + size2) &&
           (x == x_offset || x == x_offset + size2 - 1))) {
        printf("*");
      } else if ((x < size1 && (y == 0 || y == size1 - 1)) ||
                 (y < size1 && (x == 0 || x == size1 - 1))) {
        printf("#");
      } else {
        printf(" ");
      }
    }
printf("\n");
    }
}

However, my code only works for some test cases e.g.

Testing ./squares 1 0 0 1
PASSED
 - Correct
Testing ./squares 1 0 0 5
Your program produced the wrong output!
 - Incorrect
Testing ./squares 1 0 0 9
Your program produced the wrong output!
 - Incorrect
Testing ./squares 1 0 1 1
PASSED
 - Correct
Testing ./squares 1 0 1 5
Your program produced the wrong output!
 - Incorrect
Testing ./squares 1 0 1 9
Your program produced the wrong output!
 - Incorrect
Testing ./squares 1 0 3 1
PASSED
 - Correct
Testing ./squares 1 0 3 5
Your program produced the wrong output!
 - Incorrect
Testing ./squares 1 0 3 9
Your program produced the wrong output!
 - Incorrect
Testing ./squares 1 0 8 1
PASSED
 - Correct

Could anyone help me with this? Thank you so so much!

Upvotes: 1

Views: 166

Answers (2)

chux
chux

Reputation: 153358

Consider coding to a contract in a way that is easier to follow.

Given

//check if EITHER
// ((x is between x_offset and x_offset +size2) AND
// y is equal to either y_offset OR y_offset + size2 - 1 )
// OR
// ((y is between y_offset and y_offset + size2) AND
// x is equal to either x_offset OR x_offset + size2 -1)
// if so, print a *

What if code was instead of

  if (((x >= x_offset && x < x_offset + size2) &&
       (x == y_offset || x == y_offset + size2 - 1)) ||
      ((y >= y_offset && y < y_offset + size2) &&
       (x == x_offset || x == x_offset + size2 - 1))) {
    printf("*");

was

  if (is_between(x, x_offset, x_offset + size2) && 
      is_either2(x, y_offset, y_offset + size2 - 1)) ||
      is_between(y, y_offset, y_offset + size2) && 
      is_either2(x, x_offset, x_offset + size2 - 1))) {
    printf("*");

It maybe easier to see the mistake

// y is equal to either y_offset OR y_offset + size2 - 1 )  
//         v
is_either2(x, y_offset, y_offset + size2 - 1)

Also it helps deal with contract ambiguities of inclusive or exclusive endpoints in the idea of in between as we only need to amend/update the function/macro in_between() @Shawn.

Using the non-symmetric >=, < for in between is suspicious.

Upvotes: 1

Gor Asatryan
Gor Asatryan

Reputation: 924

check condition in if statement.

y is equal to either y_offset OR y_offset + size2 - 1 is y == y_offset || y == y_offset + size2 - 1

change (x == y_offset || x == y_offset + size2 - 1)) to (y == y_offset || y == y_offset + size2 - 1)) in second line of if

if (((x >= x_offset && x < x_offset + size2) && // (x is between x_offset  and x_offset +size2)
     (y == y_offset || y == y_offset + size2 - 1)) // y is equal to either y_offset OR y_offset + size2 - 1
     ||
     ((y >= y_offset && y < y_offset + size2) && //(y is between y_offset and y_offset + size2)
     (x == x_offset || x == x_offset + size2 - 1))) // x is equal to either x_offset OR x_offset + size2 -1
{
}

Upvotes: 0

Related Questions