Rich Burgis
Posts: 17
Nickname: songbird
Registered: Mar, 2003
|
|
Re: help!
|
Posted: Mar 30, 2003 1:16 PM
|
|
As I see it you are trying (I'm guessing here since you aren't displaying your output array) to create an array that looks like
10 6 2 9 33 9 5 1 17 4 11 8
(or maybe 5 1 9 17 33 11 9 10 6 2 4 8 )
If that's the case you need to do a couple of things. First and foremost you need to actually create the output array. You don't do that
When you say
int result[ ] [ ];
you declare the array, but you haven't reserved any space for it.
you need to say something like
int result[ ][ ] = new int[ 2 ][ ori.length / 2 ];
(in the alternative case you'd say new int[ 2] [ ori.length ]; )
I suspect you're getting a runtime out of bounds error. This will fix it.
Second you set the two indexes as int even = 0, odd = 1;
If you want them to be independent idexes to the two parts of the array they should both start at zero. (Better you should have one index and use it for both, but that will cause unnecessary complication at this point.)
Finally what is ori % 2?
if you want to test if the element is even or odd you need to use an index. i.e. ori[ i ] % 2.
Good luck Rich > Well, actually this is what i have done but it does not > seem to work, can anyone explain? Thanx > > > class OddEven
> {
> public static void main(String[] args) {
> int[] input = {10, 5, 6, 1, 2, 17, 9, 4, 33, 11, 9, 8};
>
> int[][] output = split(input);
>
> System.out.print("original: 10, 5, 6, 1, 2, 17, 9, 4, 33,
> 11, 9, 8");
>
> for (int i = 0; i < input.length; ++i) {
> if ( i % 2 == 0);
> System.out.print(" even: " + i);
>
> if ( i % 2 == 1);
> System.out.print(" odd: " + i);
> }
>
> public static int[][] split(int[] ori) {
>
> int i,even = 0,odd = 1;
> int result[][];
>
> for(i = 0; i < ori.length; ++i){
> if( ori % 2 == 0) {
> result[0][even] = ori;
> even++;
> }
> else {
> result[1][odd] = ori;
> odd++;
> }
> return result;
> }
> }
>
>
|
|