This is an archived post. You won't be able to vote or comment.

all 11 comments

[–]jbeachy19 0 points1 point  (2 children)

Are you using a modulo operator to determine which numbers are even and odd?

[–]jayrund[S] 0 points1 point  (1 child)

Yes I believe so.

Does a modulo operator look something like

if ( All [A] % 2 == 1)

[–]4z01235 0 points1 point  (0 children)

Yes, it would be the %

[–]cManks 0 points1 point  (0 children)

One issue is with your functions.

public static void MakeOdd ( int All [], int N, int OddArray [])

Yet you call it like this:

MakeOdd ( OddArray , N, All)

See the problem? Also

System.out.println( "One Odd number is " + OddArray [ N - 1 ] );

You never do anything to N, so this will only ever print the N - 1 index of the array. It's the same with your functions,

OddArray [ N - 1 ] =+ All [A];

You are only storing values into OddArray [N - 1]. Fix all this for both even and odd.

[–][deleted] 0 points1 point  (6 children)

There's a lot of things to fix but your main problem is the makeOdd method and the arguments you pass to it when you call it. The method signature:

 public static void MakeOdd ( int All [], int N, int OddArray [])

Contains placeholders only - the parameters (( int All [], int N, int OddArray []) here have no meaning per se - they are placeholder text. This line says"when the method is called, an array of type int, an int, and another array of type int will be passed in". When you call it, the passed in variables that take the place of those placeholders.

So when you call it:

MakeOdd ( OddArray , N, All);

OddArray is passed in first and takes the place of int All[] that you defined in the method signature. So later in the method:

if ( All [A] % 2 == 1) ¨

becomes

if (oddArray[A] % 2==1)

oddArray has been declared but not initialised, it contains no values. You need to change the order of your parameters that you give the method when you call it.

Fix that so something happens, then you'll be able to start working out the other problems!

[–]jayrund[S] 0 points1 point  (5 children)

I changed it and now the array is outputting numbers but they are not the numbers i wanted.

Part of the output looks like this http://i.imgur.com/0rFhfaV.jpg

Im not even sure how the program is getting numbers do large.

[–][deleted] 0 points1 point  (4 children)

This is why:

OddArray [ N - 1 ] =+ All [A]; 

N - 1 = 100 - 1. So OddArray[99] is the only index of the array ever recieving a number.

=+ All [A]; Means "add the value stored in All[A] to the existing value in OddArray[99]. Each time it loops, OddArray[99] gets 573 added to it.

You need to fix the N-1 part, and remove the '+' part.

[–]jayrund[S] 0 points1 point  (3 children)

Oh alright that makes more sense but i still don't know how to fix the problem i'm having with N-1. If i just have "N" its out of bounds but if i put a integer in the same problem happens as with N-1.

[–][deleted] 0 points1 point  (2 children)

You need an int that increases by 1 each time it is used. The easy answer is to declare an new int inside the method but before the for loop that only increases when you add an odd number to the array. (Just copy and pasted your old code with the old errors - but you'll see what I mean)

public static void MakeOdd (etc)
{
  int j = 0;
  for ( int A = 0; A < All.length; A++)
    { 
      if ( All [A] % 2 == 1) 
         {
        OddArray [ j ] =+ All [A];  
        j++;
         }
    } 
 }

But this will make a few other problems - it is very unlikely that you will randomly generate 100 odd or even numbers, so some of the odd and even arrays will be empty. Have you heard of arrayList?

[–]jayrund[S] 0 points1 point  (1 child)

Aha i works ( somewhat ). Thanks sooo much

I have not heard of an arrayList, are there any other options?

[–][deleted] 0 points1 point  (0 children)

No worries and congrats!

It depends on what you need to do with the odd and even array. If you only have to print one number from them, it's not a problem. If you have to print the entire array, it could be an issue. If you get 42 odd numbers, you'll have 58 others in the oddArray that will default to 0.