Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Creating an even and odd array of specific parameters from an original array

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

Your original number of values is 15, but the even and odd arrays are also of size 15 each. The size of the even and odd arrays should be eventCount and oddCount. You will not get null values then.

Create evenCount and oddcount before creating the arrays.

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];
   }
}

int[] even = new int[eventCount]; // Change #1: size = evenCount
int[] odd = new int[oddCount]; // Change #2: size = oddCount

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 < odd.length; i++) // Change #3: i < odd.length
{
    System.out.println();
    System.out.print(odd[i] + " ");
}

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading