Jump to content

[C#] splitting a 2D array

Stardar1

I have a 2D array of Button objects (yes, I know winForms is old, but this is for school). the array is always an even-numbered perfect square, and I need to split it into quadrants. 

 

It is worth noting that this is inside a method that will be called recursively, and that I need all 4 quadrants in separate 2D arrays. I'd like this to be as efficient as possible. 

Different PCPartPickers for different countries:

UK-----Italy----Canada-----Spain-----Germany-----Austrailia-----New Zealand-----'Murica-----France-----India

 

10 minutes ago, Stardar1 said:

Well, with an i7, GTX 1080, Full tower and flashy lights, it can obviously only be for one thing:

Solitaire. 

Link to comment
Share on other sites

Link to post
Share on other sites

6 hours ago, Stardar1 said:

I need to split it into quadrants. 

Can you define quadrants? Where should the splits occur? 

Interestingly enough, all even perfect squares are divisible by four. Therefore, you could always just divide the length of your list by four, and put that many items in the new arrays, iterating through them sequentially. (basically, you will be moving 1/4 of your inner arrays to a new outer array 4 times). This only works if you can only split up your list sequentially.

ENCRYPTION IS NOT A CRIME

Link to comment
Share on other sites

Link to post
Share on other sites

17 hours ago, straight_stewie said:

Can you define quadrants? Where should the splits occur? 

Interestingly enough, all even perfect squares are divisible by four. Therefore, you could always just divide the length of your list by four, and put that many items in the new arrays, iterating through them sequentially. (basically, you will be moving 1/4 of your inner arrays to a new outer array 4 times). This only works if you can only split up your list sequentially.

I have a 2d array with dimensoins 2^n. therefore, it is always divisble by 2, I wish to create 4 new 2D arrays out of the quadrants, like this:

 

Image result for quadrants

Different PCPartPickers for different countries:

UK-----Italy----Canada-----Spain-----Germany-----Austrailia-----New Zealand-----'Murica-----France-----India

 

10 minutes ago, Stardar1 said:

Well, with an i7, GTX 1080, Full tower and flashy lights, it can obviously only be for one thing:

Solitaire. 

Link to comment
Share on other sites

Link to post
Share on other sites

21 hours ago, Stardar1 said:

I have a 2d array with dimensoins 2^n. therefore, it is always divisble by 2, I wish to create 4 new 2D arrays out of the quadrants, like this:

All perfect even squares are also divisible by four because: Squares of even numbers are even (and in fact divisible by 4), since (2n)2 = 4n2. Since an odd times an odd number is always odd, and you said that your squares are always even squares, I can reasonably assume that your array meets the above criteria of being a perfect square of an even number, and therefore it is divisible by four.

 

Now, onto the real problem, I'm slightly confused about exactly where you are trying to divide them still. You said "out of the quadrants" of the below image:

21 hours ago, Stardar1 said:

 

Image result for quadrants


What I'm confused about is: What is the relationship between the button objects and this graph?



 

ENCRYPTION IS NOT A CRIME

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, straight_stewie said:

All perfect even squares are also divisible by four because: Squares of even numbers are even (and in fact divisible by 4), since (2n)2 = 4n2. Since an odd times an odd number is always odd, and you said that your squares are always even squares, I can reasonably assume that your array meets the above criteria of being a perfect square of an even number, and therefore it is divisible by four.

 

Now, onto the real problem, I'm slightly confused about exactly where you are trying to divide them still. You said "out of the quadrants" of the below image:


What I'm confused about is: What is the relationship between the button objects and this graph?



 

I'm using windows forms to create a grid of buttons, they are stored in a 2D array. I wish to split the array into 4 2D arrays, so that the dimensions of the new arrays are as the graph shows. 

 

basically, if i have a 2d array of size 16x16, i want to split it into 4 8x8 arrays. 

 

 

Different PCPartPickers for different countries:

UK-----Italy----Canada-----Spain-----Germany-----Austrailia-----New Zealand-----'Murica-----France-----India

 

10 minutes ago, Stardar1 said:

Well, with an i7, GTX 1080, Full tower and flashy lights, it can obviously only be for one thing:

Solitaire. 

Link to comment
Share on other sites

Link to post
Share on other sites

You may find this SO thread helpful. I'd certainly be thinking about implementing this as an extension method.

The single biggest problem in communication is the illusion that it has taken place.

Link to comment
Share on other sites

Link to post
Share on other sites

5 minutes ago, Nuluvius said:

You may find this SO thread helpful. I'd certainly be thinking about implementing this as an extension method.

I'm trying to split an even square 2D array into quadrants, and this will be called recursively. That link was not really helpful. 

 

I'm just wondering if there is an easier way than 4 sets of nested loops. 

Different PCPartPickers for different countries:

UK-----Italy----Canada-----Spain-----Germany-----Austrailia-----New Zealand-----'Murica-----France-----India

 

10 minutes ago, Stardar1 said:

Well, with an i7, GTX 1080, Full tower and flashy lights, it can obviously only be for one thing:

Solitaire. 

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, Stardar1 said:

That link was not really helpful.

Your explanations are not really helpful hence the low number of responses you have received thus far. I suggest you work on clarifying what you are trying to achieve.

1 minute ago, Stardar1 said:

I'm just wondering if there is an easier way than 4 sets of nested loops. 

There will be far better ways to accomplish a solution than that... if only we had something more to work with.

The single biggest problem in communication is the illusion that it has taken place.

Link to comment
Share on other sites

Link to post
Share on other sites

14 minutes ago, Nuluvius said:

Your explanations are not really helpful hence the low number of responses you have received thus far. I suggest you work on clarifying what you are trying to achieve.

There will be far better ways to accomplish a solution than that... if only we had something more to work with.

I have a square 2d array with dimensions 2^n and 2^n. I wish to split it into 4 smaller 2d arrays, each one containing a quadrant. this will be called recursively

Different PCPartPickers for different countries:

UK-----Italy----Canada-----Spain-----Germany-----Austrailia-----New Zealand-----'Murica-----France-----India

 

10 minutes ago, Stardar1 said:

Well, with an i7, GTX 1080, Full tower and flashy lights, it can obviously only be for one thing:

Solitaire. 

Link to comment
Share on other sites

Link to post
Share on other sites

23 minutes ago, Stardar1 said:

I have a square 2d array with dimensions 2^n and 2^n. I wish to split it into 4 smaller 2d arrays, each one containing a quadrant. this will be called recursively

What have you done so far? What issues are you having? 

 

4 nested loops sounds like the easiest solution to me. 4 nested loops will have a runtime O(n), where n is the number of cells in the original square. 

Link to comment
Share on other sites

Link to post
Share on other sites

42 minutes ago, PlutoNZL said:

What have you done so far? What issues are you having? 

 

4 nested loops sounds like the easiest solution to me. 4 nested loops will have a runtime O(n), where n is the number of cells in the original square. 

the issue is that I will be calling this recursively, and the entire array will eventually need to have been divided into 2x2 squares

Different PCPartPickers for different countries:

UK-----Italy----Canada-----Spain-----Germany-----Austrailia-----New Zealand-----'Murica-----France-----India

 

10 minutes ago, Stardar1 said:

Well, with an i7, GTX 1080, Full tower and flashy lights, it can obviously only be for one thing:

Solitaire. 

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, Stardar1 said:

the issue is that I will be calling this recursively, and the entire array will eventually need to have been divided into 2x2 squares

I don't understand the issue you are having. You just explained the task, not what you're having an issue with. 

 

Can't you just split the square into four quadrants and call the split method on each quadrant until the square is a 2x2?

Link to comment
Share on other sites

Link to post
Share on other sites

2 hours ago, PlutoNZL said:

I don't understand the issue you are having. You just explained the task, not what you're having an issue with. 

 

Can't you just split the square into four quadrants and call the split method on each quadrant until the square is a 2x2?

I'm asking if there is a better way to do it than with 4 sets of nested for loops, as this gets called quite often

Different PCPartPickers for different countries:

UK-----Italy----Canada-----Spain-----Germany-----Austrailia-----New Zealand-----'Murica-----France-----India

 

10 minutes ago, Stardar1 said:

Well, with an i7, GTX 1080, Full tower and flashy lights, it can obviously only be for one thing:

Solitaire. 

Link to comment
Share on other sites

Link to post
Share on other sites

10 minutes ago, Stardar1 said:

I'm asking if there is a better way to do it than with 4 sets of nested for loops, as this gets called quite often

It depends on your implementation. If each of your for loops loops over the entire NxN square, then you're going to be checking each cell 4 times, which isn't very efficient. If each of your for loops loops over a quarter of the square then you'll only be checking each cell once. I don't see how you can get more efficient than that. 

Link to comment
Share on other sites

Link to post
Share on other sites

8 hours ago, Stardar1 said:

I'm asking if there is a better way to do it than with 4 sets of nested for loops, as this gets called quite often

You can do this recursively and for a very minimal speed increase iteratively, but to do so you would have to flatten what would have been the recursive stack since this would then be state information for each iterative cycle if that makes sense.

The single biggest problem in communication is the illusion that it has taken place.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×