1

The problem im getting is that with oddSum the value outputted is the same as evenSum, and the value for sum of all elements is 0.

I cant quite see where im going wrong as the loops are pretty similar and if the even one works the others should too?

Here is my code anyway:

int evenData[] = new int [10];
int oddData[] = new int [10];
int sum = 0;
int evenSum = 0;
int oddSum = 0;

int[] data = {3, 2, 5, 7, 9, 12, 97, 24, 54};
for(int index = 0; index < data.length; index++)
{
    if (data[index] % 2 == 0)
    {

        int temp = data[index];
        data[index] = evenData[index];
        evenData[index] = temp;

    }

    else
    {
        int temp = data[index];
        data[index] = oddData[index];
        oddData[index] = temp;
    }

}
for(int evenIndex = 0; evenIndex < evenData.length; evenIndex++)
{

    evenSum =evenData[evenIndex] + evenSum;

}
System.out.print("Sum of even elements: " + evenSum);

for(int oddIndex = 0; oddIndex < oddData.length; oddIndex++)
{

    oddSum = oddData[oddIndex] + oddSum;

}
System.out.print("Sum of odd elements: " + oddSum);

for(int index = 0; index < data.length; index++)
{
    sum = data[index] + sum;
}
System.out.print("Sum of all elements: " + sum);

2 Answers 2

2

You are getting same value for even and odd because you are printing the same value: -

System.out.print("Sum of odd elements: " + evenSum);

Also, your final sum is zero because you are making out all the elements of your original array as zero, as you are swapping your elements with the elements in evenData and oddData, which are zero initially.

int temp = data[index];
data[index] = evenData[index]; // This code assigns a value 0 to current index.
evenData[index] = temp;

So, you are iterating your array, and assigning 0 to each of your index, while adding the previous element to the new array.


I would say that you are needlessly using 2 extra array and 3 extra loops. Why not just create a sum in the place where you are iterating your original array?

In fact, all your sums can be computed in a single loop: -

for(int index = 0; index < data.length; index++)
{
    sum += data[index];

    if (data[index] % 2 == 0)
    {
        // int temp = data[index];
        // data[index] = evenData[index];
        // evenData[index] = temp;

        evenSum += data[index];
    }
    else
    {
        // int temp = data[index];
        // data[index] = oddData[index];
        // oddData[index] = temp;

        oddSum += data[index];  
    } 
}

System.out.println("Even Sum: "  + evenSum);
System.out.println("Odd Sum: "  + oddSum);
System.out.println("Total Sum: "  + sum);

So, you don't need to create extra arrays for even and odd numbers.

And, also your 4 loops have now been condensed to just a single loop.

4
  • Yeh sorry I noticed that when I posted ahah. But as for the all elements sum, you're saying that the swapping of the arrays makes the original array values 0? Commented Nov 20, 2012 at 20:31
  • @user1840051.. Yeah, because when you initialize an array, all its elements are initialized to default value 0. So, when you swap the current element with the element in your evenArray, then the current array will now contain 0.
    – Rohit Jain
    Commented Nov 20, 2012 at 20:34
  • Yes, I realize now that I didn't need to swap them, just assign the even values to another array. And thanks for showing me the more condensed way of doing it, very useful :) Commented Nov 20, 2012 at 20:40
  • @user1840051.. Yes exactly. Now that you got it, please use the way I posted. It is much more efficient, and readable also.
    – Rohit Jain
    Commented Nov 20, 2012 at 20:42
0
int temp = data[index]; 
data[index] = evenData[index]; 
evenData[index] = temp; 

Looking at your above lines, evenDate is empty and in second line you are setting your data array to be empty. You are repeating the same mistake in oddDate line as well.

Why don't you use just try the following?

    for(int index = 0; index < data.length; index++)
    { 
        if (data[index] % 2 == 0) { 
            int temp = data[index]; 
            evenData[index] = temp;

        } else { 
            int temp = data[index]; 
            oddData[index] = temp; } 
        } 
    }

    for(int evenIndex = 0; evenIndex < evenData.length; evenIndex++) 
    { 
        //since length of all 3 data,even, odd arrays are the same
        //you can put both sum operation in one for loop

        evenSum = evenData[evenIndex] + evenSum; 
        oddSum = oddData[evenIndex] + oddSum; 
        sum = data[evenIndex] + sum;  
    }

    System.out.print("Sum of even elements: " + evenSum); 
    //you have put evenSum for below odeUm printing in ur code
    System.out.print("Sum of odd elements: " + oddSum); 
    System.out.print("Sum of all elements: " + sum); 
2
  • Oh man Rohit, the phone updates are weird. If no sweat, could u please edit it mate? Thanks a heap. :-)
    – bonCodigo
    Commented Nov 20, 2012 at 20:56
  • Nice, now it looks normal ;-)
    – bonCodigo
    Commented Nov 20, 2012 at 21:05

Not the answer you're looking for? Browse other questions tagged or ask your own question.