Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8860771
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T15:20:01+00:00 2026-06-14T15:20:01+00:00

The problem im getting is that with oddSum the value outputted is the same

  • 0

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);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T15:20:03+00:00Added an answer on June 14, 2026 at 3:20 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a strange problem getting the color of the point that was touched.
I'm having a problem getting XCode to deal with a particular file structure that
I have a problem that is getting embarrassingly out of hand. I'm working with
So today's problem is getting me mad because that should be easy and i
I have a problem. I have a view that is getting back no results
I am having a problem getting an old Ruby on Rails 2 app that
I have an asp.net c# website that I am having a problem getting the
I'm having a problem getting ReSharper to see the Machine.Specification tests I've written. The
The problem I'm getting is that I'm getting heap corruption messages during deletion of
getting problem is that and how can solve this .I trying the internet is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.