3

Write a program to accept 15 random numbers into a 1D array. Sort the array using Bubble Sort. After sorting, transfer and store all the even numbers and odd numbers into separate arrays. Display the unsorted original array and the sorted even and odd arrays with appropriate messages

I have been trying to to create the even and odd arrays but its giving null values while printing

import java.util.Scanner;
class Assignment10
{
    public static void main()
    {
        Scanner sc = new Scanner(System.in);
        int arr[] = new int[15];
        
        System.out.println("Enter 15 numbers");
        for(int i = 0; i<arr.length; i++)//intializing the main array
        {
            arr[i] = sc.nextInt();
        }

        int temp;
        for(int i = 0; i<arr.length; i++)
        {
            for(int j = 0; j<arr.length-1; j++)
            {
                if(arr[j] > arr[j+1])
                {
                    temp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = temp;
                }

            }
        }
        int counter = 0;

        for(int i = 0; i<arr.length; i++)
        {
            
                if(arr[i]%2 == 0)
                {
                    counter ++;
                }
        }
        
         int[] even = new int[15];
        int[] odd = new int[15];
        int evenCount = 0, oddCount = 0;
        
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] % 2 == 0) {
                even[evenCount++] = arr[i];
            } else {
                odd[oddCount++] = arr[i];
            }
        }
        System.out.println("Even array : ");

        for(int i = 0; i<even.length; i++)
        {
            System.out.print(even[i] + " ");
        }
        
        System.out.println("Odd array : ");
        for(int i = 0; i<even.length; i++)
        {
            System.out.println();
            System.out.print(odd[i] + " ");
        }
    }
}

I expected this: Original array : 23 4 5 78 34 56 12 20 33 2 76 11 29 9 54 Even array: 2 4 12 20 34 54 56 76 78 Odd array: 5 9 11 23 29 33

But im getting null values while printing the even and odd arrays

Output: Enter 15 numbers 23 46 74 523 673 254 34 12 345 5755 344 33 5675 23242 3454 Even array : 12 34 46 74 254 344 3454 23242 0 0 0 0 0 0 0 Odd array : 23 33 345 523 673 5675 5755 0 0 0 0 0 0 0 0

  • An uninitialized int array’s elements are all zero when it is created. Use evenCount and oddCount in your last two printing loops. That is, `i – VGR Aug 27 '23 at 14:34

3 Answers3

1

Your even and odd arrays have the same length as the original array (15), so if you print the full arrays, you'll see 0 values in the high indices of the arrays.

You should only print the even and odd array elements to which you assigned values. You can use your evenCount and oddCount counters for this purpose.

Change

    for(int i = 0; i<even.length; i++)
    {
        System.out.print(even[i] + " ");
    }
    
    System.out.println("Odd array : ");
    for(int i = 0; i<even.length; i++)
    {
        System.out.println();
        System.out.print(odd[i] + " ");
    }

to

    for(int i = 0; i<evenCount; i++)
    {
        System.out.print(even[i] + " ");
    }
    
    System.out.println("Odd array : ");
    for(int i = 0; i<oddCount; i++)
    {
        System.out.println();
        System.out.print(odd[i] + " ");
    }
Eran
  • 387,369
  • 54
  • 702
  • 768
0

You have a counter variable to count the even numbers in the original array, which is not used. Use it to calculate the right size of your new arrays.

int[] even = new int[counter];
int[] odd = new int[15 - counter];
Asmir
  • 1
  • 1
0

"... I have been trying to to create the even and odd arrays but its giving null values while printing ..."

This is because even and odd were initialized to 15 elements.

A source array of 15 elements won't produce 15 elements for both outputs arrays.
Thus, at least one array will contain remaining, uninitialized values.

int[] even = new int[15];
int[] odd = new int[15];

You can trim the arrays with the Arrays#copyOfRange method.

even = Arrays.copyOfRange(even, 0, evenCount);
odd = Arrays.copyOfRange(odd, 0, oddCount);

Or, use a List object, which has a non-fixed size.

List<Integer> even = new ArrayList<>();
List<Integer> odd = new ArrayList<>();
for (int n : arr) (n % 2 == 0 ? even : odd).add(n);

Additionally, another approach would be to count the even and odd values first, and then create the arrays in a subsequent iteration.

int evenCount = 0, oddCount = 0;
for (int i = 0; i < arr.length; i++) {
    if (arr[i] % 2 == 0) evenCount++;
    else oddCount++;
}

int[] even = new int[evenCount];
int[] odd = new int[oddCount];
evenCount = oddCount = 0;

for (int i = 0; i < arr.length; i++) {
    if (arr[i] % 2 == 0) {
        even[evenCount++] = arr[i];
    } else {
        odd[oddCount++] = arr[i];
    }
}

Also, you can print the arrays with Arrays#toString, if you want to avoid the for-loop.

System.out.println("Even array : " + Arrays.toString(even));
System.out.println("Odd array : " + Arrays.toString(odd));

On a final note, you have a typo in your code.
The odd array loop is conditional to i<even.length.

System.out.println("Odd array : ");
for(int i = 0; i<even.length; i++)
{
    System.out.println();
    System.out.print(odd[i] + " ");
}

Output

Enter 15 numbers
23 4 5 78 34 56 12 20 33 2 76 11 29 9 54
Even array : [2, 4, 12, 20, 34, 54, 56, 76, 78]
Odd array : [5, 9, 11, 23, 29, 33]
Enter 15 numbers
23 46 74 523 673 254 34 12 345 5755 344 33 5675 23242 3454
Even array : [12, 34, 46, 74, 254, 344, 3454, 23242]
Odd array : [23, 33, 345, 523, 673, 5675, 5755]

Here is how I would implement the same code.

Scanner scanner = new Scanner(System.in);
List<Integer> list, even, odd;
even = new ArrayList<>();
odd = new ArrayList<>();
list = new ArrayList<>();
for (String string : scanner.nextLine().split(" +"))
    list.add(Integer.parseInt(string));
for (int a = 0, b; a < list.size() - 1; a++)
    for (b = 0; b < list.size() - a - 1; b++)
        if (list.get(b) > list.get(b + 1))
            Collections.swap(list, b, b + 1);
list.forEach(x -> (x % 2 == 0 ? even : odd).add(x));
System.out.printf("%s%n%s%n", even, odd);

Output

23 4 5 78 34 56 12 20 33 2 76 11 29 9 54
[2, 4, 12, 20, 34, 54, 56, 76, 78]
[5, 9, 11, 23, 29, 33]
Reilas
  • 3,297
  • 2
  • 4
  • 17