1 Replies - 2076 Views - Last Post: 21 October 2012 - 07:54 AM Rate Topic: -----

#1 Penders  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 21-October 12

Project troubles with arrays

Posted 21 October 2012 - 06:55 AM

"Write a method that has an int array (the shape) and two other ints (the height and width of the rectangle) as parameters, and returns another array representing the starting positions of valid fits of the rectangle in that shape. For the example above, for the shape array

[1,3,3,4,2,3,3,2,2,2]

and the rectangle described by the two parameters (height first, then width)

3 2

your code should return the array:

[1,2,5]



public class fun1 {

public static void main(String [] args) {

    int [] shape = {1,3,3,4,2,3,3,2,2,2};
    int [] result = fit(shape,3,2);
    System.out.println(Arrays.toString(result));

}


public static int [] fit(int [] shape, int height, int width) {



    int length = shape.length;
    int widthCounter = 0;
    int i = 0;
    int k = 0;
    int countCount = 0;

    for (i = 0; i < shape.length; i++){



        if (shape[i] >= height){




            widthCounter++;

            if (widthCounter == width){

                k++;


                widthCounter = 0;

            } 


        } 

    } 

    int [] freshArray = new int[k];


    System.out.println("number of elements built: " + k);
    return freshArray;

}

}


Now I feel as though I am very close but using arraylists or hashmaps are forbidden, and so I am having trouble figuring out a way to store the index position. I think I need to keep a count after:

if (shape[i] >= height){

but I just can't figure out the syntax. I also need to set i = to the value that is >= height so I can start i counting again after finding a box fit, I believe that covers it for logic, any syntax help would be greatly appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Project troubles with arrays

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2197
  • View blog
  • Posts: 5,224
  • Joined: 10-September 10

Re: Project troubles with arrays

Posted 21 October 2012 - 07:54 AM

I might be the slowest guy here, but I have no idea what you're talking about. What is the shape array? Is that an array of x/y points that define the shape of some polygon, the last pair connecting to the first? I kinda get fitting a rectangle within another shape, but I'm not sure how that's described by the array [1,2,5]. Can you explain better?

Or if I'm the only one not getting it, just ignore me.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1