public class findeven{
public static void main(String[] args){
int [] number= new int[]{1,2,3,4,5,6,7,8,9};
int[] even;
int[] odd;
even = new int[10];
odd = new int[10];
for( int i=0; i<number.length;i++){
if(number[i]%2==0){
number[i]=even;
}
else
number[i]=odd;
}
}
}
Consuming even and odd numbers from an array
Page 1 of 16 Replies - 1338 Views - Last Post: 17 April 2012 - 07:00 PM
#1
Consuming even and odd numbers from an array
Posted 16 April 2012 - 08:15 PM
I'm having difficulty getting the odd numbers to go into the odd array and the even numbers to go into the even array
Replies To: Consuming even and odd numbers from an array
#2
Re: Consuming even and odd numbers from an array
Posted 16 April 2012 - 08:18 PM
You're setting an int to int[]. That's wrong. If you're not going to put any value in the arrays int[] even and int[] odd, what's the point of declaring them?
#3
Re: Consuming even and odd numbers from an array
Posted 16 April 2012 - 08:25 PM
so i have changed it but not im getting a new error
File: C:\Users\Chimaroke\Desktop\Rahman\Computer science\spring 2012\Homework\findeven.java [line: 12]
Error: C:\Users\Chimaroke\Desktop\Rahman\Computer science\spring 2012\Homework\findeven.java:12: '.class' expected
File: C:\Users\Chimaroke\Desktop\Rahman\Computer science\spring 2012\Homework\findeven.java [line: 15]
Error: C:\Users\Chimaroke\Desktop\Rahman\Computer science\spring 2012\Homework\findeven.java:15: '.class' expected
import java.util.*;
public class findeven{
public static void main(String[] args){
int [] number= new int[]{1,2,3,4,5,6,7,8,9};
int[] odd;
even = new int[10];
odd = new int[10];
for( int i=0; i<number.length;i++){
if(number[i]%2==0){
number[i]=int[] even;
}
else
number[i]=int [] odd;
}
}
}
File: C:\Users\Chimaroke\Desktop\Rahman\Computer science\spring 2012\Homework\findeven.java [line: 12]
Error: C:\Users\Chimaroke\Desktop\Rahman\Computer science\spring 2012\Homework\findeven.java:12: '.class' expected
File: C:\Users\Chimaroke\Desktop\Rahman\Computer science\spring 2012\Homework\findeven.java [line: 15]
Error: C:\Users\Chimaroke\Desktop\Rahman\Computer science\spring 2012\Homework\findeven.java:15: '.class' expected
#4
Re: Consuming even and odd numbers from an array
Posted 16 April 2012 - 08:28 PM
What are you trying with this? You've absolutely lost me,
#5
Re: Consuming even and odd numbers from an array
Posted 16 April 2012 - 08:31 PM
the array numbers has numbers from 1-9
what i want to do is to take the odd numbers in the array called numbers and put them into a new array called odd
and i want to do thesame for even numbers but put the even numbers in an array called even
what i want to do is to take the odd numbers in the array called numbers and put them into a new array called odd
and i want to do thesame for even numbers but put the even numbers in an array called even
#6
Re: Consuming even and odd numbers from an array
Posted 16 April 2012 - 09:33 PM
The problem is you are not using arrays properly. when you trying to insert a value from the number array it should look something like
even/odd[index value]= numbers[i]
you need to figure out a way to determine the index value of the odd or even arrays.
even/odd[index value]= numbers[i]
you need to figure out a way to determine the index value of the odd or even arrays.
#7
Re: Consuming even and odd numbers from an array
Posted 17 April 2012 - 07:00 PM
you need two more variables: nbEven and nbOdd and update them when you detect one or the other
now you have nbOdd numbers in odd[] and nbEven numbers in even[]
Happy coding
int [] number= new int[]{1,2,3,4,5,6,7,8,9};
int[] odd = new int[number.length]; // worst case
int[] even = new int[number.length];
int nbOdd = 0, nbEven = 0;
for(int i = 0; i < number.length; ++i) {
if(number[i] % 2 == 0)
even[nbEven++] = number[i];
else
odd[nbOdd++] = number[i];
}
now you have nbOdd numbers in odd[] and nbEven numbers in even[]
Happy coding
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote



|